sourcequery: fix timeout

This commit is contained in:
Cynthia Foxwell 2025-10-09 17:14:01 -06:00
parent bad6d45963
commit 4bceaa5ede
Signed by: Cynosphere
SSH key fingerprint: SHA256:H3SM8ufP/uxqLwKSH7xY89TDnbR9uOHzjLoBr0tlajk

View file

@ -32,16 +32,17 @@ sourcequery.callback = async function (msg, line) {
await msg.addReaction("\uD83C\uDFD3");
let data;
let timeout = false;
try {
const timeout = setTimeout(() => {
throw "timeout";
}, 5000);
const doTimeout = setTimeout(() => {
timeout = true;
}, 60000);
const query = new SourceQuery(ip, port);
const info = await query.getInfo();
const players = await query.getPlayers();
const rules = await query.getRules();
clearTimeout(timeout);
clearTimeout(doTimeout);
data = {
info,
players,
@ -49,15 +50,13 @@ sourcequery.callback = async function (msg, line) {
};
} catch (err) {
await msg.removeReaction("\uD83C\uDFD3");
if (err == "timeout") {
return "Failed to query server after 5 seconds.";
} else {
return `:warning: An error occured while querying:\n\`\`\`\n${err.message}\n\`\`\``;
}
}
await msg.removeReaction("\uD83C\uDFD3");
if (data?.info == null) {
if (timeout) {
return "Failed to query server after 1 minute.";
} else if (data?.info == null) {
return "Failed to query any data.";
}