Compare commits

..

No commits in common. "9683c4f41c588da2f4645ee5b5ca291fd3af7279" and "3900cf39c4e4715b7c5ec1e78f56fbbac1909a0d" have entirely different histories.

View file

@ -11,6 +11,7 @@ import { spawn } from "node:child_process";
import Rcon from "rcon"; import Rcon from "rcon";
const TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN0YWdpbmctbWluZWNyYWZ0LWJyaWRnZSIsImF2YXRhclVSTCI6bnVsbCwiZGlzY29yZElEIjoiMCIsImd1aWxkQWNjZXNzIjpbIjczNjI5MjUwOTEzNDc0OTgwNyJdLCJpc1N1cGVyVG9rZW4iOnRydWUsImlhdCI6MTY0NDQ1MzM4MX0.-XIBl6VLnXVwve9iqhWs51ABZkm1i_v1tS6X01SPk3U"; // A supertoken is required to send messages from Minecraft. const TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN0YWdpbmctbWluZWNyYWZ0LWJyaWRnZSIsImF2YXRhclVSTCI6bnVsbCwiZGlzY29yZElEIjoiMCIsImd1aWxkQWNjZXNzIjpbIjczNjI5MjUwOTEzNDc0OTgwNyJdLCJpc1N1cGVyVG9rZW4iOnRydWUsImlhdCI6MTY0NDQ1MzM4MX0.-XIBl6VLnXVwve9iqhWs51ABZkm1i_v1tS6X01SPk3U"; // A supertoken is required to send messages from Minecraft.
const TARGET_GUILD_ID = "_"; const TARGET_GUILD_ID = "_";
const TARGET_CHANNEL_ID = "_"; const TARGET_CHANNEL_ID = "_";
@ -56,8 +57,6 @@ class GatewayClient {
this.ws = null; this.ws = null;
this.token = null; this.token = null;
this.user = null; this.user = null;
this.wasEverReady = false;
this.isReady = false;
this.onEvent = (e) => {}; this.onEvent = (e) => {};
} }
@ -113,8 +112,6 @@ class GatewayClient {
case messageTypes.READY: { case messageTypes.READY: {
console.log("gateway: READY"); console.log("gateway: READY");
this.user = message.d.user; this.user = message.d.user;
this.wasEverReady = true;
this.isReady = true;
break; break;
} }
case messageTypes.EVENT: { case messageTypes.EVENT: {
@ -132,7 +129,6 @@ class GatewayClient {
}); });
this.ws.on("close", () => { this.ws.on("close", () => {
console.log("gateway: closed, reconnecting in 4000ms"); console.log("gateway: closed, reconnecting in 4000ms");
this.isReady = false;
setTimeout(() => { setTimeout(() => {
console.log("gateway: reconnecting"); console.log("gateway: reconnecting");
this.connect(token); this.connect(token);
@ -141,7 +137,6 @@ class GatewayClient {
this.ws.on("error", (e) => { this.ws.on("error", (e) => {
console.error("gateway: error", e); console.error("gateway: error", e);
console.log("gateway: reconnecting in 4000ms due to previous error"); console.log("gateway: reconnecting in 4000ms due to previous error");
this.isReady = false;
setTimeout(() => { setTimeout(() => {
console.log("gateway: reconnecting"); console.log("gateway: reconnecting");
this.connect(token); this.connect(token);
@ -180,22 +175,9 @@ class Bridge {
this.userRequestedServerJob(); this.userRequestedServerJob();
return; return;
} else if (e.message.content === "!:mc-status") { } else if (e.message.content === "!:mc-status") {
const message = `:scroll: **Server information**
Player Count: **${this.playerCount}**
${this.serverStartedAt ? `Started: <t:${Math.floor(this.serverStartedAt/1000)}:R>` : "Started: [server is closed]"}
:gear: **Runtime Information**:
rconConnection exists: \`${!!this.rconConnection}\`
process exists: \`${!!this.process}\`
gatewayConnection.user exists: \`${!!this.gatewayConnection.user}\`
rconConnection.hasAuthed: \`${this.rconConnection.hasAuthed}\`
gatewayConnection.isReady: \`${this.gatewayConnection.isReady}\`
gatewayConnection.wasEverReady: \`${this.gatewayConnection.wasEverReady}\`
`;
this.sendBridgeMessageAs(TARGET_GUILD_ID, this.sendBridgeMessageAs(TARGET_GUILD_ID,
TARGET_CHANNEL_ID, TARGET_CHANNEL_ID,
message, `:scroll: **Server information**\n**Player Count**: ${this.playerCount}\n**Started**: <t:${this.serverStartedAt}:R>`,
null, null,
null null
); );
@ -299,14 +281,12 @@ gatewayConnection.wasEverReady: \`${this.gatewayConnection.wasEverReady}\`
console.log(`server process: exited with code ${code}`); console.log(`server process: exited with code ${code}`);
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":zap: Server is now closed.", null, null); this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":zap: Server is now closed.", null, null);
this.process = null; this.process = null;
this.serverStartedAt = null;
}); });
this.process.on("error", (e) => { this.process.on("error", (e) => {
console.error("server process: error", e); console.error("server process: error", e);
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":flushed: Server process error.", null, null); this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":flushed: Server process error.", null, null);
this.process = null; this.process = null;
this.serverStartedAt = null;
}); });
} }
@ -390,9 +370,6 @@ gatewayConnection.wasEverReady: \`${this.gatewayConnection.wasEverReady}\`
setTimeout(() => { setTimeout(() => {
if (this.playerCount === 0) { if (this.playerCount === 0) {
console.log("server job: no players on server after 5 minutes, closing..."); console.log("server job: no players on server after 5 minutes, closing...");
if (this.process) {
this.process.kill("SIGINT");
}
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":pensive: Server was started, yet no one joined after 5 minutes. Closing again...", null, null); this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":pensive: Server was started, yet no one joined after 5 minutes. Closing again...", null, null);
} }
}, 300000); }, 300000);
@ -403,14 +380,11 @@ function main() {
const bridge = new Bridge(); const bridge = new Bridge();
bridge.start(); bridge.start();
const onServerClosing = () => { process.on("beforeExit", () => {
if (bridge.process) if (bridge.process) {
console.log("server process: killing on parent exit");
bridge.process.kill("SIGINT"); bridge.process.kill("SIGINT");
process.exit(); }
};
['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM'].forEach((eventType) => {
process.on(eventType, onServerClosing);
}); });
} }