Add message reaction collectors & abstract collectors (#1335)

* message reaction collectors

* docs cleanup

* abstraction

* remove pointless method

* rename reaction collector creator method

* docs and stuff

* fix docs & build

* backwards compatibility, fix docs

* fix docs

* remove deprecated comments

* betterer docs again

* Fix documentation

* Fix Alias to not break depreciated code
This commit is contained in:
Will Nelson
2017-04-19 09:52:40 -07:00
committed by Crawl
parent 8475a4abee
commit ca34c43ba0
10 changed files with 345 additions and 135 deletions

View File

@@ -0,0 +1,64 @@
const Collector = require('./interfaces/Collector');
const Collection = require('../util/Collection');
/**
* @typedef {CollectorOptions} ReactionCollectorOptions
* @property {number} max The maximum total amount of reactions to collect.
* @property {number} maxEmojis The maximum number of emojis to collect.
* @property {number} maxUsers The maximum number of users to react.
*/
/**
* Collects reactions on messages.
* @implements {Collector}
*/
class ReactionCollector extends Collector {
/**
* @param {Message} message The message upon which to collect reactions.
* @param {CollectorFilter} filter The filter to apply to this collector.
* @param {ReactionCollectorOptions} [options={}] The options to apply to this collector.
*/
constructor(message, filter, options = {}) {
super(message.client, filter, options);
/**
* @type {Message} message The message.
*/
this.message = message;
/**
* @type {Collection} users Users which have reacted.
*/
this.users = new Collection();
/**
* @type {number} total Total number of reactions collected.
*/
this.total = 0;
this.client.on('messageReactionAdd', this.listener);
}
handle(reaction) {
if (reaction.message.id !== this.message.id) return null;
return {
key: reaction.emoji.id || reaction.emoji.name,
value: reaction,
};
}
postCheck(reaction, user) {
this.users.set(user.id, user);
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;
}
cleanup() {
this.client.removeListener('messageReactionAdd', this.listener);
}
}
module.exports = ReactionCollector;