chore: merge branch dev
to main
#94
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) {
|
||||||
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([]);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue