Compare commits
5 commits
3900cf39c4
...
9683c4f41c
Author | SHA1 | Date | |
---|---|---|---|
|
9683c4f41c | ||
|
490fdf26b3 | ||
|
fe7f1f8b43 | ||
|
39bfae7bc2 | ||
|
694e0e45ed |
1 changed files with 32 additions and 6 deletions
|
@ -11,7 +11,6 @@ import { spawn } from "node:child_process";
|
|||
import Rcon from "rcon";
|
||||
|
||||
|
||||
|
||||
const TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN0YWdpbmctbWluZWNyYWZ0LWJyaWRnZSIsImF2YXRhclVSTCI6bnVsbCwiZGlzY29yZElEIjoiMCIsImd1aWxkQWNjZXNzIjpbIjczNjI5MjUwOTEzNDc0OTgwNyJdLCJpc1N1cGVyVG9rZW4iOnRydWUsImlhdCI6MTY0NDQ1MzM4MX0.-XIBl6VLnXVwve9iqhWs51ABZkm1i_v1tS6X01SPk3U"; // A supertoken is required to send messages from Minecraft.
|
||||
const TARGET_GUILD_ID = "_";
|
||||
const TARGET_CHANNEL_ID = "_";
|
||||
|
@ -57,6 +56,8 @@ class GatewayClient {
|
|||
this.ws = null;
|
||||
this.token = null;
|
||||
this.user = null;
|
||||
this.wasEverReady = false;
|
||||
this.isReady = false;
|
||||
this.onEvent = (e) => {};
|
||||
}
|
||||
|
||||
|
@ -112,6 +113,8 @@ class GatewayClient {
|
|||
case messageTypes.READY: {
|
||||
console.log("gateway: READY");
|
||||
this.user = message.d.user;
|
||||
this.wasEverReady = true;
|
||||
this.isReady = true;
|
||||
break;
|
||||
}
|
||||
case messageTypes.EVENT: {
|
||||
|
@ -129,6 +132,7 @@ class GatewayClient {
|
|||
});
|
||||
this.ws.on("close", () => {
|
||||
console.log("gateway: closed, reconnecting in 4000ms");
|
||||
this.isReady = false;
|
||||
setTimeout(() => {
|
||||
console.log("gateway: reconnecting");
|
||||
this.connect(token);
|
||||
|
@ -137,6 +141,7 @@ class GatewayClient {
|
|||
this.ws.on("error", (e) => {
|
||||
console.error("gateway: error", e);
|
||||
console.log("gateway: reconnecting in 4000ms due to previous error");
|
||||
this.isReady = false;
|
||||
setTimeout(() => {
|
||||
console.log("gateway: reconnecting");
|
||||
this.connect(token);
|
||||
|
@ -175,9 +180,22 @@ class Bridge {
|
|||
this.userRequestedServerJob();
|
||||
return;
|
||||
} 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,
|
||||
TARGET_CHANNEL_ID,
|
||||
`:scroll: **Server information**\n**Player Count**: ${this.playerCount}\n**Started**: <t:${this.serverStartedAt}:R>`,
|
||||
message,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
@ -281,12 +299,14 @@ class Bridge {
|
|||
console.log(`server process: exited with code ${code}`);
|
||||
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":zap: Server is now closed.", null, null);
|
||||
this.process = null;
|
||||
this.serverStartedAt = null;
|
||||
});
|
||||
|
||||
this.process.on("error", (e) => {
|
||||
console.error("server process: error", e);
|
||||
this.sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, ":flushed: Server process error.", null, null);
|
||||
this.process = null;
|
||||
this.serverStartedAt = null;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -370,6 +390,9 @@ class Bridge {
|
|||
setTimeout(() => {
|
||||
if (this.playerCount === 0) {
|
||||
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);
|
||||
}
|
||||
}, 300000);
|
||||
|
@ -380,11 +403,14 @@ function main() {
|
|||
const bridge = new Bridge();
|
||||
bridge.start();
|
||||
|
||||
process.on("beforeExit", () => {
|
||||
if (bridge.process) {
|
||||
console.log("server process: killing on parent exit");
|
||||
const onServerClosing = () => {
|
||||
if (bridge.process)
|
||||
bridge.process.kill("SIGINT");
|
||||
}
|
||||
process.exit();
|
||||
};
|
||||
|
||||
['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM'].forEach((eventType) => {
|
||||
process.on(eventType, onServerClosing);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue