feat: Autocomplete queries in /play command

This commit is contained in:
Marius Begby committed 2023-07-23 13:41:16 +02:00
1 parent 7e0479c299
commit 025715c0da
2 files changed
+34 -5

No files matched your search

+25 -1
View File
@@ -11,7 +11,31 @@ module.exports = {
.setName('play') .setName('play')
.setDescription('Add a track or playlist to the queue by searching or url.') .setDescription('Add a track or playlist to the queue by searching or url.')
.setDMPermission(false) .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 }) => { execute: async ({ interaction }) => {
if (await notInVoiceChannel(interaction)) { if (await notInVoiceChannel(interaction)) {
return; return;
+9 -4
View File
@@ -7,10 +7,6 @@ module.exports = {
name: Events.InteractionCreate, name: Events.InteractionCreate,
isDebug: false, isDebug: false,
execute: async (interaction, { client }) => { execute: async (interaction, { client }) => {
if (!interaction.isCommand()) {
return;
}
if (await cannotSendMessageInChannel(interaction)) { if (await cannotSendMessageInChannel(interaction)) {
return; return;
} }
@@ -23,6 +19,14 @@ module.exports = {
return; return;
} }
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 { try {
const inputTime = new Date(); const inputTime = new Date();
@@ -81,4 +85,5 @@ module.exports = {
}); });
} }
} }
}
}; };