Add reaction fetching of users

This commit is contained in:
Amish Shah
2016-10-27 16:58:06 +01:00
parent 8e505ed349
commit dd9c291508
3 changed files with 45 additions and 0 deletions

View File

@@ -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;

View File

@@ -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<Collection<string, User>>}
*/
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;

View File

@@ -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')}`
);
});
}
});
}
});