Skip to content

Commit

Permalink
fix: Handle message bulk delete and thread delete in collectors (#6902)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Oct 29, 2021
1 parent 14716df commit d6685b1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/structures/InteractionCollector.js
Expand Up @@ -19,7 +19,8 @@ const { InteractionTypes, MessageComponentTypes } = require('../util/Constants')

/**
* Collects interactions.
* Will automatically stop if the message ({@link Client#event:messageDelete messageDelete}),
* Will automatically stop if the message ({@link Client#event:messageDelete messageDelete} or
* {@link Client#event:messageDeleteBulk messageDeleteBulk}),
* channel ({@link Client#event:channelDelete channelDelete}), or
* guild ({@link Client#event:guildDelete guildDelete}) is deleted.
* <info>Interaction collectors that do not specify `time` or `idle` may be prone to always running.
Expand Down Expand Up @@ -92,9 +93,14 @@ class InteractionCollector extends Collector {
this.empty = this.empty.bind(this);
this.client.incrementMaxListeners();

const bulkDeleteListener = messages => {
if (messages.has(this.messageId)) this.stop('messageDelete');
};

if (this.messageId) {
this._handleMessageDeletion = this._handleMessageDeletion.bind(this);
this.client.on(Events.MESSAGE_DELETE, this._handleMessageDeletion);
this.client.on(Events.MESSAGE_BULK_DELETE, bulkDeleteListener);
}

if (this.channelId) {
Expand All @@ -112,6 +118,7 @@ class InteractionCollector extends Collector {
this.once('end', () => {
this.client.removeListener(Events.INTERACTION_CREATE, this.handleCollect);
this.client.removeListener(Events.MESSAGE_DELETE, this._handleMessageDeletion);
this.client.removeListener(Events.MESSAGE_BULK_DELETE, bulkDeleteListener);
this.client.removeListener(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.removeListener(Events.GUILD_DELETE, this._handleGuildDeletion);
this.client.decrementMaxListeners();
Expand Down
22 changes: 20 additions & 2 deletions src/structures/MessageCollector.js
Expand Up @@ -11,7 +11,9 @@ const { Events } = require('../util/Constants');

/**
* Collects messages on a channel.
* Will automatically stop if the channel (`'channelDelete'`) or guild (`'guildDelete'`) are deleted.
* Will automatically stop if the channel ({@link Client#event:channelDelete channelDelete}),
* thread ({@link Client#event:threadDelete threadDelete}), or
* guild ({@link Client#event:guildDelete guildDelete}) is deleted.
* @extends {Collector}
*/
class MessageCollector extends Collector {
Expand All @@ -38,21 +40,25 @@ class MessageCollector extends Collector {
const bulkDeleteListener = messages => {
for (const message of messages.values()) this.handleDispose(message);
};

this._handleChannelDeletion = this._handleChannelDeletion.bind(this);
this._handleThreadDeletion = this._handleThreadDeletion.bind(this);
this._handleGuildDeletion = this._handleGuildDeletion.bind(this);

this.client.incrementMaxListeners();
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.THREAD_DELETE, this._handleThreadDeletion);
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.THREAD_DELETE, this._handleThreadDeletion);
this.client.removeListener(Events.GUILD_DELETE, this._handleGuildDeletion);
this.client.decrementMaxListeners();
});
Expand Down Expand Up @@ -107,11 +113,23 @@ class MessageCollector extends Collector {
* @returns {void}
*/
_handleChannelDeletion(channel) {
if (channel.id === this.channel.id) {
if (channel.id === this.channel.id || channel.id === this.channel.parentId) {
this.stop('channelDelete');
}
}

/**
* Handles checking if the thread has been deleted, and if so, stops the collector with the reason 'threadDelete'.
* @private
* @param {ThreadChannel} thread The thread that was deleted
* @returns {void}
*/
_handleThreadDeletion(thread) {
if (thread.id === this.channel.id) {
this.stop('threadDelete');
}
}

/**
* Handles checking if the guild has been deleted, and if so, stops the collector with the reason 'guildDelete'.
* @private
Expand Down
30 changes: 27 additions & 3 deletions src/structures/ReactionCollector.js
Expand Up @@ -13,8 +13,11 @@ const { Events } = require('../util/Constants');

/**
* Collects reactions on messages.
* Will automatically stop if the message (`'messageDelete'`),
* channel (`'channelDelete'`), or guild (`'guildDelete'`) are deleted.
* Will automatically stop if the message ({@link Client#event:messageDelete messageDelete} or
* {@link Client#event:messageDeleteBulk messageDeleteBulk}),
* channel ({@link Client#event:channelDelete channelDelete}),
* thread ({@link Client#event:threadDelete threadDelete}), or
* guild ({@link Client#event:guildDelete guildDelete}) is deleted.
* @extends {Collector}
*/
class ReactionCollector extends Collector {
Expand Down Expand Up @@ -45,23 +48,32 @@ class ReactionCollector extends Collector {

this.empty = this.empty.bind(this);
this._handleChannelDeletion = this._handleChannelDeletion.bind(this);
this._handleThreadDeletion = this._handleThreadDeletion.bind(this);
this._handleGuildDeletion = this._handleGuildDeletion.bind(this);
this._handleMessageDeletion = this._handleMessageDeletion.bind(this);

const bulkDeleteListener = messages => {
if (messages.has(this.message.id)) this.stop('messageDelete');
};

this.client.incrementMaxListeners();
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.MESSAGE_BULK_DELETE, bulkDeleteListener);
this.client.on(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.on(Events.THREAD_DELETE, this._handleThreadDeletion);
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.MESSAGE_BULK_DELETE, bulkDeleteListener);
this.client.removeListener(Events.CHANNEL_DELETE, this._handleChannelDeletion);
this.client.removeListener(Events.THREAD_DELETE, this._handleThreadDeletion);
this.client.removeListener(Events.GUILD_DELETE, this._handleGuildDeletion);
this.client.decrementMaxListeners();
});
Expand Down Expand Up @@ -175,11 +187,23 @@ class ReactionCollector extends Collector {
* @returns {void}
*/
_handleChannelDeletion(channel) {
if (channel.id === this.message.channelId) {
if (channel.id === this.message.channelId || channel.id === this.message.channel.parentId) {
this.stop('channelDelete');
}
}

/**
* Handles checking if the thread has been deleted, and if so, stops the collector with the reason 'threadDelete'.
* @private
* @param {ThreadChannel} thread The thread that was deleted
* @returns {void}
*/
_handleThreadDeletion(thread) {
if (thread.id === this.message.channelId) {
this.stop('threadDelete');
}
}

/**
* Handles checking if the guild has been deleted, and if so, stops the collector with the reason 'guildDelete'.
* @private
Expand Down

0 comments on commit d6685b1

Please sign in to comment.