misc.mcserver: more error handling

This commit is contained in:
Cynthia Foxwell 2024-08-02 12:01:12 -06:00
parent 7042f6b3f5
commit 843bb59858

View file

@ -134,7 +134,7 @@ function queryServer(ip, port, HANDSHAKE_PACKET) {
return reject(err);
}
const dataAsString = totalData.toString().trim();
console.log(dataAsString);
logger.verbose("mcserver", "data as string", dataAsString);
const json = JSON.parse(dataAsString.slice(dataAsString.indexOf("{"), dataAsString.lastIndexOf("}") + 1));
logger.verbose("mcserver", "close", json);
clearTimeout(timeout);
@ -196,14 +196,24 @@ mcserver.callback = async function (msg, line) {
await msg.addReaction("\uD83C\uDFD3");
let data = await queryServerIdent(ip, port);
let data;
try {
data = await queryServerIdent(ip, port);
} catch {
try {
data = await queryServerNoIdent(ip, port);
} catch (err) {
await msg.removeReaction("\uD83C\uDFD3");
return `Failed to query:\n\`\`\`\n${data.error.message}\n\`\`\``;
}
}
if (data == "timeout") {
await msg.removeReaction("\uD83C\uDFD3");
return "Timed out trying to query.";
} else if (data?.error) {
await msg.removeReaction("\uD83C\uDFD3");
return `Failed to query:\n\`\`\`\n${data.error}\n\`\`\``;
return `Failed to query:\n\`\`\`\n${data.error.message}\n\`\`\``;
} else {
if (data.version.name == "TCPShield.com") data = await queryServerNoIdent(ip, port);