From c4b5736a16fb651ce90038cc6682d645f108f76f Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Sun, 3 Sep 2017 12:57:49 +0200 Subject: [PATCH] refactor/fix(DataStores): move instantiating of store related classes into their date stores (#1869) * refactor/fix(DataStores): move instantiating of store related classes into the relevant create methods and fixed FetchMemberOptions * Renamed ChannelStore's constructor parameter for claritiy * define client property before creating items from passed iterable to avoid the client property being undefined * Fix fetching members with a limit * add static Channel#create back and moved channel instantiating there * make ChannelStore's constructor accept an iterable * make iterable an optional parameter, allow to just pass client and options * fixed a little typo in docs of .displayAvatarURL() (#1872) * Rewrite presence a little bit (#1853) * such presence many good * Update PresenceStore.js * Update index.js * Update ClientPresenceStore.js * Update Presence.js * Update ClientPresenceStore.js * Update ClientUser.js * Update Presence.js * add timestamps and party * Update Presence.js * Update PresenceStore.js * Update ClientPresenceStore.js * Update ClientPresenceStore.js * fix: made options.type optional in ClientUser#setActivity (#1875) * fix: made options.type optional in ClientUser#setActivity * some changes, smol fix due to too many types being possible, it should just be defaulted to 0. this means streamers will have to set the type manually, though. also mistake with activity.name check * docs(Collection): Adjust exists documentation (#1876) Since the return value of Collection#exists is basically just a boolean of Collection#find's result, the same documentation and arguments can and should be applied. * refactor(Emoji): remove Guild#deleteEmoji in favour of Emoji#delete (#1877) * refactor/fix(DataStores): move instantiating of store related classes into the relevant create methods and fixed FetchMemberOptions * Renamed ChannelStore's constructor parameter for claritiy * define client property before creating items from passed iterable to avoid the client property being undefined * Fix fetching members with a limit * add static Channel#create back and moved channel instantiating there * make ChannelStore's constructor accept an iterable * make iterable an optional parameter, allow to just pass client and options * rebase, this time with saving the file * address issue with falsy activity throwing errors when setting client's presence --- src/stores/ChannelStore.js | 32 ++++++++++++------------------- src/stores/ClientPresenceStore.js | 16 +++++++++------- src/stores/DataStore.js | 2 +- src/stores/GuildChannelStore.js | 13 ++++--------- src/stores/GuildMemberStore.js | 20 +++++++++---------- src/stores/GuildStore.js | 4 ++-- src/stores/UserStore.js | 8 +++----- src/structures/ClientUser.js | 6 ++---- src/structures/Invite.js | 6 ++---- src/structures/Message.js | 4 +--- src/structures/Webhook.js | 6 ++---- src/structures/shared/Search.js | 3 +-- 12 files changed, 49 insertions(+), 71 deletions(-) diff --git a/src/stores/ChannelStore.js b/src/stores/ChannelStore.js index 657af240a..2adb39a20 100644 --- a/src/stores/ChannelStore.js +++ b/src/stores/ChannelStore.js @@ -1,6 +1,5 @@ const DataStore = require('./DataStore'); -const DMChannel = require('../structures/DMChannel'); -const GroupDMChannel = require('../structures/GroupDMChannel'); +const Channel = require('../structures/Channel'); const Constants = require('../util/Constants'); const kLru = Symbol('LRU'); @@ -12,8 +11,12 @@ const lruable = ['group', 'dm']; * @extends {DataStore} */ class ChannelStore extends DataStore { - constructor(iterable, options = {}) { - super(iterable); + constructor(client, iterableOrOptions = {}, options) { + if (!options && typeof iterableOrOptions[Symbol.iterator] !== 'function') { + options = iterableOrOptions; + iterableOrOptions = undefined; + } + super(client, iterableOrOptions); if (options.lru) { const lru = this[kLru] = []; @@ -52,22 +55,11 @@ class ChannelStore extends DataStore { 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; + const channel = Channel.create(this.client, data, guild); + + if (!channel) { + this.client.emit(Constants.Events.DEBUG, `Failed to find guild for channel ${data.id} ${data.type}`); + return null; } if (cache) this.set(channel.id, channel); diff --git a/src/stores/ClientPresenceStore.js b/src/stores/ClientPresenceStore.js index f29b95096..42cdf55da 100644 --- a/src/stores/ClientPresenceStore.js +++ b/src/stores/ClientPresenceStore.js @@ -16,15 +16,17 @@ class ClientPresenceStore extends PresenceStore { } async setClientPresence({ status, since, afk, activity }) { // eslint-disable-line complexity - if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', 'name', 'string'); - if (!activity.type) activity.type = 0; const applicationID = activity && (activity.application ? activity.application.id || activity.application : null); let assets = new Collection(); - if (activity && activity.assets && applicationID) { - try { - const a = await this.client.api.oauth2.applications(applicationID).assets.get(); - for (const asset of a) assets.set(asset.name, asset.id); - } catch (err) {} // eslint-disable-line no-empty + if (activity) { + if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', 'name', 'string'); + if (!activity.type) activity.type = 0; + if (activity.assets && applicationID) { + try { + const a = await this.client.api.oauth2.applications(applicationID).assets.get(); + for (const asset of a) assets.set(asset.name, asset.id); + } catch (err) { } // eslint-disable-line no-empty + } } const packet = { diff --git a/src/stores/DataStore.js b/src/stores/DataStore.js index cb1e84ecc..b66d08880 100644 --- a/src/stores/DataStore.js +++ b/src/stores/DataStore.js @@ -7,8 +7,8 @@ const Collection = require('../util/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 }); + if (iterable) for (const item of iterable) this.create(item); } // Stubs diff --git a/src/stores/GuildChannelStore.js b/src/stores/GuildChannelStore.js index e59f46676..f6e4709a4 100644 --- a/src/stores/GuildChannelStore.js +++ b/src/stores/GuildChannelStore.js @@ -1,7 +1,6 @@ const DataStore = require('./DataStore'); -const TextChannel = require('../structures/TextChannel'); -const VoiceChannel = require('../structures/VoiceChannel'); -const Constants = require('../util/Constants'); +const Channel = require('../structures/Channel'); + /** * Stores guild channels. * @private @@ -13,15 +12,11 @@ class GuildChannelStore extends DataStore { this.guild = guild; } - create(data, cache = true) { + create(data) { 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; + return Channel.create(this.client, data, this.guild); } } diff --git a/src/stores/GuildMemberStore.js b/src/stores/GuildMemberStore.js index 05bba0122..d81029cf8 100644 --- a/src/stores/GuildMemberStore.js +++ b/src/stores/GuildMemberStore.js @@ -2,9 +2,10 @@ const DataStore = require('./DataStore'); const GuildMember = require('../structures/GuildMember'); const Constants = require('../util/Constants'); const Collection = require('../util/Collection'); +const { Error } = require('../errors'); + /** * Stores guild members. - * @private * @extends {DataStore} */ class GuildMemberStore extends DataStore { @@ -13,12 +14,12 @@ class GuildMemberStore extends DataStore { this.guild = guild; } - create(data) { + create(data, cache = true) { const existing = this.get(data.user.id); if (existing) return existing; const member = new GuildMember(this.guild, data); - this.set(member.id, member); + if (cache) this.set(member.id, member); return member; } @@ -66,7 +67,7 @@ class GuildMemberStore extends DataStore { const user = this.client.resolver.resolveUserID(options); if (user) return this._fetchSingle({ user, cache: true }); if (options.user) { - options.user = this.client.resolver.resolveUser(options); + options.user = this.client.resolver.resolveUserID(options.user); if (options.user) return this._fetchSingle(options); } return this._fetchMany(options); @@ -75,10 +76,7 @@ class GuildMemberStore extends DataStore { _fetchSingle({ user, cache }) { if (this.has(user)) return Promise.resolve(this.get(user)); return this.client.api.guilds(this.guild.id).members(user).get() - .then(data => { - if (cache) return this.create(data); - else return new GuildMember(this, data); - }); + .then(data => this.create(data, cache)); } _fetchMany({ query = '', limit = 0 } = {}) { @@ -99,9 +97,11 @@ class GuildMemberStore extends DataStore { const handler = (members, guild) => { if (guild.id !== this.guild.id) return; for (const member of members.values()) { - if (query || limit) fetchedMembers.set(member.user.id, member); + if (query || limit) fetchedMembers.set(member.id, member); } - if (this.guild.memberCount === this.size || ((query || limit) && members.size < 1000)) { + if (this.guild.memberCount <= this.size || + ((query || limit) && members.size < 1000) || + (limit && fetchedMembers.size >= limit)) { this.guild.client.removeListener(Constants.Events.GUILD_MEMBERS_CHUNK, handler); resolve(query || limit ? fetchedMembers : this); } diff --git a/src/stores/GuildStore.js b/src/stores/GuildStore.js index 7b05403f7..45201abb8 100644 --- a/src/stores/GuildStore.js +++ b/src/stores/GuildStore.js @@ -6,12 +6,12 @@ const Guild = require('../structures/Guild'); * @extends {DataStore} */ class GuildStore extends DataStore { - create(data) { + create(data, cache = true) { const existing = this.get(data.id); if (existing) return existing; const guild = new Guild(this.client, data); - this.set(guild.id, guild); + if (cache) this.set(guild.id, guild); return guild; } diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js index c393145b8..2eab91d01 100644 --- a/src/stores/UserStore.js +++ b/src/stores/UserStore.js @@ -6,12 +6,12 @@ const User = require('../structures/User'); * @extends {DataStore} */ class UserStore extends DataStore { - create(data) { + create(data, cache = true) { const existing = this.get(data.id); if (existing) return existing; const user = new User(this.client, data); - this.set(user.id, user); + if (cache) this.set(user.id, user); return user; } @@ -26,9 +26,7 @@ class UserStore extends DataStore { 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) - ); + return this.client.api.users(id).get().then(data => this.create(data, cache)); } } diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index 85a75cefa..231d8573b 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -5,8 +5,6 @@ const ClientUserGuildSettings = require('./ClientUserGuildSettings'); const Constants = require('../util/Constants'); const Util = require('../util/Util'); const Guild = require('./Guild'); -const Message = require('./Message'); -const GroupDMChannel = require('./GroupDMChannel'); /** * Represents the logged in client's Discord user. @@ -258,7 +256,7 @@ class ClientUser extends User { Util.mergeDefault({ limit: 25, roles: true, everyone: true, guild: null }, options); return this.client.api.users('@me').mentions.get({ query: options }) - .then(data => data.map(m => new Message(this.client.channels.get(m.channel_id), m, this.client))); + .then(data => data.map(m => this.client.channels.get(m.channel_id).messages.create(m, false))); } /** @@ -324,7 +322,7 @@ class ClientUser extends User { }, {}), } : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u.user || u.id)) }; return this.client.api.users('@me').channels.post({ data }) - .then(res => new GroupDMChannel(this.client, res)); + .then(res => this.client.channels.create(res)); } } diff --git a/src/structures/Invite.js b/src/structures/Invite.js index 192bdf5f0..e037e6418 100644 --- a/src/structures/Invite.js +++ b/src/structures/Invite.js @@ -13,13 +13,11 @@ class Invite extends Base { } _patch(data) { - const Guild = require('./Guild'); - const Channel = require('./Channel'); /** * The guild the invite is for * @type {Guild} */ - this.guild = this.client.guilds.get(data.guild.id) || new Guild(this.client, data.guild); + this.guild = this.client.guilds.create(data.guild, false); /** * The code for this invite @@ -87,7 +85,7 @@ class Invite extends Base { * The channel the invite is for * @type {GuildChannel} */ - this.channel = this.client.channels.get(data.channel.id) || Channel.create(this.client, data.channel, this.guild); + this.channel = this.client.channels.create(data.channel, this.guild, false); /** * The timestamp the invite was created at diff --git a/src/structures/Message.js b/src/structures/Message.js index 0d9a86ce3..6490388cc 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -1,7 +1,6 @@ const Mentions = require('./MessageMentions'); const Attachment = require('./MessageAttachment'); const Embed = require('./MessageEmbed'); -const MessageReaction = require('./MessageReaction'); const ReactionCollector = require('./ReactionCollector'); const ClientApplication = require('./ClientApplication'); const Util = require('../util/Util'); @@ -118,8 +117,7 @@ class Message extends Base { 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; - this.reactions.set(id, new MessageReaction(this, reaction.emoji, reaction.count, reaction.me)); + this.reactions.create(reaction); } } diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index 81f7a604a..8d96e1ed6 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -210,8 +210,7 @@ class Webhook { auth: false, }).then(data => { if (!this.client.channels) return data; - const Message = require('./Message'); - return new Message(this.client.channels.get(data.channel_id), data, this.client); + return this.client.channels.get(data.channel_id).messages.create(data, false); }); } @@ -239,8 +238,7 @@ class Webhook { data: body, }).then(data => { if (!this.client.channels) return data; - const Message = require('./Message'); - return new Message(this.client.channels.get(data.channel_id), data, this.client); + return this.client.channels.get(data.channel_id).messages.create(data, false); }); } diff --git a/src/structures/shared/Search.js b/src/structures/shared/Search.js index 6d7e510b8..b039239ad 100644 --- a/src/structures/shared/Search.js +++ b/src/structures/shared/Search.js @@ -84,14 +84,13 @@ module.exports = function search(target, options) { // Lazy load these because some of them use util const Channel = require('../Channel'); const Guild = require('../Guild'); - const Message = require('../Message'); if (!(target instanceof Channel || target instanceof Guild)) throw new TypeError('SEARCH_CHANNEL_TYPE'); let endpoint = target.client.api[target instanceof Channel ? 'channels' : 'guilds'](target.id).messages().search; return endpoint.get({ query: options }).then(body => { const results = body.messages.map(x => - x.map(m => new Message(target.client.channels.get(m.channel_id), m, target.client)) + x.map(m => target.client.channels.get(m.channel_id).messages.create(m, false)) ); return { total: body.total_results,