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

@@ -31,16 +31,28 @@ class MessageCollector extends Collector {
*/
this.received = 0;
this.client.on('message', this.listener);
const bulkDeleteListener = (messages => {
for (const message of messages.values()) this.handleDispose(message);
}).bind(this);
this.client.on('message', this.handleCollect);
this.client.on('messageDelete', this.handleDispose);
this.client.on('messageDeleteBulk', bulkDeleteListener);
this.once('end', () => {
this.client.removeListener('message', this.handleCollect);
this.client.removeListener('messageDelete', this.handleDispose);
this.client.removeListener('messageDeleteBulk', bulkDeleteListener);
});
}
/**
* Handle an incoming message for possible collection.
* Handle a message for possible collection.
* @param {Message} message The message that could be collected
* @returns {?{key: Snowflake, value: Message}} Message data to collect
* @private
*/
handle(message) {
collect(message) {
if (message.channel.id !== this.channel.id) return null;
this.received++;
return {
@@ -50,23 +62,24 @@ class MessageCollector extends Collector {
}
/**
* Check after collection to see if the collector is done.
* Handle a message for possible disposal.
* @param {Message} message The message that could be disposed
* @returns {?string} The message ID.
*/
dispose(message) {
return message.channel.id === this.channel.id ? message.id : null;
}
/**
* Check after un/collection to see if the collector is done.
* @returns {?string} Reason to end the collector, if any
* @private
*/
postCheck() {
endReason() {
if (this.options.max && this.collected.size >= this.options.max) return 'limit';
if (this.options.maxProcessed && this.received === this.options.maxProcessed) return 'processedLimit';
return null;
}
/**
* Removes event listeners.
* @private
*/
cleanup() {
this.client.removeListener('message', this.listener);
}
}
module.exports = MessageCollector;