Skip to content

Commit

Permalink
feat(Utils): add more typeguard functions to determine the interactio…
Browse files Browse the repository at this point in the history
…n types (#355)
  • Loading branch information
IanMitchell committed Mar 5, 2022
1 parent e9ee696 commit dec7717
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 0 deletions.
68 changes: 68 additions & 0 deletions deno/utils/v10.ts
Expand Up @@ -5,13 +5,20 @@ import {
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
InteractionType,
} from '../payloads/v10/mod.ts';

// Interactions
Expand Down Expand Up @@ -101,3 +108,64 @@ export function isLinkButton(component: APIButtonComponent): component is APIBut
export function isInteractionButton(component: APIButtonComponent): component is APIButtonComponentWithCustomId {
return component.style !== ButtonStyle.Link;
}

// Message Components

/**
* 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
*/
export function isMessageComponentInteraction(
interaction: APIInteraction,
): interaction is APIMessageComponentInteraction {
return interaction.type === InteractionType.MessageComponent;
}

/**
* 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
*/
export function isMessageComponentButtonInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentButtonInteraction {
return interaction.data.component_type === ComponentType.Button;
}

/**
* 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
*/
export function isMessageComponentSelectMenuInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentSelectMenuInteraction {
return interaction.data.component_type === ComponentType.SelectMenu;
}

// 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
*/
export function isChatInputApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIChatInputApplicationCommandInteraction {
return interaction.data.type === ApplicationCommandType.ChatInput;
}

/**
* 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
*/
export function isContextMenuApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIContextMenuInteraction {
return (
interaction.data.type === ApplicationCommandType.Message || interaction.data.type === ApplicationCommandType.User
);
}
68 changes: 68 additions & 0 deletions deno/utils/v9.ts
Expand Up @@ -5,13 +5,20 @@ import {
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
InteractionType,
} from '../payloads/v9/mod.ts';

// Interactions
Expand Down Expand Up @@ -101,3 +108,64 @@ export function isLinkButton(component: APIButtonComponent): component is APIBut
export function isInteractionButton(component: APIButtonComponent): component is APIButtonComponentWithCustomId {
return component.style !== ButtonStyle.Link;
}

// Message Components

/**
* 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
*/
export function isMessageComponentInteraction(
interaction: APIInteraction,
): interaction is APIMessageComponentInteraction {
return interaction.type === InteractionType.MessageComponent;
}

/**
* 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
*/
export function isMessageComponentButtonInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentButtonInteraction {
return interaction.data.component_type === ComponentType.Button;
}

/**
* 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
*/
export function isMessageComponentSelectMenuInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentSelectMenuInteraction {
return interaction.data.component_type === ComponentType.SelectMenu;
}

// 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
*/
export function isChatInputApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIChatInputApplicationCommandInteraction {
return interaction.data.type === ApplicationCommandType.ChatInput;
}

/**
* 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
*/
export function isContextMenuApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIContextMenuInteraction {
return (
interaction.data.type === ApplicationCommandType.Message || interaction.data.type === ApplicationCommandType.User
);
}
68 changes: 68 additions & 0 deletions utils/v10.ts
Expand Up @@ -5,13 +5,20 @@ import {
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
InteractionType,
} from '../payloads/v10/index';

// Interactions
Expand Down Expand Up @@ -101,3 +108,64 @@ export function isLinkButton(component: APIButtonComponent): component is APIBut
export function isInteractionButton(component: APIButtonComponent): component is APIButtonComponentWithCustomId {
return component.style !== ButtonStyle.Link;
}

// Message Components

/**
* 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
*/
export function isMessageComponentInteraction(
interaction: APIInteraction,
): interaction is APIMessageComponentInteraction {
return interaction.type === InteractionType.MessageComponent;
}

/**
* 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
*/
export function isMessageComponentButtonInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentButtonInteraction {
return interaction.data.component_type === ComponentType.Button;
}

/**
* 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
*/
export function isMessageComponentSelectMenuInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentSelectMenuInteraction {
return interaction.data.component_type === ComponentType.SelectMenu;
}

// 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
*/
export function isChatInputApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIChatInputApplicationCommandInteraction {
return interaction.data.type === ApplicationCommandType.ChatInput;
}

/**
* 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
*/
export function isContextMenuApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIContextMenuInteraction {
return (
interaction.data.type === ApplicationCommandType.Message || interaction.data.type === ApplicationCommandType.User
);
}
68 changes: 68 additions & 0 deletions utils/v9.ts
Expand Up @@ -5,13 +5,20 @@ import {
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
InteractionType,
} from '../payloads/v9/index';

// Interactions
Expand Down Expand Up @@ -101,3 +108,64 @@ export function isLinkButton(component: APIButtonComponent): component is APIBut
export function isInteractionButton(component: APIButtonComponent): component is APIButtonComponentWithCustomId {
return component.style !== ButtonStyle.Link;
}

// Message Components

/**
* 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
*/
export function isMessageComponentInteraction(
interaction: APIInteraction,
): interaction is APIMessageComponentInteraction {
return interaction.type === InteractionType.MessageComponent;
}

/**
* 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
*/
export function isMessageComponentButtonInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentButtonInteraction {
return interaction.data.component_type === ComponentType.Button;
}

/**
* 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
*/
export function isMessageComponentSelectMenuInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentSelectMenuInteraction {
return interaction.data.component_type === ComponentType.SelectMenu;
}

// 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
*/
export function isChatInputApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIChatInputApplicationCommandInteraction {
return interaction.data.type === ApplicationCommandType.ChatInput;
}

/**
* 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
*/
export function isContextMenuApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIContextMenuInteraction {
return (
interaction.data.type === ApplicationCommandType.Message || interaction.data.type === ApplicationCommandType.User
);
}

0 comments on commit dec7717

Please sign in to comment.