Add handling of autocompletion interactions

This commit is contained in:
Wroclaw 2023-05-10 04:19:49 +02:00
parent ec7df40edb
commit 3cf2af7aed
2 changed files with 10 additions and 1 deletions

View file

@ -1,4 +1,4 @@
import { PermissionsBitField } from "discord.js";
import { AutocompleteInteraction, PermissionsBitField } from "discord.js";
import { RESTPostAPIApplicationCommandsJSONBody } from "discord.js";
import { ApplicationCommandOption, ApplicationCommandType, ChatInputCommandInteraction, LocalizationMap, MessageInteraction, PermissionResolvable, UserSelectMenuInteraction } from "discord.js";
@ -23,6 +23,8 @@ interface Command<Type extends keyof InteractionTypeMap = ApplicationCommandType
abstract class Command<Type extends keyof InteractionTypeMap = ApplicationCommandType> {
abstract execute(interaction: InteractionTypeMap[Type][0]): Promise<void>;
autocomplete?(interaction: Type extends ApplicationCommandType.ChatInput ? AutocompleteInteraction : never ): Promise<void>;
toRESTPostApplicationCommands(): RESTPostAPIApplicationCommandsJSONBody {
return {
name: this.name,

View file

@ -32,6 +32,13 @@ export default class CommandManager {
foundCommand.execute(interaction);
return;
}
if (interaction.isAutocomplete()) {
const foundCommand = this.commands.find((command) => command.name == interaction.commandName );
if (!foundCommand) throw new Error(`Unknown command received (${interaction.commandName}). Did you forgot to push updated commands?`);
if (!foundCommand.autocomplete) return;
foundCommand.autocomplete(interaction);
return;
}
}
bindClient(client: DiscordClient) {