Skip to content

Commit

Permalink
Add Message Component Interaction Typeguards
Browse files Browse the repository at this point in the history
  • Loading branch information
IanMitchell committed Mar 4, 2022
1 parent c85958e commit d6d72de
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
27 changes: 27 additions & 0 deletions deno/utils/v10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
} from '../payloads/v10/mod.ts';

// Interactions
Expand Down Expand Up @@ -105,6 +108,30 @@ export function isInteractionButton(component: APIButtonComponent): component is
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

/**
Expand Down
27 changes: 27 additions & 0 deletions deno/utils/v9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
} from '../payloads/v9/mod.ts';

// Interactions
Expand Down Expand Up @@ -105,6 +108,30 @@ export function isInteractionButton(component: APIButtonComponent): component is
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

/**
Expand Down
27 changes: 27 additions & 0 deletions utils/v10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
} from '../payloads/v10/index';

// Interactions
Expand Down Expand Up @@ -105,6 +108,30 @@ export function isInteractionButton(component: APIButtonComponent): component is
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

/**
Expand Down
27 changes: 27 additions & 0 deletions utils/v9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
} from '../payloads/v9/index';

// Interactions
Expand Down Expand Up @@ -105,6 +108,30 @@ export function isInteractionButton(component: APIButtonComponent): component is
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

/**
Expand Down

0 comments on commit d6d72de

Please sign in to comment.