chore: merge branch dev to main #94

Merged
Anri merged 6 commits from dev into main 2023-04-27 22:11:29 +02:00
Showing only changes of commit 4d4851da7b - Show all commits

View file

@ -10,7 +10,7 @@ import {
import { Metadata } from "../../utils/metadata"; import { Metadata } from "../../utils/metadata";
import { getLocale, getLocalizations } from "../../utils/locales"; import { getLocale, getLocalizations } from "../../utils/locales";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
import { Player, useMasterPlayer, useQueue } from "discord-player"; import { Player, SearchResult, useMasterPlayer, useQueue } from "discord-player";
export default { export default {
scope: () => [], scope: () => [],
@ -158,20 +158,38 @@ export default {
loc_default?.get(`c_${filename}_opt1_name`) as string, loc_default?.get(`c_${filename}_opt1_name`) as string,
true true
); );
if (query) {
const results = await player.search(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
* and can take longer than that, exception of type 'Unknown interaction' (10062) * and can take longer than that, exception of type 'Unknown interaction' (10062)
* happens. * happens. */
* TODO: Silently pass the exception */
let timeoutId: NodeJS.Timeout;
const delay = new Promise(function (_, reject) {
timeoutId = setTimeout(function () {
reject(new Error());
}, 2900);
});
/* Create a race between a timeout and the research
* At the end, Discord will always receive a response */
const tracks = await Promise.race([delay, player.search(query)])
.then((res) => {
clearTimeout(timeoutId);
return (res as SearchResult).tracks;
})
.catch(() => {
return [];
});
// Returns a list of songs with their title // Returns a list of songs with their title
return interaction.respond( return interaction.respond(
results.tracks.slice(0, 10).map((t) => ({ tracks.slice(0, 10).map((t) => ({
name: t.title, name: t.title,
value: t.url, value: t.url,
})) }))
); );
} else {
return interaction.respond([]);
}
}, },
}; };