summary refs log tree commit diff homepage
path: root/src
diff options
context:
space:
mode:
authordestruc7i0n <dscdsouza@outlook.com>2020-02-05 22:07:14 -0500
committerdestruc7i0n <dscdsouza@outlook.com>2020-02-05 22:07:14 -0500
commit4d08955c59c35bcbe79a8e202fb947551e7470b8 (patch)
tree8686511d1d26a76592ea48c2db174c06fe4d1a3a /src
parentSome more cleanup (diff)
downloadshulker-4d08955c59c35bcbe79a8e202fb947551e7470b8.tar.gz
shulker-4d08955c59c35bcbe79a8e202fb947551e7470b8.zip
Allow specification of the channel name rather than id
Diffstat (limited to 'src')
-rw-r--r--src/Config.ts1
-rw-r--r--src/Discord.ts27
-rw-r--r--src/MinecraftHandler.ts15
3 files changed, 37 insertions, 6 deletions
diff --git a/src/Config.ts b/src/Config.ts
index 77fefd2..ce59ed2 100644
--- a/src/Config.ts
+++ b/src/Config.ts
@@ -5,6 +5,7 @@ export interface Config {
   WEBHOOK_URL: string
   DISCORD_TOKEN: string
   DISCORD_CHANNEL_ID: string
+  DISCORD_CHANNEL_NAME: string
   DISCORD_MESSAGE_TEMPLATE: string
 
   MINECRAFT_SERVER_RCON_IP: string
diff --git a/src/Discord.ts b/src/Discord.ts
index a0a3498..97722c2 100644
--- a/src/Discord.ts
+++ b/src/Discord.ts
@@ -1,4 +1,4 @@
-import { Client, Message, TextChannel } from 'discord.js'
+import {Client, Message, Snowflake, TextChannel} from 'discord.js'
 
 import emojiStrip from 'emoji-strip'
 import axios from 'axios'
@@ -11,26 +11,47 @@ class Discord {
   config: Config
   client: Client
 
+  channel: Snowflake
+
   constructor (config: Config, onReady?: () => void) {
     this.config = config
 
     this.client = new Client()
     if (onReady) this.client.once('ready', () => onReady())
     this.client.on('message', (message: Message) => this.onMessage(message))
+
+    this.channel = config.DISCORD_CHANNEL_ID || ''
   }
 
-  async init () {
+  public async init () {
     try {
       await this.client.login(this.config.DISCORD_TOKEN)
+      if (this.config.DISCORD_CHANNEL_NAME) this.getChannelIdFromName(this.config.DISCORD_CHANNEL_NAME)
     } catch (e) {
       console.log('[ERROR] Could not authenticate with Discord: ' + e)
       if (this.config.DEBUG) console.error(e)
     }
   }
 
+  private getChannelIdFromName (name: string) {
+    // remove the # if there is one
+    if (name.startsWith('#')) name = name.substring(1, name.length)
+    // @ts-ignore
+    const channel: TextChannel = this.client.channels.find((c: TextChannel) => c.type === 'text' && c.name === name && !c.deleted)
+    if (channel) {
+      this.channel = channel.id
+      console.log(`[INFO] Found channel #${channel.name} (id: ${channel.id}) in the server "${channel.guild.name}"`)
+    } else {
+      console.log(`[INFO] Could not find channel ${name}! Check that the name is correct or use the ID of the channel instead (DISCORD_CHANNEL_ID)!`)
+      process.exit(1)
+    }
+  }
+
   private async onMessage (message: Message) {
+    // no channel, done
+    if (!this.channel) return
     // don't want to check other channels
-    if (message.channel.id !== this.config.DISCORD_CHANNEL_ID || message.channel.type !== 'text') return
+    if (message.channel.id !== this.channel || message.channel.type !== 'text') return
     // if using webhooks, ignore this!
     if (this.config.USE_WEBHOOKS && message.webhookID) return
     // if the same user as the bot, ignore
diff --git a/src/MinecraftHandler.ts b/src/MinecraftHandler.ts
index 20bccf5..0e4e5d7 100644
--- a/src/MinecraftHandler.ts
+++ b/src/MinecraftHandler.ts
@@ -44,8 +44,10 @@ class MinecraftHandler {
     const logLineData = data.match(logLineDataRegex)
 
     if (!logLineDataRegex.test(data) || !logLineData) {
-      console.log('[ERROR] Regex could not match the string! Please verify it is correct!')
-      console.log('Received: "' + data + '", Regex matches lines that start with: "' + this.config.REGEX_SERVER_PREFIX + '"')
+      if (this.config.DEBUG) {
+        console.log('[DEBUG] Regex could not match the string:')
+        console.log('Received: "' + data + '", Regex matches lines that start with: "' + this.config.REGEX_SERVER_PREFIX + '"')
+      }
       return null
     }
 
@@ -164,7 +166,14 @@ class MinecraftHandler {
 
         mcPath = (defaultPath ? '/' : '') + path.join(mcPath, '/logs/latest.log')
 
-        console.log(`  \`tail -F ${mcPath} | grep --line-buffered ": <" | while read x ; do echo -ne $x | curl -X POST -d @- http://${url}:${port}${this.config.WEBHOOK} ; done\``)
+        let grepMatch = ': <'
+        if (this.config.SHOW_PLAYER_DEATH || this.config.SHOW_PLAYER_ME || this.config.SHOW_PLAYER_ADVANCEMENT || this.config.SHOW_PLAYER_CONN_STAT) {
+          grepMatch = this.config.REGEX_SERVER_PREFIX
+        }
+        console.log(`  \`tail -F ${mcPath} | grep --line-buffered "${grepMatch}" | while read x ; do echo -ne $x | curl -X POST -d @- http://${url}:${port}${this.config.WEBHOOK} ; done\``)
+        if (grepMatch !== ': <') {
+          console.log('       Please note that the above command can send a lot of requests to the server. Disable the non-text messages (such as "SHOW_PLAYER_CONN_STAT") to reduce this if necessary.')
+        }
       }
     })
   }