mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user