From ca5cf9c1272bb2d62eacc0c82d7593dc087d874a Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas Date: Mon, 28 Jun 2021 21:24:10 +0300 Subject: [PATCH 1/3] feat(Interaction): add guild guard --- src/structures/CommandInteraction.js | 9 +++++++++ src/structures/Interaction.js | 8 ++++++++ typings/index.d.ts | 2 ++ 3 files changed, 19 insertions(+) diff --git a/src/structures/CommandInteraction.js b/src/structures/CommandInteraction.js index 2ceafc6ae4eb..6ffad2f14b5e 100644 --- a/src/structures/CommandInteraction.js +++ b/src/structures/CommandInteraction.js @@ -1,5 +1,6 @@ 'use strict'; +const GuildChannel = require('./GuildChannel'); const Interaction = require('./Interaction'); const InteractionWebhook = require('./InteractionWebhook'); const InteractionResponses = require('./interfaces/InteractionResponses'); @@ -80,6 +81,14 @@ class CommandInteraction extends Interaction { return this.guild?.commands.cache.get(id) ?? this.client.application.commands.cache.get(id) ?? null; } + /** + * Indicates whether this interaction is received from a guild. + * @returns {boolean} + */ + inGuild() { + return this.guild && this.guildID && this.member && this.channel instanceof GuildChannel; + } + /** * Represents an option of a received command interaction. * @typedef {Object} CommandInteractionOption diff --git a/src/structures/Interaction.js b/src/structures/Interaction.js index 189b3de03ec3..74ecbd855b2c 100644 --- a/src/structures/Interaction.js +++ b/src/structures/Interaction.js @@ -105,6 +105,14 @@ class Interaction extends Base { return this.client.guilds.cache.get(this.guildID) ?? null; } + /** + * Indicates whether this interaction is received from a guild. + * @returns {boolean} + */ + inGuild() { + return this.guild && this.guildID && this.member; + } + /** * Indicates whether this interaction is a command interaction. * @returns {boolean} diff --git a/typings/index.d.ts b/typings/index.d.ts index fdabb0426d7f..938a63ef18d5 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -537,6 +537,7 @@ declare module 'discord.js' { public options: Collection; public replied: boolean; public webhook: InteractionWebhook; + public inGuild(): boolean; public defer(options?: InteractionDeferOptions): Promise; public deleteReply(): Promise; public editReply(options: string | MessagePayload | WebhookEditMessageOptions): Promise; @@ -1191,6 +1192,7 @@ declare module 'discord.js' { public type: InteractionType; public user: User; public version: number; + public inGuild(): boolean; public isButton(): this is ButtonInteraction; public isCommand(): this is CommandInteraction; public isMessageComponent(): this is MessageComponentInteraction; From c99453dd32f5ba36a1fe5e6832d1720c48fb092b Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas Date: Tue, 29 Jun 2021 02:21:38 +0300 Subject: [PATCH 2/3] fix: remove possibly uncached getters & duplicate method --- src/structures/CommandInteraction.js | 9 --------- src/structures/Interaction.js | 2 +- typings/index.d.ts | 1 - 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/src/structures/CommandInteraction.js b/src/structures/CommandInteraction.js index 6ffad2f14b5e..2ceafc6ae4eb 100644 --- a/src/structures/CommandInteraction.js +++ b/src/structures/CommandInteraction.js @@ -1,6 +1,5 @@ 'use strict'; -const GuildChannel = require('./GuildChannel'); const Interaction = require('./Interaction'); const InteractionWebhook = require('./InteractionWebhook'); const InteractionResponses = require('./interfaces/InteractionResponses'); @@ -81,14 +80,6 @@ class CommandInteraction extends Interaction { return this.guild?.commands.cache.get(id) ?? this.client.application.commands.cache.get(id) ?? null; } - /** - * Indicates whether this interaction is received from a guild. - * @returns {boolean} - */ - inGuild() { - return this.guild && this.guildID && this.member && this.channel instanceof GuildChannel; - } - /** * Represents an option of a received command interaction. * @typedef {Object} CommandInteractionOption diff --git a/src/structures/Interaction.js b/src/structures/Interaction.js index 74ecbd855b2c..e5afe4e0e8cc 100644 --- a/src/structures/Interaction.js +++ b/src/structures/Interaction.js @@ -110,7 +110,7 @@ class Interaction extends Base { * @returns {boolean} */ inGuild() { - return this.guild && this.guildID && this.member; + return this.guildID && this.member; } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index 938a63ef18d5..a854a68adde7 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -537,7 +537,6 @@ declare module 'discord.js' { public options: Collection; public replied: boolean; public webhook: InteractionWebhook; - public inGuild(): boolean; public defer(options?: InteractionDeferOptions): Promise; public deleteReply(): Promise; public editReply(options: string | MessagePayload | WebhookEditMessageOptions): Promise; From daa9a50a3696f1ee58ebcd2dde38aa1f5b57aac8 Mon Sep 17 00:00:00 2001 From: Papageorgiadis Savvas <50584606+papsavas@users.noreply.github.com> Date: Tue, 29 Jun 2021 07:24:45 +0300 Subject: [PATCH 3/3] fix: boolean ensuring Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> --- src/structures/Interaction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/Interaction.js b/src/structures/Interaction.js index e5afe4e0e8cc..ee7140fb527a 100644 --- a/src/structures/Interaction.js +++ b/src/structures/Interaction.js @@ -110,7 +110,7 @@ class Interaction extends Base { * @returns {boolean} */ inGuild() { - return this.guildID && this.member; + return Boolean(this.guildID && this.member); } /**