From b8315b79c7f65c8d68cdc03a5e1b5d265ca2e0bf Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Fri, 25 Aug 2017 21:08:58 +0100 Subject: [PATCH] Store and Model Refactor (#1618) * UserStore refactor * Create ChannelStore, remove redundant methods in ClientDataManager * Create GuildStore * Emoji stuff * Use a Base class where possible to reduce code duplication * Remove unnecessary comments from ChannelStore * Add Base._clone(); * Remove unused ClientDataManager methods * Refactor some more stuff * ESLint * Move Client#fetchUser to client.users.fetch * Remove .has checks and just see if .get is truthy * Fix guild member chunk error * ESLint * Fix typo * Fix channel storing for user bots * Remove ClientDataManager * GuildChannelStore * Reduce use of Util.cloneObject * and this one too * update typings * Fix MessageUpdate handling (#1507) * Fix role updates (probably fixes #1525) * fix for eslint * Address some of appell's comments * Use debug constant * start message store crap if it's ugly tell me later k * fix that * message store but works:tm: * clean up guild stuff * clean up channel store stuff * clean up channel event handling * does this message stuff work? find out soon in the next episode of dIsCoRd.Js * eslint * emojis * emojis and reactions * hi my name is eslint and im A LIL SHIT * so i forgot this huh * user stuff * Fix @class * Fix message stuff * Fix user store docs * Document all the bases * fix the super things * tidy up remove * fix textbasedchannel * fix that too * fix emoji store * make voice state stuff less ugly * make voice states even less ugly * make members less bad * fix bug * fix that too * fix reactions * how was this broken for so long * role store * remove super._patch from UserConnection * Rename UserProfile#setup to _patch * remove unnecessary super calls * update docgen dep (pls fix travis thx) * doc messagestore * fix docs * message store docs * things * DOCS PLS * more things * Document TextBasedChannel#messages as a MessageStore * Rebase * All the stores! --- jsdoc.json | 3 + package.json | 5 +- src/client/Client.js | 38 ++--- src/client/ClientDataManager.js | 112 ------------- src/client/actions/ChannelCreate.js | 7 +- src/client/actions/ChannelDelete.js | 20 +-- src/client/actions/ChannelUpdate.js | 20 +-- src/client/actions/GuildBanRemove.js | 2 +- src/client/actions/GuildDelete.js | 3 +- src/client/actions/GuildEmojiCreate.js | 5 +- src/client/actions/GuildEmojiDelete.js | 5 +- src/client/actions/GuildEmojiUpdate.js | 8 +- src/client/actions/GuildMemberGet.js | 2 +- src/client/actions/GuildMemberRemove.js | 15 +- src/client/actions/GuildRoleCreate.js | 4 +- src/client/actions/GuildRoleDelete.js | 15 +- src/client/actions/GuildRoleUpdate.js | 13 +- src/client/actions/GuildSync.js | 4 +- src/client/actions/GuildUpdate.js | 8 +- src/client/actions/MessageCreate.js | 66 +++----- src/client/actions/MessageDelete.js | 22 +-- src/client/actions/MessageDeleteBulk.js | 25 ++- src/client/actions/MessageReactionAdd.js | 16 +- src/client/actions/MessageReactionRemove.js | 7 +- .../actions/MessageReactionRemoveAll.js | 2 +- src/client/actions/MessageUpdate.js | 19 +-- src/client/actions/UserGet.js | 2 +- src/client/actions/UserUpdate.js | 4 +- .../packets/handlers/ChannelCreate.js | 4 +- .../packets/handlers/ChannelDelete.js | 13 +- .../packets/handlers/ChannelUpdate.js | 15 +- .../websocket/packets/handlers/GuildCreate.js | 19 ++- .../websocket/packets/handlers/GuildDelete.js | 5 +- .../packets/handlers/GuildMemberAdd.js | 12 +- .../packets/handlers/GuildMemberUpdate.js | 15 +- .../packets/handlers/GuildMembersChunk.js | 2 +- .../packets/handlers/MessageCreate.js | 12 +- .../packets/handlers/MessageDelete.js | 6 - .../packets/handlers/MessageDeleteBulk.js | 10 +- .../packets/handlers/MessageReactionAdd.js | 4 +- .../packets/handlers/MessageUpdate.js | 8 +- .../packets/handlers/PresenceUpdate.js | 11 +- .../websocket/packets/handlers/Ready.js | 10 +- .../packets/handlers/VoiceStateUpdate.js | 21 +-- src/stores/ChannelStore.js | 46 ++++++ src/stores/DataStore.js | 19 +++ src/stores/EmojiStore.js | 27 ++++ src/stores/GuildChannelStore.js | 28 ++++ src/stores/GuildMemberStore.js | 25 +++ src/stores/GuildStore.js | 20 +++ src/stores/MessageStore.js | 97 ++++++++++++ src/stores/ReactionStore.js | 27 ++++ src/stores/RoleStore.js | 25 +++ src/stores/UserStore.js | 35 ++++ src/structures/Base.js | 28 ++++ src/structures/Channel.js | 16 +- src/structures/ClientApplication.js | 19 +-- src/structures/ClientUser.js | 8 +- src/structures/DMChannel.js | 10 +- src/structures/Emoji.js | 16 +- src/structures/GroupDMChannel.js | 9 +- src/structures/Guild.js | 149 ++++++------------ src/structures/GuildAuditLogs.js | 2 +- src/structures/GuildChannel.js | 10 +- src/structures/GuildMember.js | 112 +++++++------ src/structures/Invite.js | 20 +-- src/structures/Message.js | 74 +++------ src/structures/MessageMentions.js | 3 +- src/structures/MessageReaction.js | 21 ++- src/structures/PermissionOverwrites.js | 4 +- src/structures/Role.js | 29 ++-- src/structures/TextChannel.js | 7 +- src/structures/User.js | 34 ++-- src/structures/UserConnection.js | 4 +- src/structures/UserProfile.js | 18 +-- src/structures/VoiceChannel.js | 4 +- src/structures/Webhook.js | 4 +- src/structures/interfaces/TextBasedChannel.js | 102 +----------- test/random.js | 4 +- typings | 2 +- 80 files changed, 837 insertions(+), 840 deletions(-) create mode 100644 jsdoc.json delete mode 100644 src/client/ClientDataManager.js create mode 100644 src/stores/ChannelStore.js create mode 100644 src/stores/DataStore.js create mode 100644 src/stores/EmojiStore.js create mode 100644 src/stores/GuildChannelStore.js create mode 100644 src/stores/GuildMemberStore.js create mode 100644 src/stores/GuildStore.js create mode 100644 src/stores/MessageStore.js create mode 100644 src/stores/ReactionStore.js create mode 100644 src/stores/RoleStore.js create mode 100644 src/stores/UserStore.js create mode 100644 src/structures/Base.js diff --git a/jsdoc.json b/jsdoc.json new file mode 100644 index 000000000..e64f528d9 --- /dev/null +++ b/jsdoc.json @@ -0,0 +1,3 @@ +{ + "plugins": ["node_modules/jsdoc-strip-async-await"] +} diff --git a/package.json b/package.json index b0d6b0230..c06608c86 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "types": "./typings/index.d.ts", "scripts": { "test": "npm run lint && npm run docs:test", - "docs": "docgen --source src --custom docs/index.yml --output docs/docs.json", - "docs:test": "docgen --source src --custom docs/index.yml", + "docs": "docgen --source src --custom docs/index.yml --output docs/docs.json --jsdoc jsdoc.json", + "docs:test": "docgen --source src --custom docs/index.yml --jsdoc jsdoc.json", "lint": "eslint src", "lint:fix": "eslint --fix src", "webpack": "parallel-webpack" @@ -51,6 +51,7 @@ "@types/node": "^8.0.0", "discord.js-docgen": "hydrabolt/discord.js-docgen", "eslint": "^4.0.0", + "jsdoc-strip-async-await": "^0.1.0", "parallel-webpack": "^2.0.0", "uglifyjs-webpack-plugin": "^1.0.0-beta.1", "webpack": "^3.0.0" diff --git a/src/client/Client.js b/src/client/Client.js index 7e1df3dc5..6c575f70f 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -3,7 +3,6 @@ const Constants = require('../util/Constants'); const Permissions = require('../util/Permissions'); const Util = require('../util/Util'); const RESTManager = require('./rest/RESTManager'); -const ClientDataManager = require('./ClientDataManager'); const ClientManager = require('./ClientManager'); const ClientDataResolver = require('./ClientDataResolver'); const ClientVoiceManager = require('./voice/ClientVoiceManager'); @@ -13,11 +12,13 @@ const Collection = require('../util/Collection'); const { Presence } = require('../structures/Presence'); const VoiceRegion = require('../structures/VoiceRegion'); const Webhook = require('../structures/Webhook'); -const User = require('../structures/User'); const Invite = require('../structures/Invite'); const ClientApplication = require('../structures/ClientApplication'); const ShardClientUtil = require('../sharding/ShardClientUtil'); const VoiceBroadcast = require('./voice/VoiceBroadcast'); +const UserStore = require('../stores/UserStore'); +const ChannelStore = require('../stores/ChannelStore'); +const GuildStore = require('../stores/GuildStore'); const { Error, TypeError, RangeError } = require('../errors'); /** @@ -49,13 +50,6 @@ class Client extends EventEmitter { */ this.rest = new RESTManager(this); - /** - * The data manager of the client - * @type {ClientDataManager} - * @private - */ - this.dataManager = new ClientDataManager(this); - /** * The manager of the client * @type {ClientManager} @@ -100,23 +94,23 @@ class Client extends EventEmitter { /** * All of the {@link User} objects that have been cached at any point, mapped by their IDs - * @type {Collection} + * @type {UserStore} */ - this.users = new Collection(); + this.users = new UserStore(this); /** * All of the guilds the client is currently handling, mapped by their IDs - * as long as sharding isn't being used, this will be *every* guild the bot is a member of - * @type {Collection} + * @type {GuildStore} */ - this.guilds = new Collection(); + this.guilds = new GuildStore(this); /** * All of the {@link Channel}s that the client is currently handling, mapped by their IDs - * as long as sharding isn't being used, this will be *every* channel in *every* guild, and all DM channels - * @type {Collection} + * @type {ChannelStore} */ - this.channels = new Collection(); + this.channels = new ChannelStore(this); /** * Presences that have been received for the client user's friends, mapped by user IDs @@ -322,20 +316,6 @@ class Client extends EventEmitter { }); } - /** - * Obtains a user from Discord, or the user cache if it's already available. - * This is only available when using a bot account. - * @param {Snowflake} id ID of the user - * @param {boolean} [cache=true] Whether to cache the new user object if it isn't already - * @returns {Promise} - */ - fetchUser(id, cache = true) { - if (this.users.has(id)) return Promise.resolve(this.users.get(id)); - return this.api.users(id).get().then(data => - cache ? this.dataManager.newUser(data) : new User(this, data) - ); - } - /** * Obtains an invite from Discord. * @param {InviteResolvable} invite Invite code or URL diff --git a/src/client/ClientDataManager.js b/src/client/ClientDataManager.js deleted file mode 100644 index 714fc30d4..000000000 --- a/src/client/ClientDataManager.js +++ /dev/null @@ -1,112 +0,0 @@ -const Constants = require('../util/Constants'); -const Util = require('../util/Util'); -const Guild = require('../structures/Guild'); -const User = require('../structures/User'); -const Channel = require('../structures/Channel'); -const GuildChannel = require('../structures/GuildChannel'); -const Emoji = require('../structures/Emoji'); - -class ClientDataManager { - constructor(client) { - this.client = client; - } - - get pastReady() { - return this.client.ws.connection.status === Constants.Status.READY; - } - - newGuild(data) { - const already = this.client.guilds.has(data.id); - const guild = new Guild(this.client, data); - this.client.guilds.set(guild.id, guild); - if (!this.client.user.bot && this.client.options.sync) this.client.syncGuilds([guild]); - if (this.pastReady && !already) { - /** - * Emitted whenever the client joins a guild. - * @event Client#guildCreate - * @param {Guild} guild The created guild - */ - if (this.client.options.fetchAllMembers) { - guild.fetchMembers().then(() => { this.client.emit(Constants.Events.GUILD_CREATE, guild); }); - } else { - this.client.emit(Constants.Events.GUILD_CREATE, guild); - } - } - - return guild; - } - - newUser(data) { - if (this.client.users.has(data.id)) return this.client.users.get(data.id); - const user = new User(this.client, data); - this.client.users.set(user.id, user); - return user; - } - - newChannel(data, guild) { - const already = this.client.channels.has(data.id); - const channel = Channel.create(this.client, data, guild); - - if (channel) { - if (this.pastReady && !already) this.client.emit(Constants.Events.CHANNEL_CREATE, channel); - this.client.channels.set(channel.id, channel); - return channel; - } - - return null; - } - - newEmoji(data, guild) { - const already = guild.emojis.has(data.id); - if (data && !already) { - let emoji = new Emoji(guild, data); - this.client.emit(Constants.Events.GUILD_EMOJI_CREATE, emoji); - guild.emojis.set(emoji.id, emoji); - return emoji; - } else if (already) { - return guild.emojis.get(data.id); - } - - return null; - } - - killEmoji(emoji) { - if (!(emoji instanceof Emoji && emoji.guild)) return; - this.client.emit(Constants.Events.GUILD_EMOJI_DELETE, emoji); - emoji.guild.emojis.delete(emoji.id); - } - - killGuild(guild) { - const already = this.client.guilds.has(guild.id); - this.client.guilds.delete(guild.id); - if (already && this.pastReady) this.client.emit(Constants.Events.GUILD_DELETE, guild); - } - - killUser(user) { - this.client.users.delete(user.id); - } - - killChannel(channel) { - this.client.channels.delete(channel.id); - if (channel instanceof GuildChannel) channel.guild.channels.delete(channel.id); - } - - updateGuild(currentGuild, newData) { - const oldGuild = Util.cloneObject(currentGuild); - currentGuild.setup(newData); - if (this.pastReady) this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild); - } - - updateChannel(currentChannel, newData) { - currentChannel.setup(newData); - } - - updateEmoji(currentEmoji, newData) { - const oldEmoji = Util.cloneObject(currentEmoji); - currentEmoji.setup(newData); - this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, oldEmoji, currentEmoji); - return currentEmoji; - } -} - -module.exports = ClientDataManager; diff --git a/src/client/actions/ChannelCreate.js b/src/client/actions/ChannelCreate.js index 83b1aa028..bb7a704cd 100644 --- a/src/client/actions/ChannelCreate.js +++ b/src/client/actions/ChannelCreate.js @@ -1,9 +1,14 @@ const Action = require('./Action'); +const Constants = require('../../util/Constants'); class ChannelCreateAction extends Action { handle(data) { const client = this.client; - const channel = client.dataManager.newChannel(data); + const existing = client.channels.has(data.id); + const channel = client.channels.create(data); + if (!existing && channel) { + client.emit(Constants.Events.CHANNEL_CREATE, channel); + } return { channel }; } } diff --git a/src/client/actions/ChannelDelete.js b/src/client/actions/ChannelDelete.js index 77732c3f2..cfc012cc6 100644 --- a/src/client/actions/ChannelDelete.js +++ b/src/client/actions/ChannelDelete.js @@ -1,4 +1,5 @@ const Action = require('./Action'); +const Constants = require('../../util/Constants'); class ChannelDeleteAction extends Action { constructor(client) { @@ -8,22 +9,21 @@ class ChannelDeleteAction extends Action { handle(data) { const client = this.client; - let channel = client.channels.get(data.id); + if (channel) { - client.dataManager.killChannel(channel); - this.deleted.set(channel.id, channel); - this.scheduleForDeletion(channel.id); - } else { - channel = this.deleted.get(data.id) || null; + client.channels.remove(channel.id); + client.emit(Constants.Events.CHANNEL_DELETE, channel); } return { channel }; } - - scheduleForDeletion(id) { - this.client.setTimeout(() => this.deleted.delete(id), this.client.options.restWsBridgeTimeout); - } } +/** + * Emitted whenever a channel is deleted. + * @event Client#channelDelete + * @param {Channel} channel The channel that was deleted + */ + module.exports = ChannelDeleteAction; diff --git a/src/client/actions/ChannelUpdate.js b/src/client/actions/ChannelUpdate.js index 43158b94c..ac2d4f69a 100644 --- a/src/client/actions/ChannelUpdate.js +++ b/src/client/actions/ChannelUpdate.js @@ -1,6 +1,4 @@ const Action = require('./Action'); -const Constants = require('../../util/Constants'); -const Util = require('../../util/Util'); class ChannelUpdateAction extends Action { handle(data) { @@ -8,27 +6,15 @@ class ChannelUpdateAction extends Action { const channel = client.channels.get(data.id); if (channel) { - const oldChannel = Util.cloneObject(channel); - channel.setup(data); - client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel); + const old = channel._update(data); return { - old: oldChannel, + old, updated: channel, }; } - return { - old: null, - updated: null, - }; + return {}; } } -/** - * Emitted whenever a channel is updated - e.g. name change, topic change. - * @event Client#channelUpdate - * @param {Channel} oldChannel The channel before the update - * @param {Channel} newChannel The channel after the update - */ - module.exports = ChannelUpdateAction; diff --git a/src/client/actions/GuildBanRemove.js b/src/client/actions/GuildBanRemove.js index 0276a523f..00ba04707 100644 --- a/src/client/actions/GuildBanRemove.js +++ b/src/client/actions/GuildBanRemove.js @@ -5,7 +5,7 @@ class GuildBanRemove extends Action { handle(data) { const client = this.client; const guild = client.guilds.get(data.guild_id); - const user = client.dataManager.newUser(data.user); + const user = client.users.create(data.user); if (guild && user) client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user); } } diff --git a/src/client/actions/GuildDelete.js b/src/client/actions/GuildDelete.js index 0151bf0d7..17ca38a41 100644 --- a/src/client/actions/GuildDelete.js +++ b/src/client/actions/GuildDelete.js @@ -29,7 +29,8 @@ class GuildDeleteAction extends Action { } // Delete guild - client.guilds.delete(guild.id); + client.guilds.remove(guild.id); + client.emit(Constants.Events.GUILD_DELETE, guild); this.deleted.set(guild.id, guild); this.scheduleForDeletion(guild.id); } else { diff --git a/src/client/actions/GuildEmojiCreate.js b/src/client/actions/GuildEmojiCreate.js index 79f55ba0a..4ae466d6a 100644 --- a/src/client/actions/GuildEmojiCreate.js +++ b/src/client/actions/GuildEmojiCreate.js @@ -1,9 +1,10 @@ const Action = require('./Action'); +const Constants = require('../../util/Constants'); class GuildEmojiCreateAction extends Action { handle(guild, createdEmoji) { - const client = this.client; - const emoji = client.dataManager.newEmoji(createdEmoji, guild); + const emoji = guild.emojis.create(createdEmoji); + this.client.emit(Constants.Events.GUILD_EMOJI_CREATE, emoji); return { emoji }; } } diff --git a/src/client/actions/GuildEmojiDelete.js b/src/client/actions/GuildEmojiDelete.js index 7f9aefba3..a6742d06f 100644 --- a/src/client/actions/GuildEmojiDelete.js +++ b/src/client/actions/GuildEmojiDelete.js @@ -1,9 +1,10 @@ const Action = require('./Action'); +const Constants = require('../../util/Constants'); class GuildEmojiDeleteAction extends Action { handle(emoji) { - const client = this.client; - client.dataManager.killEmoji(emoji); + emoji.guild.emojis.remove(emoji.id); + this.client.emit(Constants.Events.GUILD_EMOJI_DELETE, emoji); return { emoji }; } } diff --git a/src/client/actions/GuildEmojiUpdate.js b/src/client/actions/GuildEmojiUpdate.js index ff6ae65f9..fcd3985e5 100644 --- a/src/client/actions/GuildEmojiUpdate.js +++ b/src/client/actions/GuildEmojiUpdate.js @@ -1,9 +1,11 @@ const Action = require('./Action'); +const Constants = require('../../util/Constants'); class GuildEmojiUpdateAction extends Action { - handle(oldEmoji, newEmoji) { - const emoji = this.client.dataManager.updateEmoji(oldEmoji, newEmoji); - return { emoji }; + handle(current, data) { + const old = current._update(data); + this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, old, current); + return { emoji: current }; } } diff --git a/src/client/actions/GuildMemberGet.js b/src/client/actions/GuildMemberGet.js index ecb7a8f00..5bf2aafec 100644 --- a/src/client/actions/GuildMemberGet.js +++ b/src/client/actions/GuildMemberGet.js @@ -2,7 +2,7 @@ const Action = require('./Action'); class GuildMemberGetAction extends Action { handle(guild, data) { - const member = guild._addMember(data, false); + const member = guild.members.create(data); return { member }; } } diff --git a/src/client/actions/GuildMemberRemove.js b/src/client/actions/GuildMemberRemove.js index beadac34b..37eb268df 100644 --- a/src/client/actions/GuildMemberRemove.js +++ b/src/client/actions/GuildMemberRemove.js @@ -2,11 +2,6 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); class GuildMemberRemoveAction extends Action { - constructor(client) { - super(client); - this.deleted = new Map(); - } - handle(data) { const client = this.client; const guild = client.guilds.get(data.guild_id); @@ -15,20 +10,12 @@ class GuildMemberRemoveAction extends Action { member = guild.members.get(data.user.id); if (member) { guild.memberCount--; - guild._removeMember(member); - this.deleted.set(guild.id + data.user.id, member); + guild.members.remove(member.id); if (client.status === Constants.Status.READY) client.emit(Constants.Events.GUILD_MEMBER_REMOVE, member); - this.scheduleForDeletion(guild.id, data.user.id); - } else { - member = this.deleted.get(guild.id + data.user.id) || null; } } return { guild, member }; } - - scheduleForDeletion(guildID, userID) { - this.client.setTimeout(() => this.deleted.delete(guildID + userID), this.client.options.restWsBridgeTimeout); - } } /** diff --git a/src/client/actions/GuildRoleCreate.js b/src/client/actions/GuildRoleCreate.js index b4f320fa0..f39f78ce1 100644 --- a/src/client/actions/GuildRoleCreate.js +++ b/src/client/actions/GuildRoleCreate.js @@ -1,6 +1,5 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const Role = require('../../structures/Role'); class GuildRoleCreate extends Action { handle(data) { @@ -9,8 +8,7 @@ class GuildRoleCreate extends Action { let role; if (guild) { const already = guild.roles.has(data.role.id); - role = new Role(guild, data.role); - guild.roles.set(role.id, role); + role = guild.roles.create(data.role); if (!already) client.emit(Constants.Events.GUILD_ROLE_CREATE, role); } return { role }; diff --git a/src/client/actions/GuildRoleDelete.js b/src/client/actions/GuildRoleDelete.js index 8f032a161..3a95fa17e 100644 --- a/src/client/actions/GuildRoleDelete.js +++ b/src/client/actions/GuildRoleDelete.js @@ -2,11 +2,6 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); class GuildRoleDeleteAction extends Action { - constructor(client) { - super(client); - this.deleted = new Map(); - } - handle(data) { const client = this.client; const guild = client.guilds.get(data.guild_id); @@ -15,21 +10,13 @@ class GuildRoleDeleteAction extends Action { if (guild) { role = guild.roles.get(data.role_id); if (role) { - guild.roles.delete(data.role_id); - this.deleted.set(guild.id + data.role_id, role); - this.scheduleForDeletion(guild.id, data.role_id); + guild.roles.remove(data.role_id); client.emit(Constants.Events.GUILD_ROLE_DELETE, role); - } else { - role = this.deleted.get(guild.id + data.role_id) || null; } } return { role }; } - - scheduleForDeletion(guildID, roleID) { - this.client.setTimeout(() => this.deleted.delete(guildID + roleID), this.client.options.restWsBridgeTimeout); - } } /** diff --git a/src/client/actions/GuildRoleUpdate.js b/src/client/actions/GuildRoleUpdate.js index 7d127ab2c..c83d0e355 100644 --- a/src/client/actions/GuildRoleUpdate.js +++ b/src/client/actions/GuildRoleUpdate.js @@ -1,6 +1,5 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const Util = require('../../util/Util'); class GuildRoleUpdateAction extends Action { handle(data) { @@ -8,18 +7,16 @@ class GuildRoleUpdateAction extends Action { const guild = client.guilds.get(data.guild_id); if (guild) { - const roleData = data.role; - let oldRole = null; + let old = null; - const role = guild.roles.get(roleData.id); + const role = guild.roles.get(data.role.id); if (role) { - oldRole = Util.cloneObject(role); - role.setup(data.role); - client.emit(Constants.Events.GUILD_ROLE_UPDATE, oldRole, role); + old = role._update(data.role); + client.emit(Constants.Events.GUILD_ROLE_UPDATE, old, role); } return { - old: oldRole, + old, updated: role, }; } diff --git a/src/client/actions/GuildSync.js b/src/client/actions/GuildSync.js index 3d3a47b38..41e542027 100644 --- a/src/client/actions/GuildSync.js +++ b/src/client/actions/GuildSync.js @@ -14,9 +14,9 @@ class GuildSync extends Action { for (const syncMember of data.members) { const member = guild.members.get(syncMember.user.id); if (member) { - guild._updateMember(member, syncMember); + member._patch(syncMember); } else { - guild._addMember(syncMember, false); + guild.members.create(syncMember, false); } } } diff --git a/src/client/actions/GuildUpdate.js b/src/client/actions/GuildUpdate.js index 6806d1cfd..856e2e0ba 100644 --- a/src/client/actions/GuildUpdate.js +++ b/src/client/actions/GuildUpdate.js @@ -1,6 +1,5 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const Util = require('../../util/Util'); class GuildUpdateAction extends Action { handle(data) { @@ -8,11 +7,10 @@ class GuildUpdateAction extends Action { const guild = client.guilds.get(data.id); if (guild) { - const oldGuild = Util.cloneObject(guild); - guild.setup(data); - client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild); + const old = guild._update(data); + client.emit(Constants.Events.GUILD_UPDATE, old, guild); return { - old: oldGuild, + old, updated: guild, }; } diff --git a/src/client/actions/MessageCreate.js b/src/client/actions/MessageCreate.js index 542d5459d..7f4df687a 100644 --- a/src/client/actions/MessageCreate.js +++ b/src/client/actions/MessageCreate.js @@ -1,55 +1,39 @@ const Action = require('./Action'); -const Message = require('../../structures/Message'); +const Constants = require('../../util/Constants'); class MessageCreateAction extends Action { handle(data) { const client = this.client; - - const channel = client.channels.get((data instanceof Array ? data[0] : data).channel_id); - const user = client.users.get((data instanceof Array ? data[0] : data).author.id); + const channel = client.channels.get(data.channel_id); + const user = client.users.get(data.author.id); if (channel) { + const existing = channel.messages.get(data.id); + if (existing) return { message: existing }; const member = channel.guild ? channel.guild.member(user) : null; - if (data instanceof Array) { - const messages = new Array(data.length); - for (let i = 0; i < data.length; i++) { - messages[i] = channel._cacheMessage(new Message(channel, data[i], client)); - } - const lastMessage = messages[messages.length - 1]; - channel.lastMessageID = lastMessage.id; - channel.lastMessage = lastMessage; - if (user) { - user.lastMessageID = lastMessage.id; - user.lastMessage = lastMessage; - } - if (member) { - member.lastMessageID = lastMessage.id; - member.lastMessage = lastMessage; - } - return { - messages, - }; - } else { - const message = channel._cacheMessage(new Message(channel, data, client)); - channel.lastMessageID = data.id; - channel.lastMessage = message; - if (user) { - user.lastMessageID = data.id; - user.lastMessage = message; - } - if (member) { - member.lastMessageID = data.id; - member.lastMessage = message; - } - return { - message, - }; + const message = channel.messages.create(data); + channel.lastMessageID = data.id; + channel.lastMessage = message; + if (user) { + user.lastMessageID = data.id; + user.lastMessage = message; } + if (member) { + member.lastMessageID = data.id; + member.lastMessage = message; + } + + client.emit(Constants.Events.MESSAGE_CREATE, message); + return { message }; } - return { - message: null, - }; + return {}; } } +/** + * Emitted whenever a message is created. + * @event Client#message + * @param {Message} message The created message + */ + module.exports = MessageCreateAction; diff --git a/src/client/actions/MessageDelete.js b/src/client/actions/MessageDelete.js index be026d2d6..1b40ed1ec 100644 --- a/src/client/actions/MessageDelete.js +++ b/src/client/actions/MessageDelete.js @@ -1,11 +1,7 @@ const Action = require('./Action'); +const Constants = require('../../util/Constants'); class MessageDeleteAction extends Action { - constructor(client) { - super(client); - this.deleted = new Map(); - } - handle(data) { const client = this.client; const channel = client.channels.get(data.channel_id); @@ -15,20 +11,18 @@ class MessageDeleteAction extends Action { message = channel.messages.get(data.id); if (message) { channel.messages.delete(message.id); - this.deleted.set(channel.id + message.id, message); - this.scheduleForDeletion(channel.id, message.id); - } else { - message = this.deleted.get(channel.id + data.id) || null; + client.emit(Constants.Events.MESSAGE_DELETE, message); } } return { message }; } - - scheduleForDeletion(channelID, messageID) { - this.client.setTimeout(() => this.deleted.delete(channelID + messageID), - this.client.options.restWsBridgeTimeout); - } } +/** + * Emitted whenever a message is deleted. + * @event Client#messageDelete + * @param {Message} message The deleted message + */ + module.exports = MessageDeleteAction; diff --git a/src/client/actions/MessageDeleteBulk.js b/src/client/actions/MessageDeleteBulk.js index 6d207c2ed..05cd596e8 100644 --- a/src/client/actions/MessageDeleteBulk.js +++ b/src/client/actions/MessageDeleteBulk.js @@ -7,16 +7,25 @@ class MessageDeleteBulkAction extends Action { const client = this.client; const channel = client.channels.get(data.channel_id); - const ids = data.ids; - const messages = new Collection(); - for (const id of ids) { - const message = channel.messages.get(id); - if (message) messages.set(message.id, message); - } + if (channel) { + const ids = data.ids; + const messages = new Collection(); + for (const id of ids) { + const message = channel.messages.get(id); + if (message) messages.set(message.id, message); + } - if (messages.size > 0) client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages); - return { messages }; + if (messages.size > 0) client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages); + return { messages }; + } + return {}; } } +/** + * Emitted whenever messages are deleted in bulk. + * @event Client#messageDeleteBulk + * @param {Collection} messages The deleted messages, mapped by their ID + */ + module.exports = MessageDeleteBulkAction; diff --git a/src/client/actions/MessageReactionAdd.js b/src/client/actions/MessageReactionAdd.js index 84ab58de7..b26ad5949 100644 --- a/src/client/actions/MessageReactionAdd.js +++ b/src/client/actions/MessageReactionAdd.js @@ -1,5 +1,4 @@ const Action = require('./Action'); -const Constants = require('../../util/Constants'); /* { user_id: 'id', @@ -10,19 +9,22 @@ const Constants = require('../../util/Constants'); class MessageReactionAdd extends Action { handle(data) { - const user = this.client.users.get(data.user_id); + const user = data.user || this.client.users.get(data.user_id); if (!user) return false; // Verify channel - const channel = this.client.channels.get(data.channel_id); + const channel = data.channel || this.client.channels.get(data.channel_id); if (!channel || channel.type === 'voice') return false; // Verify message - const message = channel.messages.get(data.message_id); + const message = data.message || channel.messages.get(data.message_id); if (!message) return false; if (!data.emoji) return false; // Verify reaction - const reaction = message._addReaction(data.emoji, user); - if (reaction) this.client.emit(Constants.Events.MESSAGE_REACTION_ADD, reaction, user); - + const reaction = message.reactions.create({ + emoji: data.emoji, + count: 0, + me: user.id === this.client.user.id, + }); + reaction._add(user); return { message, reaction, user }; } } diff --git a/src/client/actions/MessageReactionRemove.js b/src/client/actions/MessageReactionRemove.js index d84c74cad..f83976d5c 100644 --- a/src/client/actions/MessageReactionRemove.js +++ b/src/client/actions/MessageReactionRemove.js @@ -20,8 +20,11 @@ class MessageReactionRemove extends Action { if (!message) return false; if (!data.emoji) return false; // Verify reaction - const reaction = message._removeReaction(data.emoji, user); - if (reaction) this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user); + const emojiID = data.emoji.id || decodeURIComponent(data.emoji.name); + const reaction = message.reactions.get(emojiID); + if (!reaction) return false; + reaction._remove(user); + this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user); return { message, reaction, user }; } diff --git a/src/client/actions/MessageReactionRemoveAll.js b/src/client/actions/MessageReactionRemoveAll.js index fa7b3e5fe..5ce961be7 100644 --- a/src/client/actions/MessageReactionRemoveAll.js +++ b/src/client/actions/MessageReactionRemoveAll.js @@ -9,7 +9,7 @@ class MessageReactionRemoveAll extends Action { const message = channel.messages.get(data.message_id); if (!message) return false; - message._clearReactions(); + message.reactions.clear(); this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE_ALL, message); return { message }; diff --git a/src/client/actions/MessageUpdate.js b/src/client/actions/MessageUpdate.js index 4e0392d72..4fbdce4e4 100644 --- a/src/client/actions/MessageUpdate.js +++ b/src/client/actions/MessageUpdate.js @@ -1,5 +1,4 @@ const Action = require('./Action'); -const Constants = require('../../util/Constants'); class MessageUpdateAction extends Action { handle(data) { @@ -10,31 +9,15 @@ class MessageUpdateAction extends Action { const message = channel.messages.get(data.id); if (message) { message.patch(data); - client.emit(Constants.Events.MESSAGE_UPDATE, message._edits[0], message); return { old: message._edits[0], updated: message, }; } - - return { - old: message, - updated: message, - }; } - return { - old: null, - updated: null, - }; + return {}; } } -/** - * Emitted whenever a message is updated - e.g. embed or content change. - * @event Client#messageUpdate - * @param {Message} oldMessage The message before the update - * @param {Message} newMessage The message after the update - */ - module.exports = MessageUpdateAction; diff --git a/src/client/actions/UserGet.js b/src/client/actions/UserGet.js index 0ee85aee3..4a135dd58 100644 --- a/src/client/actions/UserGet.js +++ b/src/client/actions/UserGet.js @@ -3,7 +3,7 @@ const Action = require('./Action'); class UserGetAction extends Action { handle(data) { const client = this.client; - const user = client.dataManager.newUser(data); + const user = client.users.create(data); return { user }; } } diff --git a/src/client/actions/UserUpdate.js b/src/client/actions/UserUpdate.js index 6b917d175..6c01c5293 100644 --- a/src/client/actions/UserUpdate.js +++ b/src/client/actions/UserUpdate.js @@ -1,6 +1,5 @@ const Action = require('./Action'); const Constants = require('../../util/Constants'); -const Util = require('../../util/Util'); class UserUpdateAction extends Action { handle(data) { @@ -14,8 +13,7 @@ class UserUpdateAction extends Action { }; } - const oldUser = Util.cloneObject(client.user); - client.user.patch(data); + const oldUser = client.user._update(data); client.emit(Constants.Events.USER_UPDATE, oldUser, client.user); return { old: oldUser, diff --git a/src/client/websocket/packets/handlers/ChannelCreate.js b/src/client/websocket/packets/handlers/ChannelCreate.js index 04cb2985a..57c235197 100644 --- a/src/client/websocket/packets/handlers/ChannelCreate.js +++ b/src/client/websocket/packets/handlers/ChannelCreate.js @@ -2,9 +2,7 @@ const AbstractHandler = require('./AbstractHandler'); class ChannelCreateHandler extends AbstractHandler { handle(packet) { - const client = this.packetManager.client; - const data = packet.d; - client.actions.ChannelCreate.handle(data); + this.packetManager.client.actions.ChannelCreate.handle(packet.d); } } diff --git a/src/client/websocket/packets/handlers/ChannelDelete.js b/src/client/websocket/packets/handlers/ChannelDelete.js index b25f585df..68eb9a903 100644 --- a/src/client/websocket/packets/handlers/ChannelDelete.js +++ b/src/client/websocket/packets/handlers/ChannelDelete.js @@ -1,20 +1,9 @@ const AbstractHandler = require('./AbstractHandler'); -const Constants = require('../../../../util/Constants'); - class ChannelDeleteHandler extends AbstractHandler { handle(packet) { - const client = this.packetManager.client; - const data = packet.d; - const response = client.actions.ChannelDelete.handle(data); - if (response.channel) client.emit(Constants.Events.CHANNEL_DELETE, response.channel); + this.packetManager.client.actions.ChannelDelete.handle(packet.d); } } -/** - * Emitted whenever a channel is deleted. - * @event Client#channelDelete - * @param {Channel} channel The channel that was deleted - */ - module.exports = ChannelDeleteHandler; diff --git a/src/client/websocket/packets/handlers/ChannelUpdate.js b/src/client/websocket/packets/handlers/ChannelUpdate.js index fa535b143..b57d3f304 100644 --- a/src/client/websocket/packets/handlers/ChannelUpdate.js +++ b/src/client/websocket/packets/handlers/ChannelUpdate.js @@ -1,11 +1,20 @@ const AbstractHandler = require('./AbstractHandler'); +const Constants = require('../../../../util/Constants'); class ChannelUpdateHandler extends AbstractHandler { handle(packet) { - const client = this.packetManager.client; - const data = packet.d; - client.actions.ChannelUpdate.handle(data); + const { old, updated } = this.packetManager.client.actions.ChannelUpdate.handle(packet.d); + if (old && updated) { + this.packetManager.client.emit(Constants.Events.CHANNEL_UPDATE, old, updated); + } } } module.exports = ChannelUpdateHandler; + +/** + * Emitted whenever a channel is updated - e.g. name change, topic change. + * @event Client#channelUpdate + * @param {Channel} oldChannel The channel before the update + * @param {Channel} newChannel The channel after the update + */ diff --git a/src/client/websocket/packets/handlers/GuildCreate.js b/src/client/websocket/packets/handlers/GuildCreate.js index d7c180377..6fe88d5a0 100644 --- a/src/client/websocket/packets/handlers/GuildCreate.js +++ b/src/client/websocket/packets/handlers/GuildCreate.js @@ -1,20 +1,31 @@ const AbstractHandler = require('./AbstractHandler'); +const Constants = require('../../../../util/Constants'); class GuildCreateHandler extends AbstractHandler { - handle(packet) { + async handle(packet) { const client = this.packetManager.client; const data = packet.d; - const guild = client.guilds.get(data.id); + let guild = client.guilds.get(data.id); if (guild) { if (!guild.available && !data.unavailable) { // A newly available guild - guild.setup(data); + guild._patch(data); this.packetManager.ws.checkIfReady(); } } else { // A new guild - client.dataManager.newGuild(data); + guild = client.guilds.create(data); + const emitEvent = client.ws.connection.status === Constants.Status.READY; + if (emitEvent) { + /** + * Emitted whenever the client joins a guild. + * @event Client#guildCreate + * @param {Guild} guild The created guild + */ + if (client.options.fetchAllMembers) await guild.fetchMembers(); + client.emit(Constants.Events.GUILD_CREATE, guild); + } } } } diff --git a/src/client/websocket/packets/handlers/GuildDelete.js b/src/client/websocket/packets/handlers/GuildDelete.js index 35e3c53d8..138832fec 100644 --- a/src/client/websocket/packets/handlers/GuildDelete.js +++ b/src/client/websocket/packets/handlers/GuildDelete.js @@ -1,12 +1,9 @@ const AbstractHandler = require('./AbstractHandler'); -const Constants = require('../../../../util/Constants'); class GuildDeleteHandler extends AbstractHandler { handle(packet) { const client = this.packetManager.client; - const data = packet.d; - const response = client.actions.GuildDelete.handle(data); - if (response.guild) client.emit(Constants.Events.GUILD_DELETE, response.guild); + client.actions.GuildDelete.handle(packet.d); } } diff --git a/src/client/websocket/packets/handlers/GuildMemberAdd.js b/src/client/websocket/packets/handlers/GuildMemberAdd.js index d4d122f70..2b4a60487 100644 --- a/src/client/websocket/packets/handlers/GuildMemberAdd.js +++ b/src/client/websocket/packets/handlers/GuildMemberAdd.js @@ -1,6 +1,7 @@ // ##untested handler## const AbstractHandler = require('./AbstractHandler'); +const Constants = require('../../../../util/Constants'); class GuildMemberAddHandler extends AbstractHandler { handle(packet) { @@ -9,9 +10,18 @@ class GuildMemberAddHandler extends AbstractHandler { const guild = client.guilds.get(data.guild_id); if (guild) { guild.memberCount++; - guild._addMember(data); + const member = guild.members.create(data); + if (client.ws.connection.status === Constants.Status.READY) { + client.emit(Constants.Events.GUILD_MEMBER_ADD, member); + } } } } module.exports = GuildMemberAddHandler; + +/** + * Emitted whenever a user joins a guild. + * @event Client#guildMemberAdd + * @param {GuildMember} member The member that has joined a guild + */ diff --git a/src/client/websocket/packets/handlers/GuildMemberUpdate.js b/src/client/websocket/packets/handlers/GuildMemberUpdate.js index 94ac71f47..ae3f3812b 100644 --- a/src/client/websocket/packets/handlers/GuildMemberUpdate.js +++ b/src/client/websocket/packets/handlers/GuildMemberUpdate.js @@ -1,16 +1,27 @@ // ##untested handler## const AbstractHandler = require('./AbstractHandler'); +const Constants = require('../../../../util/Constants'); class GuildMemberUpdateHandler extends AbstractHandler { handle(packet) { const client = this.packetManager.client; const data = packet.d; - const guild = client.guilds.get(data.guild_id); if (guild) { const member = guild.members.get(data.user.id); - if (member) guild._updateMember(member, data); + if (member) { + const old = member._update(data); + if (client.ws.connection.status === Constants.Status.READY) { + /** + * Emitted whenever a guild member changes - i.e. new role, removed role, nickname. + * @event Client#guildMemberUpdate + * @param {GuildMember} oldMember The member before the update + * @param {GuildMember} newMember The member after the update + */ + client.emit(Constants.Events.GUILD_MEMBER_UPDATE, old, member); + } + } } } } diff --git a/src/client/websocket/packets/handlers/GuildMembersChunk.js b/src/client/websocket/packets/handlers/GuildMembersChunk.js index 51951b28c..62db885c0 100644 --- a/src/client/websocket/packets/handlers/GuildMembersChunk.js +++ b/src/client/websocket/packets/handlers/GuildMembersChunk.js @@ -10,7 +10,7 @@ class GuildMembersChunkHandler extends AbstractHandler { if (!guild) return; const members = new Collection(); - for (const member of data.members) members.set(member.user.id, guild._addMember(member, false)); + for (const member of data.members) members.set(member.user.id, guild.members.create(member)); client.emit(Constants.Events.GUILD_MEMBERS_CHUNK, members, guild); diff --git a/src/client/websocket/packets/handlers/MessageCreate.js b/src/client/websocket/packets/handlers/MessageCreate.js index beed34df8..31f5f2806 100644 --- a/src/client/websocket/packets/handlers/MessageCreate.js +++ b/src/client/websocket/packets/handlers/MessageCreate.js @@ -1,19 +1,9 @@ const AbstractHandler = require('./AbstractHandler'); -const Constants = require('../../../../util/Constants'); class MessageCreateHandler extends AbstractHandler { handle(packet) { - const client = this.packetManager.client; - const data = packet.d; - const response = client.actions.MessageCreate.handle(data); - if (response.message) client.emit(Constants.Events.MESSAGE_CREATE, response.message); + this.packetManager.client.actions.MessageCreate.handle(packet.d); } } -/** - * Emitted whenever a message is created. - * @event Client#message - * @param {Message} message The created message - */ - module.exports = MessageCreateHandler; diff --git a/src/client/websocket/packets/handlers/MessageDelete.js b/src/client/websocket/packets/handlers/MessageDelete.js index 043c70a5a..6c9c03168 100644 --- a/src/client/websocket/packets/handlers/MessageDelete.js +++ b/src/client/websocket/packets/handlers/MessageDelete.js @@ -10,10 +10,4 @@ class MessageDeleteHandler extends AbstractHandler { } } -/** - * Emitted whenever a message is deleted. - * @event Client#messageDelete - * @param {Message} message The deleted message - */ - module.exports = MessageDeleteHandler; diff --git a/src/client/websocket/packets/handlers/MessageDeleteBulk.js b/src/client/websocket/packets/handlers/MessageDeleteBulk.js index db02df0f7..0077dbf81 100644 --- a/src/client/websocket/packets/handlers/MessageDeleteBulk.js +++ b/src/client/websocket/packets/handlers/MessageDeleteBulk.js @@ -2,16 +2,8 @@ const AbstractHandler = require('./AbstractHandler'); class MessageDeleteBulkHandler extends AbstractHandler { handle(packet) { - const client = this.packetManager.client; - const data = packet.d; - client.actions.MessageDeleteBulk.handle(data); + this.packetManager.client.actions.MessageDeleteBulk.handle(packet.d); } } -/** - * Emitted whenever messages are deleted in bulk. - * @event Client#messageDeleteBulk - * @param {Collection} messages The deleted messages, mapped by their ID - */ - module.exports = MessageDeleteBulkHandler; diff --git a/src/client/websocket/packets/handlers/MessageReactionAdd.js b/src/client/websocket/packets/handlers/MessageReactionAdd.js index a58db7043..1b9c5bd12 100644 --- a/src/client/websocket/packets/handlers/MessageReactionAdd.js +++ b/src/client/websocket/packets/handlers/MessageReactionAdd.js @@ -1,10 +1,12 @@ const AbstractHandler = require('./AbstractHandler'); +const Constants = require('../../../../util/Constants'); class MessageReactionAddHandler extends AbstractHandler { handle(packet) { const client = this.packetManager.client; const data = packet.d; - client.actions.MessageReactionAdd.handle(data); + const { user, reaction } = client.actions.MessageReactionAdd.handle(data); + if (reaction) client.emit(Constants.Events.MESSAGE_REACTION_ADD, reaction, user); } } diff --git a/src/client/websocket/packets/handlers/MessageUpdate.js b/src/client/websocket/packets/handlers/MessageUpdate.js index 527632d4e..0439d8f7d 100644 --- a/src/client/websocket/packets/handlers/MessageUpdate.js +++ b/src/client/websocket/packets/handlers/MessageUpdate.js @@ -1,10 +1,12 @@ const AbstractHandler = require('./AbstractHandler'); +const Constants = require('../../../../util/Constants'); class MessageUpdateHandler extends AbstractHandler { handle(packet) { - const client = this.packetManager.client; - const data = packet.d; - client.actions.MessageUpdate.handle(data); + const { old, updated } = this.packetManager.client.actions.MessageUpdate.handle(packet.d); + if (old && updated) { + this.packetManager.client.emit(Constants.Events.MESSAGE_UPDATE, old, updated); + } } } diff --git a/src/client/websocket/packets/handlers/PresenceUpdate.js b/src/client/websocket/packets/handlers/PresenceUpdate.js index 8bcf6596c..b5d04c642 100644 --- a/src/client/websocket/packets/handlers/PresenceUpdate.js +++ b/src/client/websocket/packets/handlers/PresenceUpdate.js @@ -12,14 +12,13 @@ class PresenceUpdateHandler extends AbstractHandler { // Step 1 if (!user) { if (data.user.username) { - user = client.dataManager.newUser(data.user); + user = client.users.create(data.user); } else { return; } } - const oldUser = Util.cloneObject(user); - user.patch(data.user); + const oldUser = user._update(data.user); if (!user.equals(oldUser)) { client.emit(Constants.Events.USER_UPDATE, oldUser, user); } @@ -27,12 +26,12 @@ class PresenceUpdateHandler extends AbstractHandler { if (guild) { let member = guild.members.get(user.id); if (!member && data.status !== 'offline') { - member = guild._addMember({ + member = guild.members.create({ user, roles: data.roles, deaf: false, mute: false, - }, false); + }); client.emit(Constants.Events.GUILD_MEMBER_AVAILABLE, member); } if (member) { @@ -40,7 +39,7 @@ class PresenceUpdateHandler extends AbstractHandler { guild._setPresence(user.id, data); return; } - const oldMember = Util.cloneObject(member); + const oldMember = member._clone(); if (member.presence) { oldMember.frozenPresence = Util.cloneObject(member.presence); } diff --git a/src/client/websocket/packets/handlers/Ready.js b/src/client/websocket/packets/handlers/Ready.js index 29745c42c..d2ac42b78 100644 --- a/src/client/websocket/packets/handlers/Ready.js +++ b/src/client/websocket/packets/handlers/Ready.js @@ -17,11 +17,11 @@ class ReadyHandler extends AbstractHandler { client.readyAt = new Date(); client.users.set(clientUser.id, clientUser); - for (const guild of data.guilds) client.dataManager.newGuild(guild); - for (const privateDM of data.private_channels) client.dataManager.newChannel(privateDM); + for (const guild of data.guilds) client.guilds.create(guild); + for (const privateDM of data.private_channels) client.channels.create(privateDM); for (const relation of data.relationships) { - const user = client.dataManager.newUser(relation.user); + const user = client.users.create(relation.user); if (relation.type === 1) { client.user.friends.set(user.id, user); } else if (relation.type === 2) { @@ -31,7 +31,7 @@ class ReadyHandler extends AbstractHandler { data.presences = data.presences || []; for (const presence of data.presences) { - client.dataManager.newUser(presence.user); + client.users.create(presence.user); client._setPresence(presence.user.id, presence); } @@ -45,7 +45,7 @@ class ReadyHandler extends AbstractHandler { } if (!client.users.has('1')) { - client.dataManager.newUser({ + client.users.create({ id: '1', username: 'Clyde', discriminator: '0000', diff --git a/src/client/websocket/packets/handlers/VoiceStateUpdate.js b/src/client/websocket/packets/handlers/VoiceStateUpdate.js index d0369229d..65c77b253 100644 --- a/src/client/websocket/packets/handlers/VoiceStateUpdate.js +++ b/src/client/websocket/packets/handlers/VoiceStateUpdate.js @@ -1,7 +1,6 @@ const AbstractHandler = require('./AbstractHandler'); const Constants = require('../../../../util/Constants'); -const Util = require('../../../../util/Util'); class VoiceStateUpdateHandler extends AbstractHandler { handle(packet) { @@ -12,28 +11,16 @@ class VoiceStateUpdateHandler extends AbstractHandler { if (guild) { const member = guild.members.get(data.user_id); if (member) { - const oldVoiceChannelMember = Util.cloneObject(member); - if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) { - member.voiceChannel.members.delete(oldVoiceChannelMember.id); - } - - // If the member left the voice channel, unset their speaking property - if (!data.channel_id) member.speaking = null; + const oldMember = member._clone(); + oldMember._frozenVoiceState = oldMember.voiceState; if (member.user.id === client.user.id && data.channel_id) { client.emit('self.voiceStateUpdate', data); } - const newChannel = client.channels.get(data.channel_id); - if (newChannel) newChannel.members.set(member.user.id, member); + guild.voiceStates.set(member.user.id, data); - member.serverMute = data.mute; - member.serverDeaf = data.deaf; - member.selfMute = data.self_mute; - member.selfDeaf = data.self_deaf; - member.voiceSessionID = data.session_id; - member.voiceChannelID = data.channel_id; - client.emit(Constants.Events.VOICE_STATE_UPDATE, oldVoiceChannelMember, member); + client.emit(Constants.Events.VOICE_STATE_UPDATE, oldMember, member); } } } diff --git a/src/stores/ChannelStore.js b/src/stores/ChannelStore.js new file mode 100644 index 000000000..cffa642bb --- /dev/null +++ b/src/stores/ChannelStore.js @@ -0,0 +1,46 @@ +const DataStore = require('./DataStore'); +const DMChannel = require('../structures/DMChannel'); +const GroupDMChannel = require('../structures/GroupDMChannel'); +const Constants = require('../util/Constants'); + +/** + * Stores channels. + * @private + * @extends {DataStore} + */ +class ChannelStore extends DataStore { + create(data, guild, cache = true) { + const existing = this.get(data.id); + if (existing) return existing; + + let channel; + switch (data.type) { + case Constants.ChannelTypes.DM: + channel = new DMChannel(this.client, data); + break; + case Constants.ChannelTypes.GROUP: + channel = new GroupDMChannel(this.client, data); + break; + default: // eslint-disable-line no-case-declarations + guild = guild || this.client.guilds.get(data.guild_id); + if (!guild) { + this.client.emit(Constants.Events.DEBUG, `Failed to find guild for channel ${data.id} ${data.type}`); + return null; + } + channel = guild.channels.create(data, cache); + break; + } + + if (cache) this.set(channel.id, channel); + + return channel; + } + + remove(id) { + const channel = this.get(id); + if (channel.guild) channel.guild.channels.remove(id); + super.remove(id); + } +} + +module.exports = ChannelStore; diff --git a/src/stores/DataStore.js b/src/stores/DataStore.js new file mode 100644 index 000000000..cb1e84ecc --- /dev/null +++ b/src/stores/DataStore.js @@ -0,0 +1,19 @@ +const Collection = require('../util/Collection'); + +/** + * Manages the creation, retrieval and deletion of a specific data model. + * @extends {Collection} + */ +class DataStore extends Collection { + constructor(client, iterable) { + super(); + if (iterable) for (const item of iterable) this.create(item); + Object.defineProperty(this, 'client', { value: client }); + } + + // Stubs + create() { return undefined; } + remove(key) { return this.delete(key); } +} + +module.exports = DataStore; diff --git a/src/stores/EmojiStore.js b/src/stores/EmojiStore.js new file mode 100644 index 000000000..e548760a6 --- /dev/null +++ b/src/stores/EmojiStore.js @@ -0,0 +1,27 @@ +const DataStore = require('./DataStore'); +const Emoji = require('../structures/Emoji'); +/** + * Stores emojis. + * @private + * @extends {DataStore} + */ +class EmojiStore extends DataStore { + constructor(guild, iterable) { + super(guild.client, iterable); + this.guild = guild; + } + + create(data) { + const guild = this.guild; + + const existing = guild.emojis.get(data.id); + if (existing) return existing; + + const emoji = new Emoji(guild, data); + guild.emojis.set(emoji.id, emoji); + + return emoji; + } +} + +module.exports = EmojiStore; diff --git a/src/stores/GuildChannelStore.js b/src/stores/GuildChannelStore.js new file mode 100644 index 000000000..e59f46676 --- /dev/null +++ b/src/stores/GuildChannelStore.js @@ -0,0 +1,28 @@ +const DataStore = require('./DataStore'); +const TextChannel = require('../structures/TextChannel'); +const VoiceChannel = require('../structures/VoiceChannel'); +const Constants = require('../util/Constants'); +/** + * Stores guild channels. + * @private + * @extends {DataStore} + */ +class GuildChannelStore extends DataStore { + constructor(guild, iterable) { + super(guild.client, iterable); + this.guild = guild; + } + + create(data, cache = true) { + const existing = this.get(data.id); + if (existing) return existing; + + const ChannelModel = data.type === Constants.ChannelTypes.TEXT ? TextChannel : VoiceChannel; + const channel = new ChannelModel(this.guild, data); + if (cache) this.set(channel.id, channel); + + return channel; + } +} + +module.exports = GuildChannelStore; diff --git a/src/stores/GuildMemberStore.js b/src/stores/GuildMemberStore.js new file mode 100644 index 000000000..f314eb946 --- /dev/null +++ b/src/stores/GuildMemberStore.js @@ -0,0 +1,25 @@ +const DataStore = require('./DataStore'); +const GuildMember = require('../structures/GuildMember'); +/** + * Stores guild members. + * @private + * @extends {DataStore} + */ +class GuildMemberStore extends DataStore { + constructor(guild, iterable) { + super(guild.client, iterable); + this.guild = guild; + } + + create(data) { + const existing = this.has(data.user.id); + if (existing) return existing; + + const member = new GuildMember(this.guild, data); + this.set(member.id, member); + + return member; + } +} + +module.exports = GuildMemberStore; diff --git a/src/stores/GuildStore.js b/src/stores/GuildStore.js new file mode 100644 index 000000000..7b05403f7 --- /dev/null +++ b/src/stores/GuildStore.js @@ -0,0 +1,20 @@ +const DataStore = require('./DataStore'); +const Guild = require('../structures/Guild'); +/** + * Stores guilds. + * @private + * @extends {DataStore} + */ +class GuildStore extends DataStore { + create(data) { + const existing = this.get(data.id); + if (existing) return existing; + + const guild = new Guild(this.client, data); + this.set(guild.id, guild); + + return guild; + } +} + +module.exports = GuildStore; diff --git a/src/stores/MessageStore.js b/src/stores/MessageStore.js new file mode 100644 index 000000000..7ae45779a --- /dev/null +++ b/src/stores/MessageStore.js @@ -0,0 +1,97 @@ +const DataStore = require('./DataStore'); +const Collection = require('../util/Collection'); +let Message; + +/** + * Stores messages for text-based channels. + * @extends {DataStore} + */ +class MessageStore extends DataStore { + constructor(channel, iterable) { + super(channel.client, iterable); + this.channel = channel; + Message = require('../structures/Message'); + } + + create(data, cache = true) { + const existing = this.get(data.id); + if (existing) return existing; + + const message = new Message(this.client.channels.get(data.channel_id), data, this.client); + + if (cache) this.set(message.id, message); + return message; + } + + set(key, value) { + const maxSize = this.client.options.messageCacheMaxSize; + if (maxSize === 0) return; + if (this.size >= maxSize && maxSize > 0) this.delete(this.firstKey()); + super.set(key, value); + } + + /** + * The parameters to pass in when requesting previous messages from a channel. `around`, `before` and + * `after` are mutually exclusive. All the parameters are optional. + * @typedef {Object} ChannelLogsQueryOptions + * @property {number} [limit=50] Number of messages to acquire + * @property {Snowflake} [before] ID of a message to get the messages that were posted before it + * @property {Snowflake} [after] ID of a message to get the messages that were posted after it + * @property {Snowflake} [around] ID of a message to get the messages that were posted around it + */ + + /** + * Gets a message, or messages, from this channel. + * @param {Snowflake|ChannelLogsQueryOptions} [message] The ID of the message to fetch, or query parameters. + * @returns {Promise|Promise>} + * @example + * // Get message + * channel.messages.fetch('99539446449315840') + * .then(message => console.log(message.content)) + * .catch(console.error); + * @example + * // Get messages + * channel.messages.fetch({limit: 10}) + * .then(messages => console.log(`Received ${messages.size} messages`)) + * .catch(console.error); + */ + fetch(message) { + return typeof message === 'string' ? this._fetchId(message) : this._fetchMany(message); + } + + /** + * Fetches the pinned messages of this channel and returns a collection of them. + * @returns {Promise>} + */ + fetchPinned() { + return this.client.api.channels[this.message.channel.id].pins.get().then(data => { + const messages = new Collection(); + for (const message of data) messages.set(message.id, this.create(message)); + return messages; + }); + } + + _fetchId(messageID) { + if (!this.client.user.bot) { + return this._fetchMany({ limit: 1, around: messageID }) + .then(messages => { + const msg = messages.get(messageID); + if (!msg) throw new Error('MESSAGE_MISSING'); + return msg; + }); + } + return this.client.api.channels[this.channel.id].messages[messageID].get() + .then(data => this.create(data)); + } + + _fetchMany(options = {}) { + return this.client.api.channels[this.channel.id].messages.get({ query: options }) + .then(data => { + const messages = new Collection(); + for (const message of data) messages.set(message.id, this.create(message)); + return messages; + }); + } +} + +module.exports = MessageStore; diff --git a/src/stores/ReactionStore.js b/src/stores/ReactionStore.js new file mode 100644 index 000000000..b94aae619 --- /dev/null +++ b/src/stores/ReactionStore.js @@ -0,0 +1,27 @@ +const DataStore = require('./DataStore'); +const MessageReaction = require('../structures/MessageReaction'); +/** + * Stores reactions. + * @private + * @extends {DataStore} + */ +class ReactionStore extends DataStore { + constructor(message, iterable) { + super(message.client, iterable); + this.message = message; + } + + create(data) { + const emojiID = data.emoji.id || decodeURIComponent(data.emoji.name); + + const existing = this.get(data.id); + if (existing) return existing; + + const reaction = new MessageReaction(this.message, data.emoji, data.count, data.me); + this.set(emojiID, reaction); + + return reaction; + } +} + +module.exports = ReactionStore; diff --git a/src/stores/RoleStore.js b/src/stores/RoleStore.js new file mode 100644 index 000000000..068ad5ec3 --- /dev/null +++ b/src/stores/RoleStore.js @@ -0,0 +1,25 @@ +const DataStore = require('./DataStore'); +const Role = require('../structures/Role'); +/** + * Stores roles. + * @private + * @extends {DataStore} + */ +class RoleStore extends DataStore { + constructor(guild, iterable) { + super(guild.client, iterable); + this.guild = guild; + } + + create(data) { + const existing = this.get(data.id); + if (existing) return existing; + + const role = new Role(this.guild, data); + this.set(role.id, role); + + return role; + } +} + +module.exports = RoleStore; diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js new file mode 100644 index 000000000..c393145b8 --- /dev/null +++ b/src/stores/UserStore.js @@ -0,0 +1,35 @@ +const DataStore = require('./DataStore'); +const User = require('../structures/User'); + +/** + * A data store to store User models. + * @extends {DataStore} + */ +class UserStore extends DataStore { + create(data) { + const existing = this.get(data.id); + if (existing) return existing; + + const user = new User(this.client, data); + this.set(user.id, user); + return user; + } + + /** + * Obtains a user from Discord, or the user cache if it's already available. + * This is only available when using a bot account. + * @param {Snowflake} id ID of the user + * @param {boolean} [cache=true] Whether to cache the new user object if it isn't already + * @returns {Promise} + */ + fetch(id, cache = true) { + const existing = this.get(id); + if (existing) return Promise.resolve(existing); + + return this.client.api.users(id).get().then(data => + cache ? this.create(data) : new User(this.client, data) + ); + } +} + +module.exports = UserStore; diff --git a/src/structures/Base.js b/src/structures/Base.js new file mode 100644 index 000000000..64e4a8560 --- /dev/null +++ b/src/structures/Base.js @@ -0,0 +1,28 @@ +/** + * Represents a data model that is identifiable by a Snowflake (i.e. Discord API data models). + */ +class Base { + constructor(client) { + /** + * The client that instantiated this + * @name Base#client + * @type {Client} + * @readonly + */ + Object.defineProperty(this, 'client', { value: client }); + } + + _clone() { + return Object.assign(Object.create(this), this); + } + + _patch(data) { return data; } + + _update(data) { + const clone = this._clone(); + this._patch(data); + return clone; + } +} + +module.exports = Base; diff --git a/src/structures/Channel.js b/src/structures/Channel.js index acf94e298..f70ec5bd8 100644 --- a/src/structures/Channel.js +++ b/src/structures/Channel.js @@ -1,18 +1,14 @@ const Snowflake = require('../util/Snowflake'); +const Base = require('./Base'); const Constants = require('../util/Constants'); /** * Represents any channel on Discord. + * @extends {Base} */ -class Channel { +class Channel extends Base { constructor(client, data) { - /** - * The client that instantiated the Channel - * @name Channel#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: client }); + super(client); const type = Object.keys(Constants.ChannelTypes)[data.type]; /** @@ -26,10 +22,10 @@ class Channel { */ this.type = type ? type.toLowerCase() : 'unknown'; - if (data) this.setup(data); + if (data) this._patch(data); } - setup(data) { + _patch(data) { /** * The unique ID of the channel * @type {Snowflake} diff --git a/src/structures/ClientApplication.js b/src/structures/ClientApplication.js index df0d310e8..c7aa081bd 100644 --- a/src/structures/ClientApplication.js +++ b/src/structures/ClientApplication.js @@ -1,23 +1,18 @@ const Snowflake = require('../util/Snowflake'); const Constants = require('../util/Constants'); +const Base = require('./Base'); /** * Represents a Client OAuth2 Application. + * @extends {Base} */ -class ClientApplication { +class ClientApplication extends Base { constructor(client, data) { - /** - * The client that instantiated the application - * @name ClientApplication#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: client }); - - this.setup(data); + super(client); + this._patch(data); } - setup(data) { + _patch(data) { /** * The ID of the app * @type {Snowflake} @@ -101,7 +96,7 @@ class ClientApplication { * The owner of this OAuth application * @type {?User} */ - this.owner = this.client.dataManager.newUser(data.owner); + this.owner = this.client.users.create(data.owner); } } diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index 79d448f84..b6a4a7912 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -14,8 +14,8 @@ const { TypeError } = require('../errors'); * @extends {User} */ class ClientUser extends User { - setup(data) { - super.setup(data); + _patch(data) { + super._patch(data); /** * Whether or not this account has been verified @@ -82,8 +82,8 @@ class ClientUser extends User { /** * All of the user's guild settings - * @type {Collection} * This is only filled when using a user account. + * @type {Collection} */ this.guildSettings = new Collection(); if (data.user_guild_settings) { @@ -341,7 +341,7 @@ class ClientUser extends User { const timeout = this.client.setTimeout(() => { this.client.removeListener(Constants.Events.GUILD_CREATE, handleGuild); - resolve(this.client.dataManager.newGuild(data)); + resolve(this.client.guilds.create(data)); }, 10000); return undefined; }, reject) diff --git a/src/structures/DMChannel.js b/src/structures/DMChannel.js index 91d8fca1d..c1d6ee3a9 100644 --- a/src/structures/DMChannel.js +++ b/src/structures/DMChannel.js @@ -1,6 +1,6 @@ const Channel = require('./Channel'); const TextBasedChannel = require('./interfaces/TextBasedChannel'); -const Collection = require('../util/Collection'); +const MessageStore = require('../stores/MessageStore'); /** * Represents a direct message channel between two users. @@ -10,18 +10,18 @@ const Collection = require('../util/Collection'); class DMChannel extends Channel { constructor(client, data) { super(client, data); - this.messages = new Collection(); + this.messages = new MessageStore(this); this._typing = new Map(); } - setup(data) { - super.setup(data); + _patch(data) { + super._patch(data); /** * The recipient on the other end of the DM * @type {User} */ - this.recipient = this.client.dataManager.newUser(data.recipients[0]); + this.recipient = this.client.users.create(data.recipients[0]); this.lastMessageID = data.last_message_id; } diff --git a/src/structures/Emoji.js b/src/structures/Emoji.js index a37c0162a..3c0f647cc 100644 --- a/src/structures/Emoji.js +++ b/src/structures/Emoji.js @@ -1,19 +1,15 @@ const Constants = require('../util/Constants'); const Collection = require('../util/Collection'); const Snowflake = require('../util/Snowflake'); +const Base = require('./Base'); /** * Represents a custom emoji. + * @extends {Base} */ -class Emoji { +class Emoji extends Base { constructor(guild, data) { - /** - * The client that instantiated this object - * @name Emoji#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: guild.client }); + super(guild.client); /** * The guild this emoji is part of @@ -21,10 +17,10 @@ class Emoji { */ this.guild = guild; - this.setup(data); + this._patch(data); } - setup(data) { + _patch(data) { /** * The ID of the emoji * @type {Snowflake} diff --git a/src/structures/GroupDMChannel.js b/src/structures/GroupDMChannel.js index 78e22f731..6c05ac09b 100644 --- a/src/structures/GroupDMChannel.js +++ b/src/structures/GroupDMChannel.js @@ -1,6 +1,7 @@ const Channel = require('./Channel'); const TextBasedChannel = require('./interfaces/TextBasedChannel'); const Collection = require('../util/Collection'); +const MessageStore = require('../stores/MessageStore'); const Constants = require('../util/Constants'); /* @@ -33,12 +34,12 @@ const Constants = require('../util/Constants'); class GroupDMChannel extends Channel { constructor(client, data) { super(client, data); - this.messages = new Collection(); + this.messages = new MessageStore(this); this._typing = new Map(); } - setup(data) { - super.setup(data); + _patch(data) { + super._patch(data); /** * The name of this Group DM, can be null if one isn't set @@ -88,7 +89,7 @@ class GroupDMChannel extends Channel { if (data.recipients) { for (const recipient of data.recipients) { - const user = this.client.dataManager.newUser(recipient); + const user = this.client.users.create(recipient); this.recipients.set(user.id, user); } } diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 1ed7864ad..145a0422c 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -1,5 +1,4 @@ const Long = require('long'); -const User = require('./User'); const Role = require('./Role'); const Emoji = require('./Emoji'); const Invite = require('./Invite'); @@ -15,40 +14,40 @@ const Util = require('../util/Util'); const Snowflake = require('../util/Snowflake'); const Permissions = require('../util/Permissions'); const Shared = require('./shared'); +const GuildMemberStore = require('../stores/GuildMemberStore'); +const RoleStore = require('../stores/RoleStore'); +const EmojiStore = require('../stores/EmojiStore'); +const GuildChannelStore = require('../stores/GuildChannelStore'); +const Base = require('./Base'); const { Error, TypeError } = require('../errors'); /** * Represents a guild (or a server) on Discord. * It's recommended to see if a guild is available before performing operations or reading data from it. You can * check this with `guild.available`. + * @extends {Base} */ -class Guild { +class Guild extends Base { constructor(client, data) { - /** - * The client that created the instance of the guild - * @name Guild#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: client }); + super(client); /** * A collection of members that are in this guild. The key is the member's ID, the value is the member * @type {Collection} */ - this.members = new Collection(); + this.members = new GuildMemberStore(this); /** * A collection of channels that are in this guild. The key is the channel's ID, the value is the channel - * @type {Collection} + * @type {GuildChannelStore} */ - this.channels = new Collection(); + this.channels = new GuildChannelStore(this); /** * A collection of roles that are in this guild. The key is the role's ID, the value is the role * @type {Collection} */ - this.roles = new Collection(); + this.roles = new RoleStore(this); /** * A collection of presences in this guild @@ -70,7 +69,7 @@ class Guild { */ this.id = data.id; } else { - this.setup(data); + this._patch(data); if (!data.channels) this.available = false; } } @@ -80,7 +79,7 @@ class Guild { * @param {*} data The raw data of the guild * @private */ - setup(data) { // eslint-disable-line complexity + _patch(data) { /** * The name of the guild * @type {string} @@ -177,7 +176,7 @@ class Guild { if (data.members) { this.members.clear(); - for (const guildUser of data.members) this._addMember(guildUser, false); + for (const guildUser of data.members) this.members.create(guildUser); } if (data.owner_id) { @@ -190,15 +189,14 @@ class Guild { if (data.channels) { this.channels.clear(); - for (const channel of data.channels) this.client.dataManager.newChannel(channel, this); + for (const rawChannel of data.channels) { + this.client.channels.create(rawChannel, this); + } } if (data.roles) { this.roles.clear(); - for (const role of data.roles) { - const newRole = new Role(this, role); - this.roles.set(newRole.id, newRole); - } + for (const role of data.roles) this.roles.create(role); } if (data.presences) { @@ -207,31 +205,18 @@ class Guild { } } - this._rawVoiceStates = new Collection(); + this.voiceStates = new VoiceStateCollection(this); if (data.voice_states) { - for (const voiceState of data.voice_states) { - this._rawVoiceStates.set(voiceState.user_id, voiceState); - const member = this.members.get(voiceState.user_id); - if (member) { - member.serverMute = voiceState.mute; - member.serverDeaf = voiceState.deaf; - member.selfMute = voiceState.self_mute; - member.selfDeaf = voiceState.self_deaf; - member.voiceSessionID = voiceState.session_id; - member.voiceChannelID = voiceState.channel_id; - this.channels.get(voiceState.channel_id).members.set(member.user.id, member); - } - } + for (const voiceState of data.voice_states) this.voiceStates.set(voiceState.user_id, voiceState); } if (!this.emojis) { /** - * A collection of emojis that are in this guild - * The key is the emoji's ID, the value is the emoji - * @type {Collection} + * A collection of emojis that are in this guild. The key is the emoji's ID, the value is the emoji. + * @type {EmojiStore} */ - this.emojis = new Collection(); - if (data.emojis) for (const emoji of data.emojis) this.emojis.set(emoji.id, new Emoji(this, emoji)); + this.emojis = new EmojiStore(this); + for (const emoji of data.emojis) this.emojis.create(emoji); } else { this.client.actions.GuildEmojisUpdate.handle({ guild_id: this.id, @@ -460,7 +445,7 @@ class Guild { bans.reduce((collection, ban) => { collection.set(ban.user.id, { reason: ban.reason, - user: this.client.dataManager.newUser(ban.user), + user: this.client.users.create(ban.user), }); return collection; }, new Collection()) @@ -1214,69 +1199,6 @@ class Guild { return this.name; } - _addMember(guildUser, emitEvent = true) { - const existing = this.members.has(guildUser.user.id); - if (!(guildUser.user instanceof User)) guildUser.user = this.client.dataManager.newUser(guildUser.user); - - guildUser.joined_at = guildUser.joined_at || 0; - const member = new GuildMember(this, guildUser); - this.members.set(member.id, member); - - if (this._rawVoiceStates && this._rawVoiceStates.has(member.user.id)) { - const voiceState = this._rawVoiceStates.get(member.user.id); - member.serverMute = voiceState.mute; - member.serverDeaf = voiceState.deaf; - member.selfMute = voiceState.self_mute; - member.selfDeaf = voiceState.self_deaf; - member.voiceSessionID = voiceState.session_id; - member.voiceChannelID = voiceState.channel_id; - if (this.client.channels.has(voiceState.channel_id)) { - this.client.channels.get(voiceState.channel_id).members.set(member.user.id, member); - } else { - this.client.emit('warn', `Member ${member.id} added in guild ${this.id} with an uncached voice channel`); - } - } - - /** - * Emitted whenever a user joins a guild. - * @event Client#guildMemberAdd - * @param {GuildMember} member The member that has joined a guild - */ - if (this.client.ws.connection.status === Constants.Status.READY && emitEvent && !existing) { - this.client.emit(Constants.Events.GUILD_MEMBER_ADD, member); - } - - return member; - } - - _updateMember(member, data) { - const oldMember = Util.cloneObject(member); - - if (data.roles) member._roles = data.roles; - if (typeof data.nick !== 'undefined') member.nickname = data.nick; - - const notSame = member.nickname !== oldMember.nickname || !Util.arraysEqual(member._roles, oldMember._roles); - - if (this.client.ws.connection.status === Constants.Status.READY && notSame) { - /** - * Emitted whenever a guild member changes - i.e. new role, removed role, nickname. - * @event Client#guildMemberUpdate - * @param {GuildMember} oldMember The member before the update - * @param {GuildMember} newMember The member after the update - */ - this.client.emit(Constants.Events.GUILD_MEMBER_UPDATE, oldMember, member); - } - - return { - old: oldMember, - mem: member, - }; - } - - _removeMember(guildMember) { - this.members.delete(guildMember.id); - } - _memberSpeakUpdate(user, speaking) { const member = this.members.get(user); if (member && member.speaking !== speaking) { @@ -1391,4 +1313,23 @@ class Guild { } } +class VoiceStateCollection extends Collection { + constructor(guild) { + super(); + this.guild = guild; + } + set(id, voiceState) { + super.set(id, voiceState); + const member = this.guild.members.get(id); + if (member) { + if (member.voiceChannel && member.voiceChannel.id !== voiceState.channel_id) { + member.voiceChannel.members.delete(member.id); + } + if (!voiceState.channel_id) member.speaking = null; + const newChannel = this.guild.channels.get(voiceState.channel_id); + if (newChannel) newChannel.members.set(member.user.id, member); + } + } +} + module.exports = Guild; diff --git a/src/structures/GuildAuditLogs.js b/src/structures/GuildAuditLogs.js index 616b3abf2..da6ca48bd 100644 --- a/src/structures/GuildAuditLogs.js +++ b/src/structures/GuildAuditLogs.js @@ -51,7 +51,7 @@ const Actions = { */ class GuildAuditLogs { constructor(guild, data) { - if (data.users) for (const user of data.users) guild.client.dataManager.newUser(user); + if (data.users) for (const user of data.users) guild.client.users.create(user); /** * Cached webhooks * @type {Collection} diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 5e2c4628e..185a6cc39 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -22,8 +22,8 @@ class GuildChannel extends Channel { this.guild = guild; } - setup(data) { - super.setup(data); + _patch(data) { + super._patch(data); /** * The name of the guild channel @@ -241,7 +241,11 @@ class GuildChannel extends Channel { user_limit: data.userLimit || this.userLimit, }, reason, - }).then(newData => this.client.actions.ChannelUpdate.handle(newData).updated); + }).then(newData => { + const clone = this._clone(); + clone._patch(newData); + return clone; + }); } /** diff --git a/src/structures/GuildMember.js b/src/structures/GuildMember.js index e4920402f..43f5b3092 100644 --- a/src/structures/GuildMember.js +++ b/src/structures/GuildMember.js @@ -2,22 +2,18 @@ const TextBasedChannel = require('./interfaces/TextBasedChannel'); const Role = require('./Role'); const Permissions = require('../util/Permissions'); const Collection = require('../util/Collection'); +const Base = require('./Base'); const { Presence } = require('./Presence'); const { Error, TypeError } = require('../errors'); /** * Represents a member of a guild on Discord. * @implements {TextBasedChannel} + * @extends {Base} */ -class GuildMember { +class GuildMember extends Base { constructor(guild, data) { - /** - * The client that instantiated this GuildMember - * @name GuildMember#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: guild.client }); + super(guild.client); /** * The guild that this member is part of @@ -32,7 +28,8 @@ class GuildMember { this.user = {}; this._roles = []; - if (data) this.setup(data); + + if (data) this._patch(data); /** * The ID of the last message sent by the member in their guild, if one was sent @@ -47,65 +44,72 @@ class GuildMember { this.lastMessage = null; } - setup(data) { - /** - * Whether this member is deafened server-wide - * @type {boolean} - */ - this.serverDeaf = data.deaf; - - /** - * Whether this member is muted server-wide - * @type {boolean} - */ - this.serverMute = data.mute; - - /** - * Whether this member is self-muted - * @type {boolean} - */ - this.selfMute = data.self_mute; - - /** - * Whether this member is self-deafened - * @type {boolean} - */ - this.selfDeaf = data.self_deaf; - - /** - * The voice session ID of this member, if any - * @type {?Snowflake} - */ - this.voiceSessionID = data.session_id; - - /** - * The voice channel ID of this member, if any - * @type {?Snowflake} - */ - this.voiceChannelID = data.channel_id; - + _patch(data) { /** * Whether this member is speaking * @type {boolean} + * @name GuildMember#speaking */ - this.speaking = false; + if (typeof this.speaking === 'undefined') this.speaking = false; /** * The nickname of this guild member, if they have one * @type {?string} + * @name GuildMember#nickname */ - this.nickname = data.nick || null; + if (typeof data.nick !== 'undefined') this.nickname = data.nick; /** * The timestamp the member joined the guild at * @type {number} + * @name GuildMember#joinedTimestamp */ - this.joinedTimestamp = new Date(data.joined_at).getTime(); + if (typeof data.joined_at !== 'undefined') this.joinedTimestamp = new Date(data.joined_at).getTime(); - this.user = data.user; - this._roles = data.roles; + this.user = this.guild.client.users.create(data.user); + if (data.roles) this._roles = data.roles; } + get voiceState() { + return this._frozenVoiceState || this.guild.voiceStates.get(this.id) || {}; + } + + /** + * Whether this member is deafened server-wide + * @type {boolean} + */ + get serverDeaf() { return this.voiceState.deaf; } + + /** + * Whether this member is muted server-wide + * @type {boolean} + */ + get serverMute() { return this.voiceState.mute; } + + /** + * Whether this member is self-muted + * @type {boolean} + */ + get selfMute() { return this.voiceState.self_mute; } + + /** + * Whether this member is self-deafened + * @type {boolean} + */ + get selfDeaf() { return this.voiceState.self_deaf; } + + /** + * The voice session ID of this member (if any) + * @type {?Snowflake} + */ + get voiceSessionID() { return this.voiceState.session_id; } + + /** + * The voice channel ID of this member, (if any) + * @type {?Snowflake} + */ + get voiceChannelID() { return this.voiceState.channel_id; } + /** * The time the member joined the guild * @type {Date} @@ -350,7 +354,11 @@ class GuildMember { } else { endpoint = endpoint.members(this.id); } - return endpoint.patch({ data, reason }).then(newData => this.guild._updateMember(this, newData).mem); + return endpoint.patch({ data, reason }).then(newData => { + const clone = this._clone(); + clone._patch(newData); + return clone; + }); } /** diff --git a/src/structures/Invite.js b/src/structures/Invite.js index fec7d8bf0..192bdf5f0 100644 --- a/src/structures/Invite.js +++ b/src/structures/Invite.js @@ -1,26 +1,20 @@ const Constants = require('../util/Constants'); +const Base = require('./Base'); /** * Represents an invitation to a guild channel. * The only guaranteed properties are `code`, `guild` and `channel`. Other properties can be missing. + * @extends {Base} */ -class Invite { +class Invite extends Base { constructor(client, data) { - /** - * The client that instantiated the invite - * @name Invite#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: client }); - - this.setup(data); + super(client); + this._patch(data); } - setup(data) { + _patch(data) { const Guild = require('./Guild'); const Channel = require('./Channel'); - /** * The guild the invite is for * @type {Guild} @@ -86,7 +80,7 @@ class Invite { * The user who created this invite * @type {User} */ - this.inviter = this.client.dataManager.newUser(data.inviter); + this.inviter = this.client.users.create(data.inviter); } /** diff --git a/src/structures/Message.js b/src/structures/Message.js index eb734e819..899767780 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -6,23 +6,20 @@ const ReactionCollector = require('./ReactionCollector'); const ClientApplication = require('./ClientApplication'); const Util = require('../util/Util'); const Collection = require('../util/Collection'); +const ReactionStore = require('../stores/ReactionStore'); const Constants = require('../util/Constants'); const Permissions = require('../util/Permissions'); +const Base = require('./Base'); const { Error, TypeError } = require('../errors'); let GuildMember; /** * Represents a message on Discord. + * @extends {Base} */ -class Message { +class Message extends Base { constructor(channel, data, client) { - /** - * The client that instantiated the Message - * @name Message#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: client }); + super(client); /** * The channel that the message was sent in @@ -30,10 +27,10 @@ class Message { */ this.channel = channel; - if (data) this.setup(data); + if (data) this._patch(data); } - setup(data) { // eslint-disable-line complexity + _patch(data) { // eslint-disable-line complexity /** * The ID of the message * @type {Snowflake} @@ -56,7 +53,7 @@ class Message { * The author of the message * @type {User} */ - this.author = this.client.dataManager.newUser(data.author); + this.author = this.client.users.create(data.author); /** * Represents the author of the message as a guild member @@ -116,9 +113,9 @@ class Message { /** * A collection of reactions to this message, mapped by the reaction ID - * @type {Collection} + * @type {ReactionStore} */ - this.reactions = new Collection(); + this.reactions = new ReactionStore(this); if (data.reactions && data.reactions.length > 0) { for (const reaction of data.reactions) { const id = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name; @@ -173,7 +170,7 @@ class Message { * @private */ patch(data) { - const clone = Util.cloneObject(this); + const clone = this._clone(); this._edits.unshift(clone); this.editedTimestamp = new Date(data.edited_timestamp).getTime(); @@ -395,7 +392,11 @@ class Message { return this.client.api.channels[this.channel.id].messages[this.id] .patch({ data: { content, embed } }) - .then(data => this.client.actions.MessageUpdate.handle(data).updated); + .then(data => { + const clone = this._clone(); + clone._patch(data); + return clone; + }); } /** @@ -427,7 +428,12 @@ class Message { return this.client.api.channels(this.channel.id).messages(this.id).reactions(emoji, '@me') .put() - .then(() => this._addReaction(Util.parseEmoji(emoji), this.client.user)); + .then(() => this.client.actions.MessageReactionAdd.handle({ + user: this.client.user, + channel: this.channel, + message: this, + emoji: Util.parseEmoji(emoji), + }).reaction); } /** @@ -553,42 +559,6 @@ class Message { toString() { return this.content; } - - _addReaction(emoji, user) { - const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : encodeURIComponent(emoji.name); - let reaction; - if (this.reactions.has(emojiID)) { - reaction = this.reactions.get(emojiID); - if (!reaction.me) reaction.me = user.id === this.client.user.id; - } else { - reaction = new MessageReaction(this, emoji, 0, user.id === this.client.user.id); - this.reactions.set(emojiID, reaction); - } - if (!reaction.users.has(user.id)) { - reaction.users.set(user.id, user); - reaction.count++; - } - return reaction; - } - - _removeReaction(emoji, user) { - const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : encodeURIComponent(emoji.name); - if (this.reactions.has(emojiID)) { - const reaction = this.reactions.get(emojiID); - if (reaction.users.has(user.id)) { - reaction.users.delete(user.id); - reaction.count--; - if (user.id === this.client.user.id) reaction.me = false; - if (reaction.count <= 0) this.reactions.delete(emojiID); - return reaction; - } - } - return null; - } - - _clearReactions() { - this.reactions.clear(); - } } module.exports = Message; diff --git a/src/structures/MessageMentions.js b/src/structures/MessageMentions.js index f6144ae30..bd187c760 100644 --- a/src/structures/MessageMentions.js +++ b/src/structures/MessageMentions.js @@ -22,8 +22,7 @@ class MessageMentions { } else { this.users = new Collection(); for (const mention of users) { - let user = message.client.users.get(mention.id); - if (!user) user = message.client.dataManager.newUser(mention); + let user = message.client.users.create(mention); this.users.set(user.id, user); } } diff --git a/src/structures/MessageReaction.js b/src/structures/MessageReaction.js index ee00bbf0d..0bf047743 100644 --- a/src/structures/MessageReaction.js +++ b/src/structures/MessageReaction.js @@ -90,13 +90,32 @@ class MessageReaction { .then(users => { this.users = new Collection(); for (const rawUser of users) { - const user = message.client.dataManager.newUser(rawUser); + const user = message.client.users.create(rawUser); this.users.set(user.id, user); } this.count = this.users.size; return this.users; }); } + + _add(user) { + if (!this.users.has(user.id)) { + this.users.set(user.id, user); + this.count++; + } + if (!this.me) this.me = user.id === this.message.client.user.id; + } + + _remove(user) { + if (this.users.has(user.id)) { + this.users.delete(user.id); + this.count--; + if (user.id === this.message.client.user.id) this.me = false; + if (this.count <= 0) { + this.message.reactions.remove(this.emoji.id || this.emoji.name); + } + } + } } module.exports = MessageReaction; diff --git a/src/structures/PermissionOverwrites.js b/src/structures/PermissionOverwrites.js index fffa12b89..08c4321a5 100644 --- a/src/structures/PermissionOverwrites.js +++ b/src/structures/PermissionOverwrites.js @@ -13,10 +13,10 @@ class PermissionOverwrites { */ Object.defineProperty(this, 'channel', { value: guildChannel }); - if (data) this.setup(data); + if (data) this._patch(data); } - setup(data) { + _patch(data) { /** * The ID of this overwrite, either a user ID or a role ID * @type {Snowflake} diff --git a/src/structures/Role.js b/src/structures/Role.js index 2db019931..ba038264f 100644 --- a/src/structures/Role.js +++ b/src/structures/Role.js @@ -1,19 +1,15 @@ const Snowflake = require('../util/Snowflake'); const Permissions = require('../util/Permissions'); const Util = require('../util/Util'); +const Base = require('./Base'); /** * Represents a role on Discord. + * @extends {Base} */ -class Role { +class Role extends Base { constructor(guild, data) { - /** - * The client that instantiated the role - * @name Role#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: guild.client }); + super(guild.client); /** * The guild that the role belongs to @@ -21,10 +17,10 @@ class Role { */ this.guild = guild; - if (data) this.setup(data); + if (data) this._patch(data); } - setup(data) { + _patch(data) { /** * The ID of the role (unique to the guild it is part of) * @type {Snowflake} @@ -215,7 +211,11 @@ class Role { }, reason, }) - .then(role => this.client.actions.GuildRoleUpdate.handle({ role, guild_id: this.guild.id }).updated); + .then(role => { + const clone = this._clone(); + clone._patch(role); + return clone; + }); } /** @@ -320,9 +320,10 @@ class Role { */ delete(reason) { return this.client.api.guilds[this.guild.id].roles[this.id].delete({ reason }) - .then(() => - this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: this.id }).role - ); + .then(() => { + this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: this.id }); + return this; + }); } /** diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index d0dc1fd3a..693bd35eb 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -2,6 +2,7 @@ const GuildChannel = require('./GuildChannel'); const Webhook = require('./Webhook'); const TextBasedChannel = require('./interfaces/TextBasedChannel'); const Collection = require('../util/Collection'); +const MessageStore = require('../stores/MessageStore'); /** * Represents a guild text channel on Discord. @@ -12,12 +13,12 @@ class TextChannel extends GuildChannel { constructor(guild, data) { super(guild, data); this.type = 'text'; - this.messages = new Collection(); + this.messages = new MessageStore(this); this._typing = new Map(); } - setup(data) { - super.setup(data); + _patch(data) { + super._patch(data); /** * The topic of the text channel diff --git a/src/structures/User.js b/src/structures/User.js index 13ed16a21..2a8aa0719 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -3,26 +3,21 @@ const Constants = require('../util/Constants'); const { Presence } = require('./Presence'); const UserProfile = require('./UserProfile'); const Snowflake = require('../util/Snowflake'); +const Base = require('./Base'); const { Error } = require('../errors'); /** * Represents a user on Discord. * @implements {TextBasedChannel} + * @extends {Base} */ -class User { +class User extends Base { constructor(client, data) { - /** - * The client that created the instance of the user - * @name User#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: client }); - - if (data) this.setup(data); + super(client); + this._patch(data); } - setup(data) { + _patch(data) { /** * The ID of the user * @type {Snowflake} @@ -32,26 +27,30 @@ class User { /** * The username of the user * @type {string} + * @name User#username */ - this.username = data.username; + if (data.username) this.username = data.username; /** * A discriminator based on username for the user * @type {string} + * @name User#discriminator */ - this.discriminator = data.discriminator; + if (data.discriminator) this.discriminator = data.discriminator; /** * The ID of the user's avatar * @type {string} + * @name User#avatar */ - this.avatar = data.avatar; + if (data.avatar) this.avatar = data.avatar; /** * Whether or not the user is a bot * @type {boolean} + * @name User#bot */ - this.bot = Boolean(data.bot); + if (typeof this.bot === 'undefined' && typeof data.bot !== 'undefined') this.bot = Boolean(data.bot); /** * The ID of the last message sent by the user, if one was sent @@ -64,12 +63,7 @@ class User { * @type {?Message} */ this.lastMessage = null; - } - patch(data) { - for (const prop of ['id', 'username', 'discriminator', 'avatar', 'bot']) { - if (typeof data[prop] !== 'undefined') this[prop] = data[prop]; - } if (data.token) this.client.token = data.token; } diff --git a/src/structures/UserConnection.js b/src/structures/UserConnection.js index ea6c65f11..3bcdb0894 100644 --- a/src/structures/UserConnection.js +++ b/src/structures/UserConnection.js @@ -9,10 +9,10 @@ class UserConnection { */ this.user = user; - this.setup(data); + this._patch(data); } - setup(data) { + _patch(data) { /** * The type of the connection * @type {string} diff --git a/src/structures/UserProfile.js b/src/structures/UserProfile.js index 76ebb8f47..07cfdf96e 100644 --- a/src/structures/UserProfile.js +++ b/src/structures/UserProfile.js @@ -1,26 +1,22 @@ const Collection = require('../util/Collection'); const { UserFlags } = require('../util/Constants'); const UserConnection = require('./UserConnection'); +const Base = require('./Base'); /** * Represents a user's profile on Discord. + * @extends {Base} */ -class UserProfile { +class UserProfile extends Base { constructor(user, data) { + super(user.client); + /** * The owner of the profile * @type {User} */ this.user = user; - /** - * The client that created the instance of the UserProfile - * @name UserProfile#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: user.client }); - /** * The guilds that the client user and the user share * @type {Collection} @@ -33,10 +29,10 @@ class UserProfile { */ this.connections = new Collection(); - this.setup(data); + this._patch(data); } - setup(data) { + _patch(data) { /** * If the user has Discord Premium * @type {boolean} diff --git a/src/structures/VoiceChannel.js b/src/structures/VoiceChannel.js index 9b7bd7e79..2bfaffe79 100644 --- a/src/structures/VoiceChannel.js +++ b/src/structures/VoiceChannel.js @@ -17,9 +17,7 @@ class VoiceChannel extends GuildChannel { Object.defineProperty(this, 'members', { value: new Collection() }); } - setup(data) { - super.setup(data); - + _patch(data) { /** * The bitrate of this voice channel * @type {number} diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index a179f545b..ae50a3ff7 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -17,7 +17,7 @@ class Webhook { * @readonly */ Object.defineProperty(this, 'client', { value: client }); - if (dataOrID) this.setup(dataOrID); + if (dataOrID) this._patch(dataOrID); } else { this.id = dataOrID; this.token = token; @@ -25,7 +25,7 @@ class Webhook { } } - setup(data) { + _patch(data) { /** * The name of the webhook * @type {string} diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 0bae9cbe9..025a96271 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -1,11 +1,12 @@ const path = require('path'); const MessageCollector = require('../MessageCollector'); const Shared = require('../shared'); -const Collection = require('../../util/Collection'); +const MessageStore = require('../../stores/MessageStore'); const Snowflake = require('../../util/Snowflake'); +const Collection = require('../../util/Collection'); const Attachment = require('../../structures/Attachment'); const MessageEmbed = require('../../structures/MessageEmbed'); -const { Error, RangeError, TypeError } = require('../../errors'); +const { RangeError, TypeError } = require('../../errors'); /** * Interface for classes that have text-channel-like features. @@ -15,9 +16,9 @@ class TextBasedChannel { constructor() { /** * A collection containing the messages sent to this channel - * @type {Collection} + * @type {MessageStore} */ - this.messages = new Collection(); + this.messages = new MessageStore(this); /** * The ID of the last message in the channel, if one was sent @@ -135,87 +136,6 @@ class TextBasedChannel { return Shared.sendMessage(this, options); } - /** - * Gets a single message from this channel, regardless of it being cached or not. Since the single message fetching - * endpoint is reserved for bot accounts, this abstracts the `fetchMessages` method to obtain the single message when - * using a user account. - * @param {Snowflake} messageID ID of the message to get - * @returns {Promise} - * @example - * // Get message - * channel.fetchMessage('99539446449315840') - * .then(message => console.log(message.content)) - * .catch(console.error); - */ - fetchMessage(messageID) { - const Message = require('../Message'); - if (!this.client.user.bot) { - return this.fetchMessages({ limit: 1, around: messageID }) - .then(messages => { - const msg = messages.get(messageID); - if (!msg) throw new Error('MESSAGE_MISSING'); - return msg; - }); - } - return this.client.api.channels[this.id].messages[messageID].get() - .then(data => { - const msg = data instanceof Message ? data : new Message(this, data, this.client); - this._cacheMessage(msg); - return msg; - }); - } - - /** - * The parameters to pass in when requesting previous messages from a channel. `around`, `before` and - * `after` are mutually exclusive. All the parameters are optional. - * @typedef {Object} ChannelLogsQueryOptions - * @property {number} [limit=50] Number of messages to acquire - * @property {Snowflake} [before] ID of a message to get the messages that were posted before it - * @property {Snowflake} [after] ID of a message to get the messages that were posted after it - * @property {Snowflake} [around] ID of a message to get the messages that were posted around it - */ - - /** - * Gets the past messages sent in this channel. Resolves with a collection mapping message ID's to Message objects. - * @param {ChannelLogsQueryOptions} [options={}] Query parameters to pass in - * @returns {Promise>} - * @example - * // Get messages - * channel.fetchMessages({limit: 10}) - * .then(messages => console.log(`Received ${messages.size} messages`)) - * .catch(console.error); - */ - fetchMessages(options = {}) { - const Message = require('../Message'); - return this.client.api.channels[this.id].messages.get({ query: options }) - .then(data => { - const messages = new Collection(); - for (const message of data) { - const msg = new Message(this, message, this.client); - messages.set(message.id, msg); - this._cacheMessage(msg); - } - return messages; - }); - } - - /** - * Fetches the pinned messages of this channel and returns a collection of them. - * @returns {Promise>} - */ - fetchPinnedMessages() { - const Message = require('../Message'); - return this.client.api.channels[this.id].pins.get().then(data => { - const messages = new Collection(); - for (const message of data) { - const msg = new Message(this, message, this.client); - messages.set(message.id, msg); - this._cacheMessage(msg); - } - return messages; - }); - } - /** * Performs a search within the channel. * This is only available when using a user account. @@ -394,29 +314,17 @@ class TextBasedChannel { }); } - _cacheMessage(message) { - const maxSize = this.client.options.messageCacheMaxSize; - if (maxSize === 0) return null; - if (this.messages.size >= maxSize && maxSize > 0) this.messages.delete(this.messages.firstKey()); - this.messages.set(message.id, message); - return message; - } - static applyToClass(structure, full = false, ignore = []) { const props = ['send']; if (full) { props.push( - '_cacheMessage', 'acknowledge', - 'fetchMessages', - 'fetchMessage', 'search', 'bulkDelete', 'startTyping', 'stopTyping', 'typing', 'typingCount', - 'fetchPinnedMessages', 'createMessageCollector', 'awaitMessages' ); diff --git a/test/random.js b/test/random.js index 6cc9ed559..8bbd2551b 100644 --- a/test/random.js +++ b/test/random.js @@ -8,9 +8,9 @@ console.time('magic'); const client = new Discord.Client({ fetchAllMembers: true, apiRequestMethod: 'sequential' }); -const { email, password, token, usertoken, song } = require('./auth.json'); +const { email, password, token, usertoken, song } = require('./auth.js'); -client.login(token).then(atoken => console.log('logged in with token ' + atoken)).catch(console.error); +client.login(token).then(atoken => console.log('logged in')).catch(console.error); client.on('ready', () => { console.log(`ready with ${client.users.size} users`); diff --git a/typings b/typings index 0967675a2..f07cf2bb9 160000 --- a/typings +++ b/typings @@ -1 +1 @@ -Subproject commit 0967675a2f8e6fa46ab543f955af82a82230d17a +Subproject commit f07cf2bb9673d15b77872abc7ab51395c8cced16