refactor(InteractionCollector): only keep Ids of objects (#6084)

Co-authored-by: monbrey <rsm999@uowmail.edu.au>
Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
Bluenix
2021-08-05 17:40:23 +02:00
committed by GitHub
parent 3eb41405f4
commit b639b6c653
2 changed files with 30 additions and 23 deletions

View File

@@ -14,7 +14,7 @@ const { InteractionTypes, MessageComponentTypes } = require('../util/Constants')
* @property {number} [max] The maximum total amount of interactions to collect * @property {number} [max] The maximum total amount of interactions to collect
* @property {number} [maxComponents] The maximum number of components to collect * @property {number} [maxComponents] The maximum number of components to collect
* @property {number} [maxUsers] The maximum number of users to interact * @property {number} [maxUsers] The maximum number of users to interact
* @property {Message} [message] The message to listen to interactions from * @property {Message|APIMessage} [message] The message to listen to interactions from
*/ */
/** /**
@@ -33,21 +33,28 @@ class InteractionCollector extends Collector {
/** /**
* The message from which to collect interactions, if provided * The message from which to collect interactions, if provided
* @type {?Message} * @type {?Snowflake}
*/ */
this.message = options.message ?? null; this.messageId = options.message?.id ?? null;
/** /**
* The channel from which to collect interactions, if provided * The channel from which to collect interactions, if provided
* @type {?TextBasedChannels} * @type {?Snowflake}
*/ */
this.channel = this.message?.channel ?? options.channel ?? null; this.channelId =
this.client.channels.resolveId(options.message?.channel) ??
options.message?.channel_id ??
this.client.channels.resolveId(options.channel);
/** /**
* The guild from which to collect interactions, if provided * The guild from which to collect interactions, if provided
* @type {?Guild} * @type {?Snowflake}
*/ */
this.guild = this.channel?.guild ?? options.guild ?? null; this.guildId =
this.client.guilds.resolveId(options.message?.guild) ??
options.message?.guild_id ??
this.client.guilds.resolveId(options.channel?.guild) ??
this.client.guilds.resolveId(options.guild);
/** /**
* The the type of interaction to collect * The the type of interaction to collect
@@ -82,17 +89,17 @@ class InteractionCollector extends Collector {
this.empty = this.empty.bind(this); this.empty = this.empty.bind(this);
this.client.incrementMaxListeners(); this.client.incrementMaxListeners();
if (this.message) { if (this.messageId) {
this._handleMessageDeletion = this._handleMessageDeletion.bind(this); this._handleMessageDeletion = this._handleMessageDeletion.bind(this);
this.client.on(Events.MESSAGE_DELETE, this._handleMessageDeletion); this.client.on(Events.MESSAGE_DELETE, this._handleMessageDeletion);
} }
if (this.channel) { if (this.channelId) {
this._handleChannelDeletion = this._handleChannelDeletion.bind(this); this._handleChannelDeletion = this._handleChannelDeletion.bind(this);
this.client.on(Events.CHANNEL_DELETE, this._handleChannelDeletion); this.client.on(Events.CHANNEL_DELETE, this._handleChannelDeletion);
} }
if (this.guild) { if (this.guildId) {
this._handleGuildDeletion = this._handleGuildDeletion.bind(this); this._handleGuildDeletion = this._handleGuildDeletion.bind(this);
this.client.on(Events.GUILD_DELETE, this._handleGuildDeletion); this.client.on(Events.GUILD_DELETE, this._handleGuildDeletion);
} }
@@ -127,9 +134,9 @@ class InteractionCollector extends Collector {
*/ */
if (this.interactionType && interaction.type !== this.interactionType) return null; if (this.interactionType && interaction.type !== this.interactionType) return null;
if (this.componentType && interaction.componentType !== this.componentType) return null; if (this.componentType && interaction.componentType !== this.componentType) return null;
if (this.message && interaction.message?.id !== this.message.id) return null; if (this.messageId && interaction.message?.id !== this.messageId) return null;
if (this.channel && interaction.channelId !== this.channel.id) return null; if (this.channelId && interaction.channelId !== this.channelId) return null;
if (this.guild && interaction.guildId !== this.guild.id) return null; if (this.guildId && interaction.guildId !== this.guildId) return null;
return interaction.id; return interaction.id;
} }
@@ -147,9 +154,9 @@ class InteractionCollector extends Collector {
*/ */
if (this.type && interaction.type !== this.type) return null; if (this.type && interaction.type !== this.type) return null;
if (this.componentType && interaction.componentType !== this.componentType) return null; if (this.componentType && interaction.componentType !== this.componentType) return null;
if (this.message && interaction.message?.id !== this.message.id) return null; if (this.messageId && interaction.message?.id !== this.messageId) return null;
if (this.channel && interaction.channelId !== this.channel.id) return null; if (this.channelId && interaction.channelId !== this.channelId) return null;
if (this.guild && interaction.guildId !== this.guild.id) return null; if (this.guildId && interaction.guildId !== this.guildId) return null;
return interaction.id; return interaction.id;
} }
@@ -183,7 +190,7 @@ class InteractionCollector extends Collector {
* @returns {void} * @returns {void}
*/ */
_handleMessageDeletion(message) { _handleMessageDeletion(message) {
if (message.id === this.message?.id) { if (message.id === this.messageId) {
this.stop('messageDelete'); this.stop('messageDelete');
} }
} }
@@ -195,7 +202,7 @@ class InteractionCollector extends Collector {
* @returns {void} * @returns {void}
*/ */
_handleChannelDeletion(channel) { _handleChannelDeletion(channel) {
if (channel.id === this.channel?.id) { if (channel.id === this.channelId) {
this.stop('channelDelete'); this.stop('channelDelete');
} }
} }
@@ -207,7 +214,7 @@ class InteractionCollector extends Collector {
* @returns {void} * @returns {void}
*/ */
_handleGuildDeletion(guild) { _handleGuildDeletion(guild) {
if (guild.id === this.guild?.id) { if (guild.id === this.guildId) {
this.stop('guildDelete'); this.stop('guildDelete');
} }
} }

8
typings/index.d.ts vendored
View File

@@ -984,12 +984,12 @@ export class InteractionCollector<T extends Interaction> extends Collector<Snowf
private _handleChannelDeletion(channel: GuildChannel): void; private _handleChannelDeletion(channel: GuildChannel): void;
private _handleGuildDeletion(guild: Guild): void; private _handleGuildDeletion(guild: Guild): void;
public channel: TextBasedChannels | null; public channelId: Snowflake | null;
public componentType: MessageComponentType | null; public componentType: MessageComponentType | null;
public readonly endReason: string | null; public readonly endReason: string | null;
public guild: Guild | null; public guildId: Snowflake | null;
public interactionType: InteractionType | null; public interactionType: InteractionType | null;
public message: Message | null; public messageId: Snowflake | null;
public options: InteractionCollectorOptions<T>; public options: InteractionCollectorOptions<T>;
public total: number; public total: number;
public users: Collection<Snowflake, User>; public users: Collection<Snowflake, User>;
@@ -3790,7 +3790,7 @@ export interface InteractionCollectorOptions<T extends Interaction> extends Coll
max?: number; max?: number;
maxComponents?: number; maxComponents?: number;
maxUsers?: number; maxUsers?: number;
message?: Message; message?: Message | APIMessage;
} }
export interface InteractionDeferOptions { export interface InteractionDeferOptions {