From 4e0e64d8a19096c625603fe02f26cac2e42fda66 Mon Sep 17 00:00:00 2001 From: Lewdcario Date: Sat, 24 Mar 2018 15:22:21 -0600 Subject: [PATCH] fix(MessageReaction): inaccurate count also works towards async rewrite goal fixes #2404 --- src/stores/MessageStore.js | 30 +++++++++++++----------------- src/structures/MessageReaction.js | 18 +++++++----------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/stores/MessageStore.js b/src/stores/MessageStore.js index 565f4c13d..00aed5895 100644 --- a/src/stores/MessageStore.js +++ b/src/stores/MessageStore.js @@ -45,7 +45,7 @@ class MessageStore extends DataStore { * .catch(console.error); * @example * // Get messages - * channel.messages.fetch({limit: 10}) + * channel.messages.fetch({ limit: 10 }) * .then(messages => console.log(`Received ${messages.size} messages`)) * .catch(console.error); */ @@ -67,26 +67,22 @@ class MessageStore extends DataStore { }); } - _fetchId(messageID) { + async _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; - }); + const messages = await this._fetchMany({ limit: 1, around: messageID }); + 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.add(data)); + const data = await this.client.api.channels[this.channel.id].messages[messageID].get(); + return this.add(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.add(message)); - return messages; - }); + async _fetchMany(options = {}) { + const data = await this.client.api.channels[this.channel.id].messages.get({ query: options }); + const messages = new Collection(); + for (const message of data) messages.set(message.id, this.add(message)); + return messages; } diff --git a/src/structures/MessageReaction.js b/src/structures/MessageReaction.js index 81cb51c58..872c7a827 100644 --- a/src/structures/MessageReaction.js +++ b/src/structures/MessageReaction.js @@ -62,21 +62,17 @@ class MessageReaction { } _add(user) { - if (!this.users.has(user.id)) { - this.users.set(user.id, user); - if (!this.me || user.id !== this.message.client.user.id) this.count++; - } + this.users.set(user.id, user); + if (!this.me || user.id !== this.message.client.user.id || this.count === 0) 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); - } + this.users.delete(user.id); + if (!this.me || user.id !== this.message.client.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); } } }