From f7a80d0aa56b0b7c415df15a0ff61d778036053c Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Wed, 27 Oct 2021 11:49:17 +0100 Subject: [PATCH 1/4] fix: handle bulk deletes --- src/structures/InteractionCollector.js | 11 +++++++++-- src/structures/MessageCollector.js | 3 ++- src/structures/ReactionCollector.js | 12 ++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/structures/InteractionCollector.js b/src/structures/InteractionCollector.js index eddc69a4da19..1c07735a4ac6 100644 --- a/src/structures/InteractionCollector.js +++ b/src/structures/InteractionCollector.js @@ -19,9 +19,10 @@ 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} | + * {@link Client#event:messageDeleteBulk messageDeleteBulk}), * channel ({@link Client#event:channelDelete channelDelete}), or - * guild ({@link Client#event:guildDelete guildDelete}) is deleted. + * guild ({@link Client#event:guildDelete guildDelete}) are deleted. * @extends {Collector} */ class InteractionCollector extends Collector { @@ -90,9 +91,14 @@ class InteractionCollector extends Collector { this.empty = this.empty.bind(this); this.client.incrementMaxListeners(); + const bulkDeleteListener = messages => { + for (const deletedMessage of messages.values()) this._handleMessageDeletion(deletedMessage); + }; + 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) { @@ -110,6 +116,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(); diff --git a/src/structures/MessageCollector.js b/src/structures/MessageCollector.js index faf5b527db41..3206884a267d 100644 --- a/src/structures/MessageCollector.js +++ b/src/structures/MessageCollector.js @@ -11,7 +11,8 @@ 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}) or + * guild ({@link Client#event:guildDelete guildDelete}) are deleted. * @extends {Collector} */ class MessageCollector extends Collector { diff --git a/src/structures/ReactionCollector.js b/src/structures/ReactionCollector.js index f581cea75dbc..b4461f5f972e 100644 --- a/src/structures/ReactionCollector.js +++ b/src/structures/ReactionCollector.js @@ -13,8 +13,10 @@ 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} | + * {@link Client#event:messageDeleteBulk messageDeleteBulk}), + * channel ({@link Client#event:channelDelete channelDelete}), or + * guild ({@link Client#event:guildDelete guildDelete}) are deleted. * @extends {Collector} */ class ReactionCollector extends Collector { @@ -48,11 +50,16 @@ class ReactionCollector extends Collector { this._handleGuildDeletion = this._handleGuildDeletion.bind(this); this._handleMessageDeletion = this._handleMessageDeletion.bind(this); + const bulkDeleteListener = messages => { + for (const deletedMessage of messages.values()) this._handleMessageDeletion(deletedMessage); + }; + 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.GUILD_DELETE, this._handleGuildDeletion); @@ -61,6 +68,7 @@ class ReactionCollector extends Collector { 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.GUILD_DELETE, this._handleGuildDeletion); this.client.decrementMaxListeners(); From 264cb0615b6a4c7e7695ac8c39525bb836aaca8c Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Wed, 27 Oct 2021 16:23:35 +0100 Subject: [PATCH 2/4] docs: no `|` and grammar stuff --- src/structures/InteractionCollector.js | 4 ++-- src/structures/MessageCollector.js | 2 +- src/structures/ReactionCollector.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/structures/InteractionCollector.js b/src/structures/InteractionCollector.js index 1c07735a4ac6..72107db058b1 100644 --- a/src/structures/InteractionCollector.js +++ b/src/structures/InteractionCollector.js @@ -19,10 +19,10 @@ 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}) are deleted. + * guild ({@link Client#event:guildDelete guildDelete}) is deleted. * @extends {Collector} */ class InteractionCollector extends Collector { diff --git a/src/structures/MessageCollector.js b/src/structures/MessageCollector.js index 3206884a267d..769520417c83 100644 --- a/src/structures/MessageCollector.js +++ b/src/structures/MessageCollector.js @@ -12,7 +12,7 @@ const { Events } = require('../util/Constants'); /** * Collects messages on a channel. * Will automatically stop if the channel ({@link Client#event:channelDelete channelDelete}) or - * guild ({@link Client#event:guildDelete guildDelete}) are deleted. + * guild ({@link Client#event:guildDelete guildDelete}) is deleted. * @extends {Collector} */ class MessageCollector extends Collector { diff --git a/src/structures/ReactionCollector.js b/src/structures/ReactionCollector.js index b4461f5f972e..f4353e0fd689 100644 --- a/src/structures/ReactionCollector.js +++ b/src/structures/ReactionCollector.js @@ -13,10 +13,10 @@ const { Events } = require('../util/Constants'); /** * Collects reactions on messages. - * 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}) are deleted. + * guild ({@link Client#event:guildDelete guildDelete}) is deleted. * @extends {Collector} */ class ReactionCollector extends Collector { From 29fb4f6ace44bcbae3e7b635a50c655a82804e75 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Wed, 27 Oct 2021 17:06:13 +0100 Subject: [PATCH 3/4] refactor: check in Collection --- src/structures/InteractionCollector.js | 2 +- src/structures/ReactionCollector.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/InteractionCollector.js b/src/structures/InteractionCollector.js index 72107db058b1..9524fbfe1047 100644 --- a/src/structures/InteractionCollector.js +++ b/src/structures/InteractionCollector.js @@ -92,7 +92,7 @@ class InteractionCollector extends Collector { this.client.incrementMaxListeners(); const bulkDeleteListener = messages => { - for (const deletedMessage of messages.values()) this._handleMessageDeletion(deletedMessage); + if (messages.has(this.messageId)) this.stop('messageDelete'); }; if (this.messageId) { diff --git a/src/structures/ReactionCollector.js b/src/structures/ReactionCollector.js index f4353e0fd689..478fddc9e5be 100644 --- a/src/structures/ReactionCollector.js +++ b/src/structures/ReactionCollector.js @@ -51,7 +51,7 @@ class ReactionCollector extends Collector { this._handleMessageDeletion = this._handleMessageDeletion.bind(this); const bulkDeleteListener = messages => { - for (const deletedMessage of messages.values()) this._handleMessageDeletion(deletedMessage); + if (messages.has(this.message.id)) this.stop('messageDelete'); }; this.client.incrementMaxListeners(); From 3aa9b40ae38285c84181d3e04cfc6fbd26db319f Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Wed, 27 Oct 2021 23:34:08 +0100 Subject: [PATCH 4/4] fix: stop on thread deletion --- src/structures/MessageCollector.js | 21 +++++++++++++++++++-- src/structures/ReactionCollector.js | 20 ++++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/structures/MessageCollector.js b/src/structures/MessageCollector.js index 769520417c83..34deaff4b4c1 100644 --- a/src/structures/MessageCollector.js +++ b/src/structures/MessageCollector.js @@ -11,7 +11,8 @@ const { Events } = require('../util/Constants'); /** * Collects messages on a channel. - * Will automatically stop if the channel ({@link Client#event:channelDelete channelDelete}) or + * 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} */ @@ -39,7 +40,9 @@ 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(); @@ -47,6 +50,7 @@ class MessageCollector extends Collector { 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', () => { @@ -54,6 +58,7 @@ class MessageCollector extends Collector { 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(); }); @@ -108,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 diff --git a/src/structures/ReactionCollector.js b/src/structures/ReactionCollector.js index 478fddc9e5be..c003272244c4 100644 --- a/src/structures/ReactionCollector.js +++ b/src/structures/ReactionCollector.js @@ -15,7 +15,8 @@ const { Events } = require('../util/Constants'); * Collects reactions on messages. * Will automatically stop if the message ({@link Client#event:messageDelete messageDelete} or * {@link Client#event:messageDeleteBulk messageDeleteBulk}), - * channel ({@link Client#event:channelDelete channelDelete}), or + * channel ({@link Client#event:channelDelete channelDelete}), + * thread ({@link Client#event:threadDelete threadDelete}), or * guild ({@link Client#event:guildDelete guildDelete}) is deleted. * @extends {Collector} */ @@ -47,6 +48,7 @@ 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); @@ -61,6 +63,7 @@ class ReactionCollector extends Collector { 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', () => { @@ -70,6 +73,7 @@ class ReactionCollector extends Collector { 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(); }); @@ -183,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