misc.mcserver: debug

This commit is contained in:
Cynthia Foxwell 2023-06-28 00:08:22 -06:00
parent 48c0d64cdb
commit 1cbdda7c2d

View file

@ -1,4 +1,5 @@
const Command = require("../lib/command.js"); const Command = require("../lib/command.js");
const logger = require("../lib/logger.js");
const CATEGORY = "misc"; const CATEGORY = "misc";
const {librex} = require("../../config.json"); const {librex} = require("../../config.json");
@ -753,26 +754,33 @@ mcserver.helpText = "Query a Minecraft server";
mcserver.callback = async function (msg, line) { mcserver.callback = async function (msg, line) {
if (!line || line == "") return "Arguments required."; if (!line || line == "") return "Arguments required.";
const [ip, port] = line.split(":"); const split = line.split(":");
const ip = split[0];
const port = split[1] ?? 25565;
const data = await new Promise((resolve, reject) => { const data = await new Promise((resolve, reject) => {
const timeout = setTimeout(() => resolve("timeout"), 5000); const timeout = setTimeout(() => resolve("timeout"), 10000);
const client = net.createConnection({ const client = net.createConnection({
host: ip, host: ip,
port: port ?? 25565, port: port,
timeout: 5000, timeout: 10000,
}); });
client.on("connect", async function () { client.on("connect", function () {
logger.debug("mcserver", "connect");
client.write(HANDSHAKE_PACKET); client.write(HANDSHAKE_PACKET);
}); });
let totalData = Buffer.alloc(0); let totalData = Buffer.alloc(0);
client.on("data", function (data) { client.on("data", function (data) {
totalData = Buffer.concat([totalData, data]); totalData = Buffer.concat([totalData, data]);
logger.debug("mcserver", "data", data.length, totalData.length);
}); });
client.on("close", function (err) { client.on("close", function (err) {
if (err) return reject(err); if (err) {
logger.debug("mcserver", "close with error", err);
return reject(err);
}
const dataAsString = totalData.toString().trim(); const dataAsString = totalData.toString().trim();
console.log(dataAsString); console.log(dataAsString);
const json = JSON.parse( const json = JSON.parse(
@ -781,6 +789,7 @@ mcserver.callback = async function (msg, line) {
dataAsString.lastIndexOf("}") + 1 dataAsString.lastIndexOf("}") + 1
) )
); );
logger.debug("mcserver", "close", json);
clearTimeout(timeout); clearTimeout(timeout);
return resolve(json); return resolve(json);
}); });