Clean up nearly all promises to utilise chaining, other small fixes

This commit is contained in:
Schuyler Cebulskie
2016-10-30 16:27:28 -04:00
parent 8306d50bd8
commit 60e0d507f0
10 changed files with 482 additions and 620 deletions

View File

@@ -1,54 +1,6 @@
const Collection = require('../util/Collection');
const Emoji = require('./Emoji');
/**
* Represents a limited emoji set used for both custom and unicode emojis. Custom emojis
* will use this class opposed to the Emoji class when the client doesn't know enough
* information about them.
*/
class ReactionEmoji {
constructor(reaction, name, id) {
/**
* The message reaction this emoji refers to
* @type {MessageReaction}
*/
this.reaction = reaction;
/**
* The name of this reaction emoji.
* @type {string}
*/
this.name = name;
/**
* The ID of this reaction emoji.
* @type {string}
*/
this.id = id;
}
/**
* The identifier of this emoji, used for message reactions
* @readonly
* @type {string}
*/
get identifier() {
if (this.id) {
return `${this.name}:${this.id}`;
}
return encodeURIComponent(this.name);
}
/**
* Creates the text required to form a graphical emoji on Discord.
* @example
* // send the emoji used in a reaction to the channel the reaction is part of
* reaction.message.channel.sendMessage(`The emoji used is ${reaction.emoji}`);
* @returns {string}
*/
toString() {
return this.id ? `<:${this.name}:${this.id}>` : this.name;
}
}
/**
* Represents a reaction to a message
*/
@@ -59,22 +11,26 @@ class MessageReaction {
* @type {Message}
*/
this.message = message;
/**
* Whether the client has given this reaction
* @type {boolean}
*/
this.me = me;
this._emoji = new ReactionEmoji(this, emoji.name, emoji.id);
/**
* The number of people that have given the same reaction.
* @type {number}
*/
this.count = count || 0;
/**
* The users that have given this reaction, mapped by their ID.
* @type {Collection<string, User>}
*/
this.users = new Collection();
this._emoji = new ReactionEmoji(this, emoji.name, emoji.id);
}
/**
@@ -83,9 +39,7 @@ class MessageReaction {
* @type {Emoji|ReactionEmoji}
*/
get emoji() {
if (this._emoji instanceof Emoji) {
return this._emoji;
}
if (this._emoji instanceof Emoji) return this._emoji;
// check to see if the emoji has become known to the client
if (this._emoji.id) {
const emojis = this.message.client.emojis;
@@ -106,11 +60,10 @@ class MessageReaction {
remove(user = this.message.client.user) {
const message = this.message;
user = this.message.client.resolver.resolveUserID(user);
if (!user) return Promise.reject('A UserIDResolvable is required (string, user, member, message, guild)');
if (!user) return Promise.reject('Couldn\'t resolve the user ID to remove from the reaction.');
return message.client.rest.methods.removeMessageReaction(
message.channel.id, message.id, this.emoji.identifier, user);
message.channel.id, message.id, this.emoji.identifier, user
);
}
/**
@@ -121,19 +74,66 @@ class MessageReaction {
*/
fetchUsers(limit = 100) {
const message = this.message;
return new Promise((resolve, reject) => {
message.client.rest.methods.getMessageReactionUsers(message.channel.id, message.id, this.emoji.identifier, limit)
.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);
}, reject);
return message.client.rest.methods.getMessageReactionUsers(
message.channel.id, message.id, this.emoji.identifier, limit
).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;
return users;
});
}
}
/**
* Represents a limited emoji set used for both custom and unicode emojis. Custom emojis
* will use this class opposed to the Emoji class when the client doesn't know enough
* information about them.
*/
class ReactionEmoji {
constructor(reaction, name, id) {
/**
* The message reaction this emoji refers to
* @type {MessageReaction}
*/
this.reaction = reaction;
/**
* The name of this reaction emoji.
* @type {string}
*/
this.name = name;
/**
* The ID of this reaction emoji.
* @type {string}
*/
this.id = id;
}
/**
* The identifier of this emoji, used for message reactions
* @readonly
* @type {string}
*/
get identifier() {
if (this.id) return `${this.name}:${this.id}`;
return encodeURIComponent(this.name);
}
/**
* Creates the text required to form a graphical emoji on Discord.
* @example
* // send the emoji used in a reaction to the channel the reaction is part of
* reaction.message.channel.sendMessage(`The emoji used is ${reaction.emoji}`);
* @returns {string}
*/
toString() {
return this.id ? `<:${this.name}:${this.id}>` : this.name;
}
}
module.exports = MessageReaction;