Initial commit
This commit is contained in:
72
packages/lib/src/discord/commands/command-helpers.ts
Normal file
72
packages/lib/src/discord/commands/command-helpers.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { Constants } from '@projectdysnomia/dysnomia';
|
||||
import type {
|
||||
CommandInteraction,
|
||||
ExecutableInteraction,
|
||||
Interaction,
|
||||
AutocompleteInteraction,
|
||||
ComponentInteraction,
|
||||
ModalSubmitInteraction,
|
||||
PingInteraction,
|
||||
} from '../types';
|
||||
|
||||
export function isApplicationCommand(interaction: Interaction): interaction is CommandInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.APPLICATION_COMMAND;
|
||||
}
|
||||
|
||||
export function isModalSubmit(interaction: Interaction): interaction is ModalSubmitInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.MODAL_SUBMIT;
|
||||
}
|
||||
|
||||
export function isMessageComponent(interaction: Interaction): interaction is ComponentInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.MESSAGE_COMPONENT;
|
||||
}
|
||||
|
||||
export function isAutocomplete(interaction: Interaction): interaction is AutocompleteInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE;
|
||||
}
|
||||
|
||||
export function isPing(interaction: Interaction): interaction is PingInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.PING;
|
||||
}
|
||||
|
||||
export function commandHasName(interaction: Interaction, name: string): boolean {
|
||||
return isApplicationCommand(interaction) && interaction.data.name === name;
|
||||
}
|
||||
|
||||
export function commandHasIdPrefix(interaction: Interaction, prefix: string): boolean {
|
||||
return (isModalSubmit(interaction) || isMessageComponent(interaction)) && interaction.data.custom_id.startsWith(prefix);
|
||||
}
|
||||
|
||||
export function getCommandName(interaction: ExecutableInteraction): string | undefined {
|
||||
if (isApplicationCommand(interaction) || isAutocomplete(interaction)) {
|
||||
return interaction.data.name;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function augmentInteraction(interaction: Interaction): Interaction {
|
||||
interaction.isApplicationCommand = function (): this is CommandInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.APPLICATION_COMMAND;
|
||||
};
|
||||
interaction.isModalSubmit = function (): this is ModalSubmitInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.MODAL_SUBMIT;
|
||||
};
|
||||
interaction.isMessageComponent = function (): this is ComponentInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.MESSAGE_COMPONENT;
|
||||
};
|
||||
interaction.isAutocomplete = function (): this is AutocompleteInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE;
|
||||
};
|
||||
interaction.isPing = function (): this is PingInteraction {
|
||||
return interaction.type === Constants.InteractionTypes.PING;
|
||||
};
|
||||
interaction.isExecutable = function (): this is ExecutableInteraction {
|
||||
return (
|
||||
interaction.type === Constants.InteractionTypes.APPLICATION_COMMAND ||
|
||||
interaction.type === Constants.InteractionTypes.MODAL_SUBMIT ||
|
||||
interaction.type === Constants.InteractionTypes.MESSAGE_COMPONENT ||
|
||||
interaction.type === Constants.InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE
|
||||
);
|
||||
};
|
||||
return interaction;
|
||||
}
|
||||
Reference in New Issue
Block a user