misc.mcserver: debug
This commit is contained in:
parent
48c0d64cdb
commit
1cbdda7c2d
1 changed files with 15 additions and 6 deletions
|
@ -1,4 +1,5 @@
|
|||
const Command = require("../lib/command.js");
|
||||
const logger = require("../lib/logger.js");
|
||||
const CATEGORY = "misc";
|
||||
|
||||
const {librex} = require("../../config.json");
|
||||
|
@ -753,26 +754,33 @@ mcserver.helpText = "Query a Minecraft server";
|
|||
mcserver.callback = async function (msg, line) {
|
||||
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 timeout = setTimeout(() => resolve("timeout"), 5000);
|
||||
const timeout = setTimeout(() => resolve("timeout"), 10000);
|
||||
|
||||
const client = net.createConnection({
|
||||
host: ip,
|
||||
port: port ?? 25565,
|
||||
timeout: 5000,
|
||||
port: port,
|
||||
timeout: 10000,
|
||||
});
|
||||
client.on("connect", async function () {
|
||||
client.on("connect", function () {
|
||||
logger.debug("mcserver", "connect");
|
||||
client.write(HANDSHAKE_PACKET);
|
||||
});
|
||||
let totalData = Buffer.alloc(0);
|
||||
client.on("data", function (data) {
|
||||
totalData = Buffer.concat([totalData, data]);
|
||||
logger.debug("mcserver", "data", data.length, totalData.length);
|
||||
});
|
||||
|
||||
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();
|
||||
console.log(dataAsString);
|
||||
const json = JSON.parse(
|
||||
|
@ -781,6 +789,7 @@ mcserver.callback = async function (msg, line) {
|
|||
dataAsString.lastIndexOf("}") + 1
|
||||
)
|
||||
);
|
||||
logger.debug("mcserver", "close", json);
|
||||
clearTimeout(timeout);
|
||||
return resolve(json);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue