feat(Collectors): make collectors auto-stop when relevant structures are deleted (#3632)

* Collectors: make Collectors automatically stop when Channel, Guild, or Message are deleted.

* fix potential error with DM collectors

* Message collectors dont have a `this.message` you dummy

* Fix(various): nitpicks, documentation, typings, and stray error

* Pleasing mr tslint

* fix: typings

* Grammatical fixes

Co-Authored-By: SpaceEEC <spaceeec@yahoo.com>

* Fixing the linting after space's suggestions

* docs(ReactionCollector): remove whitespace

Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
BorgerKing
2020-01-19 05:24:55 -05:00
committed by SpaceEEC
parent 69c79a4136
commit cbb8db3058
3 changed files with 85 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ const { Events } = require('../util/Constants');
/**
* Collects messages on a channel.
* Will automatically stop if the channel (`'channelDelete'`) or guild (`'guildDelete'`) are deleted.
* @extends {Collector}
*/
class MessageCollector extends Collector {
@@ -38,16 +39,22 @@ class MessageCollector extends Collector {
const bulkDeleteListener = (messages => {
for (const message of messages.values()) this.handleDispose(message);
}).bind(this);
this._handleChannelDeletion = this._handleChannelDeletion.bind(this);
this._handleGuildDeletion = this._handleGuildDeletion.bind(this);
if (this.client.getMaxListeners() !== 0) this.client.setMaxListeners(this.client.getMaxListeners() + 1);
this.client.on(Events.MESSAGE_CREATE, this.handleCollect);
this.client.on(Events.MESSAGE_DELETE, this.handleDispose);
this.client.on(Events.MESSAGE_BULK_DELETE, bulkDeleteListener);
this.client.on(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.on(Events.GUILD_DELETE, this._handleGuildDeletion);
this.once('end', () => {
this.client.removeListener(Events.MESSAGE_CREATE, this.handleCollect);
this.client.removeListener(Events.MESSAGE_DELETE, this.handleDispose);
this.client.removeListener(Events.MESSAGE_BULK_DELETE, bulkDeleteListener);
this.client.removeListener(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.removeListener(Events.GUILD_DELETE, this._handleGuildDeletion);
if (this.client.getMaxListeners() !== 0) this.client.setMaxListeners(this.client.getMaxListeners() - 1);
});
}
@@ -93,6 +100,30 @@ class MessageCollector extends Collector {
if (this.options.maxProcessed && this.received === this.options.maxProcessed) return 'processedLimit';
return null;
}
/**
* Handles checking if the channel has been deleted, and if so, stops the collector with the reason 'channelDelete'.
* @private
* @param {GuildChannel} channel The channel that was deleted
* @returns {void}
*/
_handleChannelDeletion(channel) {
if (channel.id === this.channel.id) {
this.stop('channelDelete');
}
}
/**
* Handles checking if the guild has been deleted, and if so, stops the collector with the reason 'guildDelete'.
* @private
* @param {Guild} guild The guild that was deleted
* @returns {void}
*/
_handleGuildDeletion(guild) {
if (this.channel.guild && guild.id === this.channel.guild.id) {
this.stop('guildDelete');
}
}
}
module.exports = MessageCollector;

View File

@@ -13,6 +13,8 @@ const { Events } = require('../util/Constants');
/**
* Collects reactions on messages.
* Will automatically stop if the message (`'messageDelete'`),
* channel (`'channelDelete'`), or guild (`'guildDelete'`) are deleted.
* @extends {Collector}
*/
class ReactionCollector extends Collector {
@@ -43,16 +45,25 @@ class ReactionCollector extends Collector {
this.total = 0;
this.empty = this.empty.bind(this);
this._handleChannelDeletion = this._handleChannelDeletion.bind(this);
this._handleGuildDeletion = this._handleGuildDeletion.bind(this);
this._handleMessageDeletion = this._handleMessageDeletion.bind(this);
if (this.client.getMaxListeners() !== 0) this.client.setMaxListeners(this.client.getMaxListeners() + 1);
this.client.on(Events.MESSAGE_REACTION_ADD, this.handleCollect);
this.client.on(Events.MESSAGE_REACTION_REMOVE, this.handleDispose);
this.client.on(Events.MESSAGE_REACTION_REMOVE_ALL, this.empty);
this.client.on(Events.MESSAGE_DELETE, this._handleMessageDeletion);
this.client.on(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.on(Events.GUILD_DELETE, this._handleGuildDeletion);
this.once('end', () => {
this.client.removeListener(Events.MESSAGE_REACTION_ADD, this.handleCollect);
this.client.removeListener(Events.MESSAGE_REACTION_REMOVE, this.handleDispose);
this.client.removeListener(Events.MESSAGE_REACTION_REMOVE_ALL, this.empty);
this.client.removeListener(Events.MESSAGE_DELETE, this._handleMessageDeletion);
this.client.removeListener(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.removeListener(Events.GUILD_DELETE, this._handleGuildDeletion);
if (this.client.getMaxListeners() !== 0) this.client.setMaxListeners(this.client.getMaxListeners() - 1);
});
@@ -131,6 +142,42 @@ class ReactionCollector extends Collector {
return null;
}
/**
* Handles checking if the message has been deleted, and if so, stops the collector with the reason 'messageDelete'.
* @private
* @param {Message} message The message that was deleted
* @returns {void}
*/
_handleMessageDeletion(message) {
if (message.id === this.message.id) {
this.stop('messageDelete');
}
}
/**
* Handles checking if the channel has been deleted, and if so, stops the collector with the reason 'channelDelete'.
* @private
* @param {GuildChannel} channel The channel that was deleted
* @returns {void}
*/
_handleChannelDeletion(channel) {
if (channel.id === this.message.channel.id) {
this.stop('channelDelete');
}
}
/**
* Handles checking if the guild has been deleted, and if so, stops the collector with the reason 'guildDelete'.
* @private
* @param {Guild} guild The guild that was deleted
* @returns {void}
*/
_handleGuildDeletion(guild) {
if (this.message.guild && guild.id === this.message.guild.id) {
this.stop('guildDelete');
}
}
/**
* Gets the collector key for a reaction.
* @param {MessageReaction} reaction The message reaction to get the key for