mirror of
https://github.com/discordjs/discord-api-types.git
synced 2026-05-29 14:50:08 +00:00
feat(Utils): add more typeguard functions to determine the interaction types (#355)
This commit is contained in:
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
68
utils/v10.ts
68
utils/v10.ts
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
68
utils/v9.ts
68
utils/v9.ts
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user