From f883770df6ae7ce8b1b036c7619b4ab8c4f92354 Mon Sep 17 00:00:00 2001 From: LukeZ <117578407+The-LukeZ@users.noreply.github.com> Date: Mon, 10 Nov 2025 14:59:15 +0100 Subject: [PATCH] feat: add `isModalSubmitInteraction()` type guard (#1428) * Implement type-guard for modal submit interactions Added a type-guard function to check for modal submit interactions. * Implement missing isModalSubmitInteraction typeguard in deno types Added a type-guard function to check for modal submit interactions in deno types because I missed that before. * fix(typings): replace 'type-guard' with 'type guard' This corrects the spacing in the term across multiple locations for consistency with standard TypeScript terminology. * Revert "fix(typings): replace 'type-guard' with 'type guard'" This reverts commit 822b2e0bbc4b16b1d02cc17538edeadd6b3a1f81. * fix(typings): replace 'type-guard' with 'type guard' in jsdoc comments in v10 * fix(typings): update JSDoc comments to use 'type guard' instead of 'type-guard' * fix(typings): replace "type-guard" with "type guard" in v9 utils * feat(interactions): add type guard for modal submit interactions in v9 --------- Co-authored-by: Luca Hentschel --- deno/utils/v10.ts | 39 ++++++++++++++++++++++++++------------- deno/utils/v9.ts | 39 ++++++++++++++++++++++++++------------- utils/v10.ts | 39 ++++++++++++++++++++++++++------------- utils/v9.ts | 39 ++++++++++++++++++++++++++------------- 4 files changed, 104 insertions(+), 52 deletions(-) diff --git a/deno/utils/v10.ts b/deno/utils/v10.ts index 7349962a..c5dd5125 100644 --- a/deno/utils/v10.ts +++ b/deno/utils/v10.ts @@ -15,13 +15,14 @@ import type { APIMessageComponentGuildInteraction, APIMessageComponentInteraction, APIMessageComponentSelectMenuInteraction, + APIModalSubmitInteraction, } from '../payloads/v10/mod.ts'; import { ApplicationCommandType, ButtonStyle, ComponentType, InteractionType } from '../payloads/v10/mod.ts'; // Interactions /** - * A type-guard check for DM interactions + * A type guard check for DM interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction was received in a DM channel @@ -31,7 +32,7 @@ export function isDMInteraction(interaction: APIInteraction): interaction is API } /** - * A type-guard check for guild interactions + * A type guard check for guild interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction was received in a guild @@ -43,7 +44,7 @@ export function isGuildInteraction(interaction: APIInteraction): interaction is // ApplicationCommandInteractions /** - * A type-guard check for DM application command interactions + * A type guard check for DM application command interactions * * @param interaction - The application command interaction to check against * @returns A boolean that indicates if the application command interaction was received in a DM channel @@ -55,7 +56,7 @@ export function isApplicationCommandDMInteraction( } /** - * A type-guard check for guild application command interactions + * A type guard check for guild application command interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the application command interaction was received in a guild @@ -69,7 +70,7 @@ export function isApplicationCommandGuildInteraction( // MessageComponentInteractions /** - * A type-guard check for DM message component interactions + * A type guard check for DM message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component interaction was received in a DM channel @@ -81,7 +82,7 @@ export function isMessageComponentDMInteraction( } /** - * A type-guard check for guild message component interactions + * A type guard check for guild message component interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the message component interaction was received in a guild @@ -95,7 +96,7 @@ export function isMessageComponentGuildInteraction( // Buttons /** - * A type-guard check for buttons that have a `url` attached to them. + * A type guard check for buttons that have a `url` attached to them. * * @param component - The button to check against * @returns A boolean that indicates if the button has a `url` attached to it @@ -105,7 +106,7 @@ export function isLinkButton(component: APIButtonComponent): component is APIBut } /** - * A type-guard check for buttons that have a `custom_id` attached to them. + * A type guard check for buttons that have a `custom_id` attached to them. * * @param component - The button to check against * @returns A boolean that indicates if the button has a `custom_id` attached to it @@ -114,10 +115,22 @@ export function isInteractionButton(component: APIButtonComponent): component is return ![ButtonStyle.Link, ButtonStyle.Premium].includes(component.style); } +// Modal + +/** + * A type guard check for modals submit interactions + * + * @param interaction - The interaction to check against + * @returns A boolean that indicates if the interaction is a modal submission + */ +export function isModalSubmitInteraction(interaction: APIInteraction): interaction is APIModalSubmitInteraction { + return interaction.type === InteractionType.ModalSubmit; +} + // Message Components /** - * A type-guard check for message component interactions + * A type guard check for message component interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a message component @@ -129,7 +142,7 @@ export function isMessageComponentInteraction( } /** - * A type-guard check for button message component interactions + * A type guard check for button message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component is a button @@ -141,7 +154,7 @@ export function isMessageComponentButtonInteraction( } /** - * A type-guard check for select menu message component interactions + * A type guard check for select menu message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component is a select menu @@ -161,7 +174,7 @@ export function isMessageComponentSelectMenuInteraction( // Application Commands /** - * A type-guard check for chat input application commands. + * A type guard check for chat input application commands. * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a chat input application command @@ -173,7 +186,7 @@ export function isChatInputApplicationCommandInteraction( } /** - * A type-guard check for context menu application commands. + * A type guard check for context menu application commands. * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a context menu application command diff --git a/deno/utils/v9.ts b/deno/utils/v9.ts index 58ebc6f5..a328a27a 100644 --- a/deno/utils/v9.ts +++ b/deno/utils/v9.ts @@ -15,13 +15,14 @@ import type { APIMessageComponentGuildInteraction, APIMessageComponentInteraction, APIMessageComponentSelectMenuInteraction, + APIModalSubmitInteraction, } from '../payloads/v9/mod.ts'; import { ApplicationCommandType, ButtonStyle, ComponentType, InteractionType } from '../payloads/v9/mod.ts'; // Interactions /** - * A type-guard check for DM interactions + * A type guard check for DM interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction was received in a DM channel @@ -31,7 +32,7 @@ export function isDMInteraction(interaction: APIInteraction): interaction is API } /** - * A type-guard check for guild interactions + * A type guard check for guild interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction was received in a guild @@ -43,7 +44,7 @@ export function isGuildInteraction(interaction: APIInteraction): interaction is // ApplicationCommandInteractions /** - * A type-guard check for DM application command interactions + * A type guard check for DM application command interactions * * @param interaction - The application command interaction to check against * @returns A boolean that indicates if the application command interaction was received in a DM channel @@ -55,7 +56,7 @@ export function isApplicationCommandDMInteraction( } /** - * A type-guard check for guild application command interactions + * A type guard check for guild application command interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the application command interaction was received in a guild @@ -69,7 +70,7 @@ export function isApplicationCommandGuildInteraction( // MessageComponentInteractions /** - * A type-guard check for DM message component interactions + * A type guard check for DM message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component interaction was received in a DM channel @@ -81,7 +82,7 @@ export function isMessageComponentDMInteraction( } /** - * A type-guard check for guild message component interactions + * A type guard check for guild message component interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the message component interaction was received in a guild @@ -95,7 +96,7 @@ export function isMessageComponentGuildInteraction( // Buttons /** - * A type-guard check for buttons that have a `url` attached to them. + * A type guard check for buttons that have a `url` attached to them. * * @param component - The button to check against * @returns A boolean that indicates if the button has a `url` attached to it @@ -105,7 +106,7 @@ export function isLinkButton(component: APIButtonComponent): component is APIBut } /** - * A type-guard check for buttons that have a `custom_id` attached to them. + * A type guard check for buttons that have a `custom_id` attached to them. * * @param component - The button to check against * @returns A boolean that indicates if the button has a `custom_id` attached to it @@ -114,10 +115,22 @@ export function isInteractionButton(component: APIButtonComponent): component is return ![ButtonStyle.Link, ButtonStyle.Premium].includes(component.style); } +// Modal + +/** + * A type guard check for modals submit interactions + * + * @param interaction - The interaction to check against + * @returns A boolean that indicates if the interaction is a modal submission + */ +export function isModalSubmitInteraction(interaction: APIInteraction): interaction is APIModalSubmitInteraction { + return interaction.type === InteractionType.ModalSubmit; +} + // Message Components /** - * A type-guard check for message component interactions + * A type guard check for message component interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a message component @@ -129,7 +142,7 @@ export function isMessageComponentInteraction( } /** - * A type-guard check for button message component interactions + * A type guard check for button message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component is a button @@ -141,7 +154,7 @@ export function isMessageComponentButtonInteraction( } /** - * A type-guard check for select menu message component interactions + * A type guard check for select menu message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component is a select menu @@ -161,7 +174,7 @@ export function isMessageComponentSelectMenuInteraction( // Application Commands /** - * A type-guard check for chat input application commands. + * A type guard check for chat input application commands. * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a chat input application command @@ -173,7 +186,7 @@ export function isChatInputApplicationCommandInteraction( } /** - * A type-guard check for context menu application commands. + * A type guard check for context menu application commands. * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a context menu application command diff --git a/utils/v10.ts b/utils/v10.ts index ce47351b..0efce63a 100644 --- a/utils/v10.ts +++ b/utils/v10.ts @@ -15,13 +15,14 @@ import type { APIMessageComponentGuildInteraction, APIMessageComponentInteraction, APIMessageComponentSelectMenuInteraction, + APIModalSubmitInteraction, } from '../payloads/v10/index'; import { ApplicationCommandType, ButtonStyle, ComponentType, InteractionType } from '../payloads/v10/index'; // Interactions /** - * A type-guard check for DM interactions + * A type guard check for DM interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction was received in a DM channel @@ -31,7 +32,7 @@ export function isDMInteraction(interaction: APIInteraction): interaction is API } /** - * A type-guard check for guild interactions + * A type guard check for guild interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction was received in a guild @@ -43,7 +44,7 @@ export function isGuildInteraction(interaction: APIInteraction): interaction is // ApplicationCommandInteractions /** - * A type-guard check for DM application command interactions + * A type guard check for DM application command interactions * * @param interaction - The application command interaction to check against * @returns A boolean that indicates if the application command interaction was received in a DM channel @@ -55,7 +56,7 @@ export function isApplicationCommandDMInteraction( } /** - * A type-guard check for guild application command interactions + * A type guard check for guild application command interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the application command interaction was received in a guild @@ -69,7 +70,7 @@ export function isApplicationCommandGuildInteraction( // MessageComponentInteractions /** - * A type-guard check for DM message component interactions + * A type guard check for DM message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component interaction was received in a DM channel @@ -81,7 +82,7 @@ export function isMessageComponentDMInteraction( } /** - * A type-guard check for guild message component interactions + * A type guard check for guild message component interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the message component interaction was received in a guild @@ -95,7 +96,7 @@ export function isMessageComponentGuildInteraction( // Buttons /** - * A type-guard check for buttons that have a `url` attached to them. + * A type guard check for buttons that have a `url` attached to them. * * @param component - The button to check against * @returns A boolean that indicates if the button has a `url` attached to it @@ -105,7 +106,7 @@ export function isLinkButton(component: APIButtonComponent): component is APIBut } /** - * A type-guard check for buttons that have a `custom_id` attached to them. + * A type guard check for buttons that have a `custom_id` attached to them. * * @param component - The button to check against * @returns A boolean that indicates if the button has a `custom_id` attached to it @@ -114,10 +115,22 @@ export function isInteractionButton(component: APIButtonComponent): component is return ![ButtonStyle.Link, ButtonStyle.Premium].includes(component.style); } +// Modal + +/** + * A type guard check for modals submit interactions + * + * @param interaction - The interaction to check against + * @returns A boolean that indicates if the interaction is a modal submission + */ +export function isModalSubmitInteraction(interaction: APIInteraction): interaction is APIModalSubmitInteraction { + return interaction.type === InteractionType.ModalSubmit; +} + // Message Components /** - * A type-guard check for message component interactions + * A type guard check for message component interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a message component @@ -129,7 +142,7 @@ export function isMessageComponentInteraction( } /** - * A type-guard check for button message component interactions + * A type guard check for button message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component is a button @@ -141,7 +154,7 @@ export function isMessageComponentButtonInteraction( } /** - * A type-guard check for select menu message component interactions + * A type guard check for select menu message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component is a select menu @@ -161,7 +174,7 @@ export function isMessageComponentSelectMenuInteraction( // Application Commands /** - * A type-guard check for chat input application commands. + * A type guard check for chat input application commands. * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a chat input application command @@ -173,7 +186,7 @@ export function isChatInputApplicationCommandInteraction( } /** - * A type-guard check for context menu application commands. + * A type guard check for context menu application commands. * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a context menu application command diff --git a/utils/v9.ts b/utils/v9.ts index b21003ef..ed166193 100644 --- a/utils/v9.ts +++ b/utils/v9.ts @@ -15,13 +15,14 @@ import type { APIMessageComponentGuildInteraction, APIMessageComponentInteraction, APIMessageComponentSelectMenuInteraction, + APIModalSubmitInteraction, } from '../payloads/v9/index'; import { ApplicationCommandType, ButtonStyle, ComponentType, InteractionType } from '../payloads/v9/index'; // Interactions /** - * A type-guard check for DM interactions + * A type guard check for DM interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction was received in a DM channel @@ -31,7 +32,7 @@ export function isDMInteraction(interaction: APIInteraction): interaction is API } /** - * A type-guard check for guild interactions + * A type guard check for guild interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction was received in a guild @@ -43,7 +44,7 @@ export function isGuildInteraction(interaction: APIInteraction): interaction is // ApplicationCommandInteractions /** - * A type-guard check for DM application command interactions + * A type guard check for DM application command interactions * * @param interaction - The application command interaction to check against * @returns A boolean that indicates if the application command interaction was received in a DM channel @@ -55,7 +56,7 @@ export function isApplicationCommandDMInteraction( } /** - * A type-guard check for guild application command interactions + * A type guard check for guild application command interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the application command interaction was received in a guild @@ -69,7 +70,7 @@ export function isApplicationCommandGuildInteraction( // MessageComponentInteractions /** - * A type-guard check for DM message component interactions + * A type guard check for DM message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component interaction was received in a DM channel @@ -81,7 +82,7 @@ export function isMessageComponentDMInteraction( } /** - * A type-guard check for guild message component interactions + * A type guard check for guild message component interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the message component interaction was received in a guild @@ -95,7 +96,7 @@ export function isMessageComponentGuildInteraction( // Buttons /** - * A type-guard check for buttons that have a `url` attached to them. + * A type guard check for buttons that have a `url` attached to them. * * @param component - The button to check against * @returns A boolean that indicates if the button has a `url` attached to it @@ -105,7 +106,7 @@ export function isLinkButton(component: APIButtonComponent): component is APIBut } /** - * A type-guard check for buttons that have a `custom_id` attached to them. + * A type guard check for buttons that have a `custom_id` attached to them. * * @param component - The button to check against * @returns A boolean that indicates if the button has a `custom_id` attached to it @@ -114,10 +115,22 @@ export function isInteractionButton(component: APIButtonComponent): component is return ![ButtonStyle.Link, ButtonStyle.Premium].includes(component.style); } +// Modal + +/** + * A type guard check for modals submit interactions + * + * @param interaction - The interaction to check against + * @returns A boolean that indicates if the interaction is a modal submission + */ +export function isModalSubmitInteraction(interaction: APIInteraction): interaction is APIModalSubmitInteraction { + return interaction.type === InteractionType.ModalSubmit; +} + // Message Components /** - * A type-guard check for message component interactions + * A type guard check for message component interactions * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a message component @@ -129,7 +142,7 @@ export function isMessageComponentInteraction( } /** - * A type-guard check for button message component interactions + * A type guard check for button message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component is a button @@ -141,7 +154,7 @@ export function isMessageComponentButtonInteraction( } /** - * A type-guard check for select menu message component interactions + * A type guard check for select menu message component interactions * * @param interaction - The message component interaction to check against * @returns A boolean that indicates if the message component is a select menu @@ -161,7 +174,7 @@ export function isMessageComponentSelectMenuInteraction( // Application Commands /** - * A type-guard check for chat input application commands. + * A type guard check for chat input application commands. * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a chat input application command @@ -173,7 +186,7 @@ export function isChatInputApplicationCommandInteraction( } /** - * A type-guard check for context menu application commands. + * A type guard check for context menu application commands. * * @param interaction - The interaction to check against * @returns A boolean that indicates if the interaction is a context menu application command