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

This commit is contained in:
Ian Mitchell
2022-03-05 10:30:41 -08:00
committed by GitHub
parent e9ee6966c3
commit dec7717bc7
4 changed files with 272 additions and 0 deletions

View File

@@ -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
@@ -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
);
}

View File

@@ -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
@@ -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
);
}

View File

@@ -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
@@ -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
);
}

View File

@@ -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
@@ -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
);
}