mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
initial
This commit is contained in:
16
src/structures/Channel.js
Normal file
16
src/structures/Channel.js
Normal file
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
class Channel {
|
||||
constructor(client, data) {
|
||||
this.client = client;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
this.id = data.id;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Channel;
|
||||
17
src/structures/ClientUser.js
Normal file
17
src/structures/ClientUser.js
Normal file
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
const User = require('./User');
|
||||
|
||||
class ClientUser extends User {
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
this.verified = data.verified;
|
||||
this.email = data.email;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientUser;
|
||||
17
src/structures/DMChannel.js
Normal file
17
src/structures/DMChannel.js
Normal file
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
const Channel = require('./Channel');
|
||||
const User = require('./User');
|
||||
|
||||
class DMChannel extends Channel{
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
this.recipient = this.client.store.add('users', new User(this.client, data.recipient));
|
||||
this.lastMessageID = data.last_message_id;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DMChannel;
|
||||
91
src/structures/Guild.js
Normal file
91
src/structures/Guild.js
Normal file
@@ -0,0 +1,91 @@
|
||||
'use strict';
|
||||
|
||||
const User = require('./User');
|
||||
const GuildDataStore = require('./datastore/GuildDataStore');
|
||||
const TextChannel = require('./TextChannel');
|
||||
const VoiceChannel = require('./VoiceChannel');
|
||||
const Constants = require('../Util/Constants');
|
||||
|
||||
class Guild {
|
||||
constructor(client, data) {
|
||||
this.client = client;
|
||||
this.store = new GuildDataStore();
|
||||
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.unavailable) {
|
||||
this.available = false;
|
||||
this.id = data.id;
|
||||
} else {
|
||||
this.available = true;
|
||||
this.setup(data);
|
||||
}
|
||||
}
|
||||
|
||||
_addMember(guildUser) {
|
||||
let user = this.client.store.NewUser(guildUser.user);
|
||||
this.store.memberData[user.id] = {
|
||||
deaf: guildUser.deaf,
|
||||
mute: guildUser.mute,
|
||||
joinDate: new Date(guildUser.joined_at),
|
||||
roles: guildUser.roles,
|
||||
};
|
||||
if (this.client.ws.emittedReady) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, user);
|
||||
}
|
||||
}
|
||||
|
||||
_updateMember(currentUser, newData) {
|
||||
let oldRoles = this.store.memberData[currentUser.id].roles;
|
||||
this.store.currentUser[currentUser.id].roles = newData.roles;
|
||||
if (this.client.ws.emittedReady) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_ROLES_UPDATE, this, oldRoles, this.store.memberData[currentUser.id].roles);
|
||||
}
|
||||
}
|
||||
|
||||
_removeMember(guildUser) {
|
||||
this.store.remove('members', guildUser);
|
||||
if (this.client.ws.emittedReady) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_REMOVE, this, guildUser);
|
||||
}
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
this.id = data.id;
|
||||
this.available = !data.unavailable;
|
||||
this.splash = data.splash;
|
||||
this.region = data.region;
|
||||
this.ownerID = data.owner_id;
|
||||
this.name = data.name;
|
||||
this.memberCount = data.member_count;
|
||||
this.large = data.large;
|
||||
this.joinDate = new Date(data.joined_at);
|
||||
this.icon = data.icon;
|
||||
this.features = data.features;
|
||||
this.emojis = data.emojis;
|
||||
this.afkTimeout = data.afk_timeout;
|
||||
this.afkChannelID = data.afk_channel_id;
|
||||
this.embedEnabled = data.embed_enabled;
|
||||
this.embedChannelID = data.embed_channel_id;
|
||||
this.verificationLevel = data.verification_level;
|
||||
this.features = data.features || [];
|
||||
|
||||
if (data.members) {
|
||||
this.store.clear('members');
|
||||
for (let guildUser of data.members) {
|
||||
this._addMember(guildUser);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.channels) {
|
||||
this.store.clear('channels');
|
||||
for (let channel of data.channels) {
|
||||
this.client.store.NewChannel(channel, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Guild;
|
||||
9
src/structures/Message.js
Normal file
9
src/structures/Message.js
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
class Message {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Message;
|
||||
22
src/structures/ServerChannel.js
Normal file
22
src/structures/ServerChannel.js
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const Channel = require('./Channel');
|
||||
|
||||
class ServerChannel extends Channel{
|
||||
constructor(guild, data) {
|
||||
super(guild.client, data);
|
||||
this.guild = guild;
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
this.type = data.type;
|
||||
this.topic = data.topic;
|
||||
this.position = data.position;
|
||||
this.permissionOverwrites = data.permission_overwrites;
|
||||
this.name = data.name;
|
||||
this.lastMessageID = data.last_message_id;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ServerChannel;
|
||||
13
src/structures/TextChannel.js
Normal file
13
src/structures/TextChannel.js
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
const ServerChannel = require('./ServerChannel');
|
||||
const TextChannelDataStore = require('./datastore/TextChannelDataStore');
|
||||
|
||||
class TextChannel extends ServerChannel {
|
||||
constructor(guild, data) {
|
||||
super(guild, data);
|
||||
this.store = new TextChannelDataStore();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TextChannel;
|
||||
20
src/structures/User.js
Normal file
20
src/structures/User.js
Normal file
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
class User {
|
||||
constructor(client, data) {
|
||||
this.client = client;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
this.username = data.username;
|
||||
this.id = data.id;
|
||||
this.discriminator = data.discriminator;
|
||||
this.avatar = data.avatar;
|
||||
this.bot = Boolean(data.bot);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = User;
|
||||
16
src/structures/VoiceChannel.js
Normal file
16
src/structures/VoiceChannel.js
Normal file
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const ServerChannel = require('./ServerChannel');
|
||||
|
||||
class VoiceChannel extends ServerChannel {
|
||||
constructor(guild, data) {
|
||||
super(guild, data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
this.bitrate = data.bitrate;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = VoiceChannel;
|
||||
43
src/structures/datastore/AbstractDataStore.js
Normal file
43
src/structures/datastore/AbstractDataStore.js
Normal file
@@ -0,0 +1,43 @@
|
||||
'use strict';
|
||||
|
||||
class AbstractDataStore{
|
||||
constructor() {
|
||||
this.data = {};
|
||||
}
|
||||
|
||||
register(name) {
|
||||
this.data[name] = {};
|
||||
}
|
||||
|
||||
add(location, object) {
|
||||
if (this.data[location][object.id]) {
|
||||
return this.data[location][object.id];
|
||||
} else {
|
||||
return this.data[location][object.id] = object;
|
||||
}
|
||||
}
|
||||
|
||||
clear(location) {
|
||||
this.data[location] = {};
|
||||
}
|
||||
|
||||
remove(location, object) {
|
||||
let id = (typeof object === 'string' || object instanceof String) ? object : object.id;
|
||||
if (this.data[location][id]) {
|
||||
delete this.data[location][id];
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
get(location, value) {
|
||||
return this.data[location][value];
|
||||
}
|
||||
|
||||
getAsArray(location) {
|
||||
return Object.values(this.data[location]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AbstractDataStore;
|
||||
111
src/structures/datastore/ClientDataStore.js
Normal file
111
src/structures/datastore/ClientDataStore.js
Normal file
@@ -0,0 +1,111 @@
|
||||
'use strict';
|
||||
|
||||
const AbstractDataStore = require('./AbstractDataStore');
|
||||
const Constants = require('../../util/Constants');
|
||||
const CloneObject = require('../../util/CloneObject');
|
||||
const Guild = require('../Guild');
|
||||
const User = require('../User');
|
||||
const DMChannel = require('../DMChannel');
|
||||
const TextChannel = require('../TextChannel');
|
||||
const VoiceChannel = require('../VoiceChannel');
|
||||
const ServerChannel = require('../ServerChannel');
|
||||
|
||||
class ClientDataStore extends AbstractDataStore{
|
||||
constructor(client) {
|
||||
super();
|
||||
|
||||
this.client = client;
|
||||
this.token = null;
|
||||
this.session = null;
|
||||
this.user = null;
|
||||
|
||||
this.register('users');
|
||||
this.register('guilds');
|
||||
this.register('channels');
|
||||
}
|
||||
|
||||
get pastReady() {
|
||||
return this.client.ws.emittedReady;
|
||||
}
|
||||
|
||||
NewGuild(data) {
|
||||
let already = this.get('guilds', data.id);
|
||||
let guild = this.add('guilds', new Guild(this.client, data));
|
||||
if (this.pastReady && !already) {
|
||||
this.client.emit(Constants.Events.GUILD_CREATE, guild);
|
||||
}
|
||||
|
||||
return guild;
|
||||
}
|
||||
|
||||
NewUser(data) {
|
||||
return this.add('users', new User(this.client, data));
|
||||
}
|
||||
|
||||
NewChannel(data, guild) {
|
||||
let already = this.get('channels', data.id);
|
||||
let channel;
|
||||
if (data.is_private) {
|
||||
channel = new DMChannel(this.client, data);
|
||||
}else {
|
||||
guild = guild || this.get('guilds', data.guild_id);
|
||||
if (guild) {
|
||||
if (data.type === 'text') {
|
||||
channel = new TextChannel(guild, data);
|
||||
guild.store.add('channels', channel);
|
||||
}else if (data.type === 'voice') {
|
||||
channel = new VoiceChannel(guild, data);
|
||||
guild.store.add('channels', channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (channel) {
|
||||
if (this.pastReady && !already) {
|
||||
this.client.emit(Constants.Events.CHANNEL_CREATE, channel);
|
||||
}
|
||||
|
||||
return this.add('channels', channel);
|
||||
}
|
||||
}
|
||||
|
||||
KillGuild(guild) {
|
||||
let already = this.get('guilds', guilds.id);
|
||||
this.remove('guilds', guild);
|
||||
if (already && this.pastReady) {
|
||||
this.client.emit(Constants.Events.GUILD_DELETE, guild);
|
||||
}
|
||||
}
|
||||
|
||||
KillUser(user) {
|
||||
this.remove('users', user);
|
||||
}
|
||||
|
||||
KillChannel(channel) {
|
||||
let already = this.get('channels', channel.id);
|
||||
this.remove('channels', channel);
|
||||
if (channel instanceof ServerChannel) {
|
||||
channel.guild.store.remove('channels', channel);
|
||||
}
|
||||
|
||||
if (already && this.pastReady) {
|
||||
this.client.emit(Constants.Events.CHANNEL_DELETE, channel);
|
||||
}
|
||||
}
|
||||
|
||||
UpdateGuild(currentGuild, newData) {
|
||||
let oldGuild = CloneObject(currentGuild);
|
||||
currentGuild.setup(newData);
|
||||
if (this.pastReady) {
|
||||
this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild);
|
||||
}
|
||||
}
|
||||
|
||||
UpdateChannel(currentChannel, newData) {
|
||||
let oldChannel = CloneObject(currentChannel);
|
||||
currentChannel.setup(newData);
|
||||
this.client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, currentChannel);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientDataStore;
|
||||
16
src/structures/datastore/GuildDataStore.js
Normal file
16
src/structures/datastore/GuildDataStore.js
Normal file
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const AbstractDataStore = require('./AbstractDataStore');
|
||||
|
||||
class GuildDataStore extends AbstractDataStore{
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.memberData = {};
|
||||
|
||||
this.register('members');
|
||||
this.register('channels');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildDataStore;
|
||||
12
src/structures/datastore/TextChannelDataStore.js
Normal file
12
src/structures/datastore/TextChannelDataStore.js
Normal file
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
const AbstractDataStore = require('./AbstractDataStore');
|
||||
|
||||
class TextChannelDataStore extends AbstractDataStore{
|
||||
constructor() {
|
||||
super();
|
||||
this.register('messages');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TextChannelDataStore;
|
||||
Reference in New Issue
Block a user