From 7747d286446be31745dc4ebf2574d84c252b0fc1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 31 Oct 2024 18:12:35 +0100 Subject: [PATCH] add manual search with autocompletion (fix #198) --- src/commands/music/play.ts | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/commands/music/play.ts b/src/commands/music/play.ts index 95dd2c1..349323c 100644 --- a/src/commands/music/play.ts +++ b/src/commands/music/play.ts @@ -193,6 +193,9 @@ export default { const query = interaction.options.getString(loc_default!.get(`c_${filename}_opt1_name`)!, true); const limit_value_discord = 100; + const limit_element_discord = 25; + + const query_discord = query.slice(0, limit_value_discord); if (query) { /* Since Discord wanna receive a response within 3 secs and results is async @@ -208,7 +211,7 @@ export default { /* Create a race between a timeout and the search * At the end, Discord will always receive a response */ - let tracks = await Promise.race([ + const tracks = await Promise.race([ delay, player.search(query, { requestedBy: interaction.user, @@ -225,17 +228,12 @@ export default { // If tracks found if (tracks.length > 0) { - if (tracks.length > 25) { - tracks = tracks - // Assure that URL is under the limit of Discord - .filter((v) => v.url.length < limit_value_discord) - // Slice the list if needed to the 25 first results - .slice(0, 25); - } - - // Returns a list of songs with their title and author - return interaction.respond( - tracks.map((t) => { + const payload = tracks + // Assure that URL is under the limit of Discord + .filter((v) => v.url.length < limit_value_discord) + // Slice the list to respect the limit of Discord + .slice(0, limit_element_discord - 1) + .map((t) => { let title = t.title; let author = t.author; let name = `${title} • ${author}`; @@ -257,12 +255,18 @@ export default { name, value: t.url, }; - }), - ); + }); + + payload.unshift({ + name: query_discord, + value: query_discord, + }); + + // Returns a list of songs with their title and author + return interaction.respond(payload); } } - return interaction.respond([ - { name: loc.get("c_play9"), value: query.slice(0, limit_value_discord) }, - ]); + + return interaction.respond([{ name: loc.get("c_play9"), value: query_discord }]); }, };