Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle message bulk delete and thread delete in collectors #6902

Merged
merged 4 commits into from Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
* @extends {Collector}
Expand Down Expand Up @@ -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);
Jiralite marked this conversation as resolved.
Show resolved Hide resolved
};

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 @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/structures/MessageCollector.js
Expand Up @@ -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}) is deleted.
* @extends {Collector}
*/
class MessageCollector extends Collector {
Expand Down
12 changes: 10 additions & 2 deletions src/structures/ReactionCollector.js
Expand Up @@ -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} or
* {@link Client#event:messageDeleteBulk messageDeleteBulk}),
* channel ({@link Client#event:channelDelete channelDelete}), or
* guild ({@link Client#event:guildDelete guildDelete}) is deleted.
* @extends {Collector}
*/
class ReactionCollector extends Collector {
Expand Down Expand Up @@ -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);
Jiralite marked this conversation as resolved.
Show resolved Hide resolved
};

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);

Expand All @@ -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();
Expand Down