update collectors (#1616)

* start new collector stuff

* bugfixes

* remove pointless cleanup method

* rename methods, events, and options; remove extraneous methods,

* update doc ref
This commit is contained in:
Will Nelson
2017-07-26 01:10:35 -07:00
committed by Crawl
parent e29a3ec08b
commit 8bd7b82110
3 changed files with 130 additions and 56 deletions

View File

@@ -39,7 +39,27 @@ class ReactionCollector extends Collector {
*/
this.total = 0;
this.client.on('messageReactionAdd', this.listener);
this.empty = this.empty.bind(this);
this.client.on('messageReactionAdd', this.handleCollect);
this.client.on('messageReactionRemove', this.handleDispose);
this.client.on('messageReactionRemoveAll', this.empty);
this.once('end', () => {
this.client.removeListener('messageReactionAdd', this.handleCollect);
this.client.removeListener('messageReactionRemove', this.handleDispose);
this.client.removeListener('messageReactionRemoveAll', this.empty);
});
this.on('collect', (collected, reaction, user) => {
this.total++;
this.users.set(user.id, user);
});
this.on('dispose', (disposed, reaction, user) => {
this.total--;
if (!this.collected.some(r => r.users.has(user.id))) this.users.delete(user.id);
});
}
/**
@@ -48,35 +68,47 @@ class ReactionCollector extends Collector {
* @returns {?{key: Snowflake, value: MessageReaction}} Reaction data to collect
* @private
*/
handle(reaction) {
collect(reaction) {
if (reaction.message.id !== this.message.id) return null;
return {
key: reaction.emoji.id || reaction.emoji.name,
key: ReactionCollector.key(reaction),
value: reaction,
};
}
/**
* Check after collection to see if the collector is done.
* @param {MessageReaction} reaction The reaction that was collected
* @param {User} user The user that reacted
* @returns {?string} Reason to end the collector, if any
* @private
* Handle a reaction deletion for possible disposal.
* @param {MessageReaction} reaction The reaction to possibly dispose
* @returns {?Snowflake|string} The reaction key
*/
postCheck(reaction, user) {
this.users.set(user.id, user);
if (this.options.max && ++this.total >= this.options.max) return 'limit';
dispose(reaction) {
return reaction.message.id === this.message.id && !reaction.count ? ReactionCollector.key(reaction) : null;
}
/**
* Empty this reaction collector.
*/
empty() {
this.total = 0;
this.collected.clear();
this.users.clear();
this.checkEnd();
}
endReason() {
if (this.options.max && this.total >= this.options.max) return 'limit';
if (this.options.maxEmojis && this.collected.size >= this.options.maxEmojis) return 'emojiLimit';
if (this.options.maxUsers && this.users.size >= this.options.maxUsers) return 'userLimit';
return null;
}
/**
* Remove event listeners.
* @private
* Get the collector key for a reaction.
* @param {MessageReaction} reaction The message reaction to get the key for
* @returns {Snowflake|string} The emoji ID (if custom) or the emoji name (if native; will be unicode)
*/
cleanup() {
this.client.removeListener('messageReactionAdd', this.listener);
static key(reaction) {
return reaction.emoji.id || reaction.emoji.name;
}
}