From 5cd42695aeb974e998d4b220cba1c27b30a55628 Mon Sep 17 00:00:00 2001 From: Isabella Date: Tue, 14 Nov 2017 02:11:44 -0600 Subject: [PATCH] refactor(MessageReaction): ReactionUserStore (#2078) * refactor(MessageReactions): fetchUsers() is now users.fetch() made a lovely class for it and all happify linter stuff i know how to code i swear i lied * bdistin suggestions * space suggestions, rename store * fix count * documentation update --- src/index.js | 1 + src/stores/ReactionUserStore.js | 33 +++++++++++++++++++++++++++++++ src/structures/MessageReaction.js | 26 +++--------------------- 3 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 src/stores/ReactionUserStore.js diff --git a/src/index.js b/src/index.js index 7bea36f1b4f0..de6a6e66c7d8 100644 --- a/src/index.js +++ b/src/index.js @@ -29,6 +29,7 @@ module.exports = { GuildChannelStore: require('./stores/GuildChannelStore'), GuildMemberStore: require('./stores/GuildMemberStore'), GuildStore: require('./stores/GuildStore'), + ReactionUserStore: require('./stores/ReactionUserStore'), MessageStore: require('./stores/MessageStore'), PresenceStore: require('./stores/PresenceStore'), RoleStore: require('./stores/RoleStore'), diff --git a/src/stores/ReactionUserStore.js b/src/stores/ReactionUserStore.js new file mode 100644 index 000000000000..b3c3ec012638 --- /dev/null +++ b/src/stores/ReactionUserStore.js @@ -0,0 +1,33 @@ +const DataStore = require('./DataStore'); +/** + * A data store to store User models who reacted to a MessageReaction. + * @extends {DataStore} + */ +class ReactionUserStore extends DataStore { + constructor(client, iterable, reaction) { + super(client, iterable, require('../structures/User')); + this.reaction = reaction; + } + + /** + * Fetches all the users that gave this reaction. Resolves with a collection of users, mapped by their IDs. + * @param {Object} [options] Options for fetching the users + * @param {number} [options.limit=100] The maximum amount of users to fetch, defaults to 100 + * @param {Snowflake} [options.before] Limit fetching users to those with an id lower than the supplied id + * @param {Snowflake} [options.after] Limit fetching users to those with an id greater than the supplied id + * @returns {Promise>} + */ + async fetch({ limit = 100, after, before } = {}) { + const message = this.reaction.message; + const users = await this.client.api.channels[message.channel.id].messages[message.id] + .reactions[this.reaction.emoji.identifier] + .get({ query: { limit, before, after } }); + for (const rawUser of users) { + const user = this.client.users.create(rawUser); + this.set(user.id, user); + } + return this; + } +} + +module.exports = ReactionUserStore; diff --git a/src/structures/MessageReaction.js b/src/structures/MessageReaction.js index 2b273f84bc9c..28a320a63dd3 100644 --- a/src/structures/MessageReaction.js +++ b/src/structures/MessageReaction.js @@ -1,6 +1,6 @@ -const Collection = require('../util/Collection'); const Emoji = require('./Emoji'); const ReactionEmoji = require('./ReactionEmoji'); +const ReactionUserStore = require('../stores/ReactionUserStore'); const { Error } = require('../errors'); /** @@ -28,9 +28,9 @@ class MessageReaction { /** * The users that have given this reaction, mapped by their ID - * @type {Collection} + * @type {ReactionUserStore} */ - this.users = new Collection(); + this.users = new ReactionUserStore(client, undefined, this); this._emoji = new ReactionEmoji(this, data.emoji.name, data.emoji.id); } @@ -77,26 +77,6 @@ class MessageReaction { ); } - /** - * Fetches all the users that gave this reaction. Resolves with a collection of users, mapped by their IDs. - * @param {Object} [options] Options for fetching the users - * @param {number} [options.limit=100] The maximum amount of users to fetch, defaults to 100 - * @param {Snowflake} [options.after] Limit fetching users to those with an id greater than the supplied id - * @returns {Promise>} - */ - async fetchUsers({ limit = 100, after } = {}) { - const message = this.message; - const users = await message.client.api.channels[message.channel.id].messages[message.id] - .reactions[this.emoji.identifier] - .get({ query: { limit, after } }); - for (const rawUser of users) { - 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);