From dd9c2915082211be67d851040f540a20d38d9e8c Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Thu, 27 Oct 2016 16:58:06 +0100 Subject: [PATCH] Add reaction fetching of users --- src/client/rest/RESTMethods.js | 7 +++++++ src/structures/MessageReaction.js | 22 ++++++++++++++++++++++ test/random.js | 16 ++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index a3f99d286..601697644 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -751,6 +751,13 @@ class RESTMethods { .catch(reject); }); } + + getMessageReactionUsers(channelID, messageID, emoji, limit = 100) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('get', Constants.Endpoints.messageReaction(channelID, messageID, emoji, limit), true) + .then(resolve, reject); + }); + } } module.exports = RESTMethods; diff --git a/src/structures/MessageReaction.js b/src/structures/MessageReaction.js index 77d6a1bd4..67f8b9ffb 100644 --- a/src/structures/MessageReaction.js +++ b/src/structures/MessageReaction.js @@ -108,6 +108,28 @@ class MessageReaction { const message = this.message; return message.client.rest.methods.removeMessageReaction(message.channel.id, message.id, this.emoji.identifier); } + + /** + * Fetch all the users that gave this reaction. Resolves with a collection of users, + * mapped by their IDs. + * @returns {Promise>} + */ + fetchUsers() { + const message = this.message; + return new Promise((resolve, reject) => { + message.client.rest.methods.getMessageReactionUsers(message.channel.id, message.id, this.emoji.identifier) + .then(users => { + this.users = new Collection(); + for (const rawUser of users) { + const user = this.message.client.dataManager.newUser(rawUser); + this.users.set(user.id, user); + } + this.count = this.users.size; + resolve(this.users); + }) + .catch(reject); + }); + } } module.exports = MessageReaction; diff --git a/test/random.js b/test/random.js index 1e6df7642..f3af346a7 100644 --- a/test/random.js +++ b/test/random.js @@ -187,3 +187,19 @@ client.on('messageReactionRemove', (reaction, user) => { if (reaction.message.channel.id !== '222086648706498562') return; reaction.message.channel.sendMessage(`${user.username} removed reaction ${reaction.emoji}, count is now ${reaction.count}`); }); + +client.on('message', m => { + if (m.content.startsWith('#reactions')) { + const mID = m.content.split(' ')[1]; + m.channel.fetchMessage(mID).then(rM => { + for (const reaction of rM.reactions.values()) { + reaction.fetchUsers().then(users => { + m.channel.sendMessage( + `The following gave that message ${reaction.emoji}:\n` + + `${users.map(u => u.username).map(t => `- ${t}`).join('\n')}` + ); + }); + } + }); + } +}); \ No newline at end of file