summary refs log tree commit diff homepage
path: root/src
diff options
context:
space:
mode:
authordestruc7i0n <dscdsouza@outlook.com>2020-04-29 10:00:30 -0400
committerdestruc7i0n <dscdsouza@outlook.com>2020-04-29 10:00:30 -0400
commite288b74fabf5a6d69ebdf32182fadd35b2ace1b3 (patch)
tree072b3ab2a4770fbaee5a16cf4bae6b081e1bf21a /src
parentUpdate README.md (diff)
downloadshulker-e288b74fabf5a6d69ebdf32182fadd35b2ace1b3.tar.gz
shulker-e288b74fabf5a6d69ebdf32182fadd35b2ace1b3.zip
add the ability to handle webhook messages
Diffstat (limited to 'src')
-rw-r--r--src/Config.ts1
-rw-r--r--src/Discord.ts37
-rw-r--r--src/MinecraftHandler.ts3
3 files changed, 38 insertions, 3 deletions
diff --git a/src/Config.ts b/src/Config.ts
index ce59ed2..dff0b10 100644
--- a/src/Config.ts
+++ b/src/Config.ts
@@ -3,6 +3,7 @@ export interface Config {
 
   USE_WEBHOOKS: boolean
   WEBHOOK_URL: string
+  IGNORE_WEBHOOKS: string
   DISCORD_TOKEN: string
   DISCORD_CHANNEL_ID: string
   DISCORD_CHANNEL_NAME: string
diff --git a/src/Discord.ts b/src/Discord.ts
index bc15fac..d8263e8 100644
--- a/src/Discord.ts
+++ b/src/Discord.ts
@@ -48,13 +48,46 @@ class Discord {
     }
   }
 
+  private parseDiscordWebhook (url: string) {
+    const re = /discordapp.com\/api\/webhooks\/([^\/]+)\/([^\/]+)/
+
+    // the is of the webhook
+    let id = null
+    let token = null
+
+    if (!re.test(url)) {
+      // In case the url changes at some point, I will warn if it still works
+      console.log('[WARN] The Webhook URL may not be valid!')
+    } else {
+      const match = url.match(re)
+      if (match) {
+        id = match[1]
+        token = match[2]
+      }
+    }
+
+    return { id, token }
+  }
+
   private async onMessage (message: Message) {
     // no channel, done
     if (!this.channel) return
     // don't want to check other channels
     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 (message.webhookID) {
+      // if ignoring all webhooks, ignore
+      if (this.config.IGNORE_WEBHOOKS) {
+        return
+      } else if (this.config.USE_WEBHOOKS) {
+        // otherwise, ignore all webhooks that are not the same as this one
+        const { id } = this.parseDiscordWebhook(this.config.WEBHOOK_URL)
+        if (id === message.webhookID) {
+          if (this.config.DEBUG) console.log('[INFO] Ignoring webhook from self')
+          return
+        }
+      }
+    }
     // if the same user as the bot, ignore
     if (message.author.id === this.client.user.id) return
     // ignore any attachments
@@ -81,6 +114,8 @@ class Discord {
       command = `/tellraw @a ${this.makeMinecraftTellraw(message)}`
     }
 
+    if (this.config.DEBUG) console.log(`[DEBUG] Sending command "${command}" to the server`)
+
     if (command) {
       await rcon.command(command).catch((e) => {
         console.log('[ERROR] Could not send command!')
diff --git a/src/MinecraftHandler.ts b/src/MinecraftHandler.ts
index 0e4e5d7..96b525a 100644
--- a/src/MinecraftHandler.ts
+++ b/src/MinecraftHandler.ts
@@ -158,8 +158,7 @@ class MinecraftHandler {
 
         console.log('[INFO] Please enter the following command on your server running the Minecraft server:')
         if (defaultPath) {
-          console.log('       Replace "PATH_TO_MINECRAFT_SERVER_INSTALL" with the path to your Minecraft server install')
-          if (defaultUrl) console.log('       and "YOUR_URL" with the URL/IP of the server running Shulker!')
+          console.log('       Replace "PATH_TO_MINECRAFT_SERVER_INSTALL" with the path to your Minecraft server install' + (defaultUrl ? ' and "YOUR_URL" with the URL/IP of the server running Shulker.' : ''))
         } else {
           if (defaultUrl) console.log('       Replace "YOUR_URL" with the URL/IP of the server running Shulker')
         }