summary refs log tree commit diff homepage
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
parentUpdate README.md (diff)
downloadshulker-e288b74fabf5a6d69ebdf32182fadd35b2ace1b3.tar.gz
shulker-e288b74fabf5a6d69ebdf32182fadd35b2ace1b3.zip
add the ability to handle webhook messages
-rw-r--r--.gitignore1
-rw-r--r--README.md1
-rw-r--r--config.json1
-rw-r--r--src/Config.ts1
-rw-r--r--src/Discord.ts37
-rw-r--r--src/MinecraftHandler.ts3
6 files changed, 41 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index effcb88..cb53203 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ package-lock.json
 yarn-error.log
 .idea
 build
+.DS_Store
diff --git a/README.md b/README.md
index fe48837..49f017e 100644
--- a/README.md
+++ b/README.md
@@ -57,6 +57,7 @@ You can also easily Deploy to Heroku and the like, just be sure to edit `YOUR_UR
     
     "USE_WEBHOOKS": true, /* If you want to use snazzy webhooks */
     "WEBHOOK_URL": "DISCORD_WEBHOOK_URL_HERE", /* Be sure to create a webhook in the channel settings and place it here! */
+    "IGNORE_WEBHOOKS": true, /* Ignore any messages that are sent by webhooks. If disabled, then all webhooks but those sent from the configured webhook will be handled as well */
     "DISCORD_TOKEN": "<12345>", /* Discord bot token. [Click here](https://discordapp.com/developers/applications/me) to create you application and add a bot to it. */
     "DISCORD_CHANNEL_ID": "<channel>", /* Discord channel ID for for the discord bot. Enable developer mode in your Discord client, then right click channel and select "Copy ID". */
     "DISCORD_CHANNEL_NAME": "#<channel name>" /* The Discord channel name. It is recommended to use the ID if the bot is in multiple servers. The ID will take precedence. */
diff --git a/config.json b/config.json
index 43d7140..2ca92fb 100644
--- a/config.json
+++ b/config.json
@@ -3,6 +3,7 @@
 
     "USE_WEBHOOKS": true,
     "WEBHOOK_URL": "DISCORD_WEBHOOK_URL_HERE",
+    "IGNORE_WEBHOOKS": true,
     "DISCORD_TOKEN": "TOKEN_HERE",
     "DISCORD_CHANNEL_ID": "",
     "DISCORD_CHANNEL_NAME": "#bot",
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')
         }