mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
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:
@@ -11,6 +11,7 @@ const { Events } = require('../util/Constants');
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Collects messages on a channel.
|
* Collects messages on a channel.
|
||||||
|
* Will automatically stop if the channel (`'channelDelete'`) or guild (`'guildDelete'`) are deleted.
|
||||||
* @extends {Collector}
|
* @extends {Collector}
|
||||||
*/
|
*/
|
||||||
class MessageCollector extends Collector {
|
class MessageCollector extends Collector {
|
||||||
@@ -38,16 +39,22 @@ class MessageCollector extends Collector {
|
|||||||
const bulkDeleteListener = (messages => {
|
const bulkDeleteListener = (messages => {
|
||||||
for (const message of messages.values()) this.handleDispose(message);
|
for (const message of messages.values()) this.handleDispose(message);
|
||||||
}).bind(this);
|
}).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);
|
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_CREATE, this.handleCollect);
|
||||||
this.client.on(Events.MESSAGE_DELETE, this.handleDispose);
|
this.client.on(Events.MESSAGE_DELETE, this.handleDispose);
|
||||||
this.client.on(Events.MESSAGE_BULK_DELETE, bulkDeleteListener);
|
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.once('end', () => {
|
||||||
this.client.removeListener(Events.MESSAGE_CREATE, this.handleCollect);
|
this.client.removeListener(Events.MESSAGE_CREATE, this.handleCollect);
|
||||||
this.client.removeListener(Events.MESSAGE_DELETE, this.handleDispose);
|
this.client.removeListener(Events.MESSAGE_DELETE, this.handleDispose);
|
||||||
this.client.removeListener(Events.MESSAGE_BULK_DELETE, bulkDeleteListener);
|
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);
|
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';
|
if (this.options.maxProcessed && this.received === this.options.maxProcessed) return 'processedLimit';
|
||||||
return null;
|
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;
|
module.exports = MessageCollector;
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ const { Events } = require('../util/Constants');
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Collects reactions on messages.
|
* Collects reactions on messages.
|
||||||
|
* Will automatically stop if the message (`'messageDelete'`),
|
||||||
|
* channel (`'channelDelete'`), or guild (`'guildDelete'`) are deleted.
|
||||||
* @extends {Collector}
|
* @extends {Collector}
|
||||||
*/
|
*/
|
||||||
class ReactionCollector extends Collector {
|
class ReactionCollector extends Collector {
|
||||||
@@ -43,16 +45,25 @@ class ReactionCollector extends Collector {
|
|||||||
this.total = 0;
|
this.total = 0;
|
||||||
|
|
||||||
this.empty = this.empty.bind(this);
|
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);
|
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_ADD, this.handleCollect);
|
||||||
this.client.on(Events.MESSAGE_REACTION_REMOVE, this.handleDispose);
|
this.client.on(Events.MESSAGE_REACTION_REMOVE, this.handleDispose);
|
||||||
this.client.on(Events.MESSAGE_REACTION_REMOVE_ALL, this.empty);
|
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.once('end', () => {
|
||||||
this.client.removeListener(Events.MESSAGE_REACTION_ADD, this.handleCollect);
|
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, this.handleDispose);
|
||||||
this.client.removeListener(Events.MESSAGE_REACTION_REMOVE_ALL, this.empty);
|
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);
|
if (this.client.getMaxListeners() !== 0) this.client.setMaxListeners(this.client.getMaxListeners() - 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -131,6 +142,42 @@ class ReactionCollector extends Collector {
|
|||||||
return null;
|
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.
|
* Gets the collector key for a reaction.
|
||||||
* @param {MessageReaction} reaction The message reaction to get the key for
|
* @param {MessageReaction} reaction The message reaction to get the key for
|
||||||
|
|||||||
7
typings/index.d.ts
vendored
7
typings/index.d.ts
vendored
@@ -1023,6 +1023,9 @@ declare module 'discord.js' {
|
|||||||
|
|
||||||
export class MessageCollector extends Collector<Snowflake, Message> {
|
export class MessageCollector extends Collector<Snowflake, Message> {
|
||||||
constructor(channel: TextChannel | DMChannel, filter: CollectorFilter, options?: MessageCollectorOptions);
|
constructor(channel: TextChannel | DMChannel, filter: CollectorFilter, options?: MessageCollectorOptions);
|
||||||
|
private _handleChannelDeletion(channel: GuildChannel): void;
|
||||||
|
private _handleGuildDeletion(guild: Guild): void;
|
||||||
|
|
||||||
public channel: Channel;
|
public channel: Channel;
|
||||||
public options: MessageCollectorOptions;
|
public options: MessageCollectorOptions;
|
||||||
public received: number;
|
public received: number;
|
||||||
@@ -1153,6 +1156,10 @@ declare module 'discord.js' {
|
|||||||
|
|
||||||
export class ReactionCollector extends Collector<Snowflake, MessageReaction> {
|
export class ReactionCollector extends Collector<Snowflake, MessageReaction> {
|
||||||
constructor(message: Message, filter: CollectorFilter, options?: ReactionCollectorOptions);
|
constructor(message: Message, filter: CollectorFilter, options?: ReactionCollectorOptions);
|
||||||
|
private _handleChannelDeletion(channel: GuildChannel): void;
|
||||||
|
private _handleGuildDeletion(guild: Guild): void;
|
||||||
|
private _handleMessageDeletion(message: Message): void;
|
||||||
|
|
||||||
public message: Message;
|
public message: Message;
|
||||||
public options: ReactionCollectorOptions;
|
public options: ReactionCollectorOptions;
|
||||||
public total: number;
|
public total: number;
|
||||||
|
|||||||
Reference in New Issue
Block a user