Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Utils): add more typeguard functions to determine the interaction types #355

Merged
merged 3 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions deno/utils/v10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import {
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
} from '../payloads/v10/mod.ts';

// Interactions
Expand Down Expand Up @@ -101,3 +107,53 @@ 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 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
);
}
56 changes: 56 additions & 0 deletions deno/utils/v9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import {
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
} from '../payloads/v9/mod.ts';

// Interactions
Expand Down Expand Up @@ -101,3 +107,53 @@ 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 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
);
}
56 changes: 56 additions & 0 deletions utils/v10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import {
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
} from '../payloads/v10/index';

// Interactions
Expand Down Expand Up @@ -101,3 +107,53 @@ 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 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(
IanMitchell marked this conversation as resolved.
Show resolved Hide resolved
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
);
}
56 changes: 56 additions & 0 deletions utils/v9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import {
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
} from '../payloads/v9/index';

// Interactions
Expand Down Expand Up @@ -101,3 +107,53 @@ 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 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
);
}