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 { 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) {
|
||||||
|
/* 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
|
/* Create a race between a timeout and the research
|
||||||
* and can take longer than that, exception of type 'Unknown interaction' (10062)
|
* At the end, Discord will always receive a response */
|
||||||
* happens.
|
const tracks = await Promise.race([delay, player.search(query)])
|
||||||
* TODO: Silently pass the exception */
|
.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([]);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue