add join and leave messages to minecraft script

This commit is contained in:
hippoz 2022-02-15 13:41:30 +02:00
parent 5ce0b81864
commit d24929da0c
Signed by: hippoz
GPG key ID: 7C52899193467641

View file

@ -23,6 +23,8 @@ const messageTypes = {
}; };
const chatMessageRegex = /^\[(?:.*?)\]: \<(?<username>.*)\> (?<message>.*)/; const chatMessageRegex = /^\[(?:.*?)\]: \<(?<username>.*)\> (?<message>.*)/;
const joinNotificationRegex = /^\[(?:.*?)\]: (?<username>.*) joined the game/;
const leaveNotificationRegex = /^\[(?:.*?)\]: (?<username>.*) left the game/;
const rconConnection = new Rcon("localhost", "25575", process.env.RCON_PASSWORD); const rconConnection = new Rcon("localhost", "25575", process.env.RCON_PASSWORD);
export default class GatewayClient { export default class GatewayClient {
@ -91,7 +93,7 @@ export default class GatewayClient {
console.log("gateway: open"); console.log("gateway: open");
}); });
this.ws.on("close", () => { this.ws.on("close", () => {
console.log("gateway: closed"); console.log("gateway: closed, reconnecting in 4000ms");
setTimeout(() => { setTimeout(() => {
console.log("gateway: reconnecting"); console.log("gateway: reconnecting");
this.connect(token); this.connect(token);
@ -141,10 +143,10 @@ async function main() {
rconConnection.on("error", (e) => { rconConnection.on("error", (e) => {
console.error("rcon: got error", e); console.error("rcon: got error", e);
if (!rconConnection.hasAuthed) { if (!rconConnection.hasAuthed) {
console.log("rcon: reconnecting in 1200ms due to error before hasAuthed"); console.log("rcon: reconnecting in 5000ms due to error before hasAuthed (server might not be up yet?)");
setTimeout(() => { setTimeout(() => {
rconConnection.connect(); rconConnection.connect();
}, 1200); }, 5000);
} }
}); });
const gateway = new GatewayClient(GATEWAY_ORIGIN); const gateway = new GatewayClient(GATEWAY_ORIGIN);
@ -160,12 +162,24 @@ async function main() {
process.stdin.on("data", async (rawDataBuffer) => { process.stdin.on("data", async (rawDataBuffer) => {
const stringData = rawDataBuffer.toString().trim(); const stringData = rawDataBuffer.toString().trim();
console.log(stringData); console.log(stringData);
const result = chatMessageRegex.exec(stringData);
if (!result)
return;
const { username, message } = result.groups;
await sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, message, username, null); const joinResult = joinNotificationRegex.exec(stringData);
if (joinResult) {
await sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, `**${joinResult.groups.username}** joined the game`, null, null);
return;
}
const leaveResult = leaveNotificationRegex.exec(stringData);
if (leaveResult) {
await sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, `**${leaveResult.groups.username}** left the game`, null, null);
return;
}
const messageResult = chatMessageRegex.exec(stringData);
if (messageResult) {
await sendBridgeMessageAs(TARGET_GUILD_ID, TARGET_CHANNEL_ID, messageResult.groups.message, messageResult.groups.username, null);
return;
}
}); });
} }