add manual search with autocompletion (fix #198)
All checks were successful
PR Check / lint-and-format (pull_request) Successful in 22s

This commit is contained in:
Mylloon 2024-10-31 18:12:35 +01:00
parent 399b3285df
commit 7747d28644
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -193,6 +193,9 @@ export default {
const query = interaction.options.getString(loc_default!.get(`c_${filename}_opt1_name`)!, true); const query = interaction.options.getString(loc_default!.get(`c_${filename}_opt1_name`)!, true);
const limit_value_discord = 100; const limit_value_discord = 100;
const limit_element_discord = 25;
const query_discord = query.slice(0, limit_value_discord);
if (query) { if (query) {
/* Since Discord wanna receive a response within 3 secs and results is async /* 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 /* Create a race between a timeout and the search
* At the end, Discord will always receive a response */ * At the end, Discord will always receive a response */
let tracks = await Promise.race([ const tracks = await Promise.race([
delay, delay,
player.search(query, { player.search(query, {
requestedBy: interaction.user, requestedBy: interaction.user,
@ -225,17 +228,12 @@ export default {
// If tracks found // If tracks found
if (tracks.length > 0) { if (tracks.length > 0) {
if (tracks.length > 25) { const payload = tracks
tracks = tracks // Assure that URL is under the limit of Discord
// Assure that URL is under the limit of Discord .filter((v) => v.url.length < limit_value_discord)
.filter((v) => v.url.length < limit_value_discord) // Slice the list to respect the limit of Discord
// Slice the list if needed to the 25 first results .slice(0, limit_element_discord - 1)
.slice(0, 25); .map((t) => {
}
// Returns a list of songs with their title and author
return interaction.respond(
tracks.map((t) => {
let title = t.title; let title = t.title;
let author = t.author; let author = t.author;
let name = `${title}${author}`; let name = `${title}${author}`;
@ -257,12 +255,18 @@ export default {
name, name,
value: t.url, 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 }]);
]);
}, },
}; };