mc-dashboard/lib/server-status.ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

import { status } from "minecraft-server-util";
import { execSync } from "child_process";
import { MC_SERVER_IP, MC_SERVER_PORT } from "./constants";
import { sendCommand } from "./rcon";
export type StatusResult = {
online: boolean;
starting?: boolean;
players: { online: number; max: number };
version?: string;
motd?: string;
};
export async function probeStatus(): Promise<StatusResult> {
const ping = status(MC_SERVER_IP, MC_SERVER_PORT, { timeout: 3000 }).then(
(r): StatusResult => ({
online: true,
players: { online: r.players.online, max: r.players.max },
version: r.version.name,
motd: r.motd.clean,
})
);
const rcon = sendCommand("list").then((response): StatusResult => {
const match = response.match(/There are (\d+) of a max of (\d+) players/);
return {
online: true,
players: {
online: match ? parseInt(match[1], 10) : 0,
max: match ? parseInt(match[2], 10) : 20,
},
};
});
try {
return await Promise.any([ping, rcon]);
} catch {
let starting = false;
try {
const out = execSync("systemctl is-active minecraft.service", {
encoding: "utf8",
}).trim();
starting = out === "active" || out === "activating";
} catch {}
return { online: false, starting, players: { online: 0, max: 0 } };
}
}