From 025715c0da58ad967736e8fd33ee089068385ff2 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sun, 23 Jul 2023 13:41:16 +0200 Subject: [PATCH] feat: Autocomplete queries in /play command --- src/commands/player/play.js | 26 +++++- src/events/interactions/interactionCreate.js | 93 +++++++++++--------- 2 files changed, 74 insertions(+), 45 deletions(-) diff --git a/src/commands/player/play.js b/src/commands/player/play.js index 53378cf..331b3bd 100644 --- a/src/commands/player/play.js +++ b/src/commands/player/play.js @@ -11,7 +11,31 @@ module.exports = { .setName('play') .setDescription('Add a track or playlist to the queue by searching or url.') .setDMPermission(false) - .addStringOption((option) => option.setName('query').setDescription('Search query or URL.').setRequired(true)), + .addStringOption((option) => + option + .setName('query') + .setDescription('Search query or URL.') + .setRequired(true) + .setMinLength(2) + .setMaxLength(500) + .setAutocomplete(true) + ), + autocomplete: async ({ interaction }) => { + const player = useMainPlayer(); + const query = interaction.options.getString('query', true); + if (query.length < 2) { + return; + } + const searchResults = await player.search(query); + + logger.debug(`[Shard ${interaction.guild.shardId}] Autocomplete search responded for query: ${query}`); + return interaction.respond( + searchResults.tracks.slice(0, 5).map((track) => ({ + name: `${track.title} [Author: ${track.author}]`, + value: track.url + })) + ); + }, execute: async ({ interaction }) => { if (await notInVoiceChannel(interaction)) { return; diff --git a/src/events/interactions/interactionCreate.js b/src/events/interactions/interactionCreate.js index 398901d..80b70be 100644 --- a/src/events/interactions/interactionCreate.js +++ b/src/events/interactions/interactionCreate.js @@ -7,10 +7,6 @@ module.exports = { name: Events.InteractionCreate, isDebug: false, execute: async (interaction, { client }) => { - if (!interaction.isCommand()) { - return; - } - if (await cannotSendMessageInChannel(interaction)) { return; } @@ -23,62 +19,71 @@ module.exports = { return; } - try { - const inputTime = new Date(); + if (interaction.isAutocomplete()) { + try { + await command.autocomplete({ interaction, client }); + } catch (error) { + logger.warn(error, `[Shard ${interaction.guild.shardId}] Autocomplete failed to execute.`); + return; + } + } else if (interaction.isChatInputCommand()) { + try { + const inputTime = new Date(); - await interaction.deferReply(); - await command.execute({ interaction, client }); + await interaction.deferReply(); + await command.execute({ interaction, client }); - const outputTime = new Date(); - const executionTime = outputTime - inputTime; + const outputTime = new Date(); + const executionTime = outputTime - inputTime; - if (executionTime > 20000) { - // don't send warning message for commands with collector timeouts, as collector timeout happens after 60 seconds - if ( - (interaction.commandName === 'filters' || interaction.commandName === 'nowplaying') && - executionTime > 55000 - ) { + if (executionTime > 20000) { + // don't send warning message for commands with collector timeouts, as collector timeout happens after 60 seconds + if ( + (interaction.commandName === 'filters' || interaction.commandName === 'nowplaying') && + executionTime > 55000 + ) { + logger.info( + `[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' executed in ${executionTime} ms.` + ); + return; + } + + logger.warn( + `[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' took ${executionTime} ms to execute.` + ); + + return await interaction.followUp({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${embedOptions.icons.warning} Warning**\nThis command took ${ + executionTime / 1000 + } seconds to execute.\n\n_If you experienced problems with the command, please try again._` + ) + .setColor(embedOptions.colors.warning) + ] + }); + } else { logger.info( `[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' executed in ${executionTime} ms.` ); - return; } - - logger.warn( - `[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' took ${executionTime} ms to execute.` + } catch (error) { + logger.error( + error, + `[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' failed to execute.` ); - return await interaction.followUp({ + await interaction.followUp({ embeds: [ new EmbedBuilder() .setDescription( - `**${embedOptions.icons.warning} Warning**\nThis command took ${ - executionTime / 1000 - } seconds to execute.\n\n_If you experienced problems with the command, please try again._` + `**${embedOptions.icons.error} Uh-oh... _Something_ went wrong!**\nThere was an unexpected error while trying to execute this command.\n\nYou can try to perform the command again.\n\n_If this problem persists, please submit a bug report in the **[support server](${botOptions.serverInviteUrl})**._` ) - .setColor(embedOptions.colors.warning) + .setColor(embedOptions.colors.error) ] }); - } else { - logger.info( - `[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' executed in ${executionTime} ms.` - ); } - } catch (error) { - logger.error( - error, - `[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' failed to execute.` - ); - - await interaction.followUp({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${embedOptions.icons.error} Uh-oh... _Something_ went wrong!**\nThere was an unexpected error while trying to execute this command.\n\nYou can try to perform the command again.\n\n_If this problem persists, please submit a bug report in the **[support server](${botOptions.serverInviteUrl})**._` - ) - .setColor(embedOptions.colors.error) - ] - }); } } };