From aced837d31535f59e367b99e1bd445fa41de4579 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 14 Jan 2023 21:43:20 +0000 Subject: Stay connected to rcon --- src/Discord.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Discord.ts b/src/Discord.ts index caf8996..be3b9ee 100644 --- a/src/Discord.ts +++ b/src/Discord.ts @@ -15,6 +15,8 @@ class Discord { channel: TextChannel | null + rcon: Rcon + mentionCache: Map constructor (config: Config, onReady?: () => void) { @@ -28,6 +30,8 @@ class Discord { this.channel = null + this.rcon = new Rcon(this.config.MINECRAFT_SERVER_RCON_IP, this.config.MINECRAFT_SERVER_RCON_PORT, this.config.DEBUG) + this.mentionCache = new Map() } @@ -57,6 +61,14 @@ class Discord { if (this.channel) { console.log(`[INFO] Using channel #${this.channel.name} (id: ${this.channel.id}) in the server "${this.channel.guild.name}"`) } + + try { + await this.rcon.auth(this.config.MINECRAFT_SERVER_RCON_PASSWORD) + } catch (e) { + console.log('[ERROR] Could not auth with the server!') + if (this.config.DEBUG) console.error(e) + process.exit(1) + } } private async onMessage (message: Message) { @@ -94,17 +106,9 @@ class Discord { if (command) { if (this.config.DEBUG) console.log(`[DEBUG] Sending command "${command}" to the server`) - const rcon = new Rcon(this.config.MINECRAFT_SERVER_RCON_IP, this.config.MINECRAFT_SERVER_RCON_PORT, this.config.DEBUG) - try { - await rcon.auth(this.config.MINECRAFT_SERVER_RCON_PASSWORD) - } catch (e) { - console.log('[ERROR] Could not auth with the server!') - if (this.config.DEBUG) console.error(e) - } - let response: string | undefined; try { - response = await rcon.command(command) + response = await this.rcon.command(command) } catch (e) { console.log('[ERROR] Could not send command!') if (this.config.DEBUG) console.error(e) @@ -116,8 +120,6 @@ class Discord { console.log('[INFO] Your Minecraft version may not support tellraw, please check MINECRAFT_TELLRAW_DOESNT_EXIST in the README') } } - - rcon.close() } } -- cgit 1.4.1