Files
star-kitten/packages/lib/src/discord/commands/command-helpers.ts

82 lines
3.4 KiB
TypeScript

import { Constants } from '@projectdysnomia/dysnomia';
import type {
CommandInteraction,
ExecutableInteraction,
Interaction,
AutocompleteInteraction,
ComponentInteraction,
ModalSubmitInteraction,
PingInteraction,
SelectMenuInteraction,
ButtonInteraction,
} 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)) {
console.log(`command name: ${interaction.data.name}`);
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.isSelectMenu = function (): this is SelectMenuInteraction {
return interaction.isMessageComponent() && interaction.data.component_type in [3, 5, 6, 7, 8];
};
interaction.isButton = function (): this is ButtonInteraction {
return interaction.isMessageComponent() && interaction.data.component_type === Constants.ComponentTypes.BUTTON;
};
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;
}