From 8ab611836b9cafa26addcfb9ea5fc72eade68a8b Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 3 Jun 2023 23:31:27 -0400 Subject: [PATCH] Allow commands to use autocomplete --- Server/events/interactionCreate.js | 72 +++++++++++++++++------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/Server/events/interactionCreate.js b/Server/events/interactionCreate.js index 0350cb3..01240c5 100644 --- a/Server/events/interactionCreate.js +++ b/Server/events/interactionCreate.js @@ -8,45 +8,53 @@ const log = new DebugBuilder("server", "interactionCreate"); module.exports = { name: Events.InteractionCreate, async execute(interaction) { + const command = interaction.client.commands.get(interaction.commandName); + log.VERBOSE("Interaction for command: ", command); + + // Execute autocomplete if the user is checking autocomplete + if (interaction.isAutocomplete()) { + log.DEBUG("Running autocomplete for command: ", command.data.name); + return await command.autocomplete(interaction); + } + + // Check if the interaction is a command if (!interaction.isChatInputCommand()) return; - const command = interaction.client.commands.get(interaction.commandName); + if (!command) { + log.ERROR(`No command matching ${interaction.commandName} was found.`); + return; + } - if (!command) { - log.ERROR(`No command matching ${interaction.commandName} was found.`); - return; - } + log.DEBUG(`${interaction.member.user} is running '${interaction.commandName}'`); - log.DEBUG(`${interaction.member.user} is running '${interaction.commandName}'`); - - await authorizeCommand(interaction, command, async () => { - await authorizeTokenUsage(interaction, command, undefined, async () => { - try { - if (command.deferInitialReply) { - try { - if (interaction.options.getBool('public') && interaction.options.getBool('public') == false) await interaction.deferReply({ ephemeral: true }); - else await interaction.deferReply({ ephemeral: false }); - } - catch (err) { - if (err instanceof TypeError) { - // The public option doesn't exist in this command - await interaction.deferReply({ ephemeral: false }); - } else { - throw err; + await authorizeCommand(interaction, command, async () => { + await authorizeTokenUsage(interaction, command, undefined, async () => { + try { + if (command.deferInitialReply) { + try { + if (interaction.options.getBool('public') && interaction.options.getBool('public') == false) await interaction.deferReply({ ephemeral: true }); + else await interaction.deferReply({ ephemeral: false }); } + catch (err) { + if (err instanceof TypeError) { + // The public option doesn't exist in this command + await interaction.deferReply({ ephemeral: false }); + } else { + throw err; + } + } + } + command.execute(interaction); + } catch (error) { + log.ERROR(error); + if (interaction.replied || interaction.deferred) { + interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); + } else { + interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } - } - command.execute(interaction); - } catch (error) { - log.ERROR(error); - if (interaction.replied || interaction.deferred) { - interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); - } else { - interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } - } - }) - }); + }) + }); }, }; \ No newline at end of file