From 3cf2af7aed7fca4ce6d6c18cc0d9afc85ef772ad Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Wed, 10 May 2023 04:19:49 +0200 Subject: [PATCH] Add handling of autocompletion interactions --- src/command.ts | 4 +++- src/interactionManager.ts | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/command.ts b/src/command.ts index 4ef8a64..2334ad0 100644 --- a/src/command.ts +++ b/src/command.ts @@ -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 { abstract execute(interaction: InteractionTypeMap[Type][0]): Promise; + autocomplete?(interaction: Type extends ApplicationCommandType.ChatInput ? AutocompleteInteraction : never ): Promise; + toRESTPostApplicationCommands(): RESTPostAPIApplicationCommandsJSONBody { return { name: this.name, diff --git a/src/interactionManager.ts b/src/interactionManager.ts index 3b7968b..d6ef69a 100644 --- a/src/interactionManager.ts +++ b/src/interactionManager.ts @@ -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) {