always return a reponse to discord
This commit is contained in:
parent
6c5dd185bc
commit
4d4851da7b
1 changed files with 31 additions and 13 deletions
|
@ -10,7 +10,7 @@ import {
|
|||
import { Metadata } from "../../utils/metadata";
|
||||
import { getLocale, getLocalizations } from "../../utils/locales";
|
||||
import { getFilename } from "../../utils/misc";
|
||||
import { Player, useMasterPlayer, useQueue } from "discord-player";
|
||||
import { Player, SearchResult, useMasterPlayer, useQueue } from "discord-player";
|
||||
|
||||
export default {
|
||||
scope: () => [],
|
||||
|
@ -158,20 +158,38 @@ export default {
|
|||
loc_default?.get(`c_${filename}_opt1_name`) as string,
|
||||
true
|
||||
);
|
||||
if (query) {
|
||||
/* 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)
|
||||
* happens. */
|
||||
|
||||
const results = await player.search(query);
|
||||
let timeoutId: NodeJS.Timeout;
|
||||
const delay = new Promise(function (_, reject) {
|
||||
timeoutId = setTimeout(function () {
|
||||
reject(new Error());
|
||||
}, 2900);
|
||||
});
|
||||
|
||||
/* 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)
|
||||
* happens.
|
||||
* TODO: Silently pass the exception */
|
||||
/* 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
|
||||
return interaction.respond(
|
||||
results.tracks.slice(0, 10).map((t) => ({
|
||||
name: t.title,
|
||||
value: t.url,
|
||||
}))
|
||||
);
|
||||
// Returns a list of songs with their title
|
||||
return interaction.respond(
|
||||
tracks.slice(0, 10).map((t) => ({
|
||||
name: t.title,
|
||||
value: t.url,
|
||||
}))
|
||||
);
|
||||
} else {
|
||||
return interaction.respond([]);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue