summary refs log tree commit diff homepage
path: root/index.js
diff options
context:
space:
mode:
authordestruc7i0n <6181960+destruc7i0n@users.noreply.github.com>2018-11-11 15:56:49 -0500
committerGitHub <noreply@github.com>2018-11-11 15:56:49 -0500
commitb8781c45e095832e273248e73528328eaefe4138 (patch)
tree29b6e17e56059403410df4810917eb87b6008d19 /index.js
parentMerge pull request #29 from Arcensoth/master (diff)
parentLet's make this usable again! (diff)
downloadshulker-b8781c45e095832e273248e73528328eaefe4138.tar.gz
shulker-b8781c45e095832e273248e73528328eaefe4138.zip
Merge pull request #33 from destruc7i0n/webhooks
Webhooks!
Diffstat (limited to '')
-rw-r--r--index.js192
1 files changed, 114 insertions, 78 deletions
diff --git a/index.js b/index.js
index 82d5694..fae323c 100644
--- a/index.js
+++ b/index.js
@@ -1,93 +1,129 @@
-/*jslint bitwise: true, node: true */
-'use strict';
+'use strict'
 
-var Discord = require("discord.js");
-var Rcon = require("./lib/rcon.js");
-var express = require("express");
-var app = express();
-var http = require("http").Server(app);
+const Discord = require('discord.js')
+const Rcon = require('./lib/rcon.js')
+const express = require('express')
+const axios = require('axios')
+const emojiStrip = require('emoji-strip')
+const app = express()
+const http = require('http').Server(app)
 
-var cfile = (process.argv.length > 2) ? process.argv[2] : "./config.json"
+const configFile = (process.argv.length > 2) ? process.argv[2] : './config.json'
 
-console.log("[INFO] Using configuration file:", cfile);
+console.log('[INFO] Using configuration file:', configFile)
 
-var c = require(cfile);
+const c = require(configFile)
 
-function makeDiscordMessage(bodymatch) {
-    // make a discord message string by formatting the configured template with the given parameters
-    return c.DISCORD_MESSAGE_TEMPLATE
-        .replace("%username%", bodymatch[1].replace(/(\§[A-Z-a-z-0-9])/g, ""))
-        .replace("%message%", bodymatch[2]);
+const fixUsername = (username) => username.replace(/(§[A-Z-a-z0-9])/g, '')
+
+function makeDiscordMessage (username, message) {
+  // make a discord message string by formatting the configured template with the given parameters
+  return c.DISCORD_MESSAGE_TEMPLATE
+    .replace('%username%', username)
+    .replace('%message%', message)
+}
+
+function makeDiscordWebhook (username, message) {
+  return {
+    username: username,
+    content: message,
+    'avatar_url': `https://minotar.net/helm/${username}/256.png`
+  }
 }
 
-function makeMinecraftTellraw(message) {
-    // same as the discord side but with discord message parameters
-    return c.MINECRAFT_TELLRAW_TEMPLATE
-        .replace("%username%", message.author.username)
-        .replace("%discriminator%", message.author.discriminator)
-        .replace("%message%", message.cleanContent);
+function makeMinecraftTellraw (message) {
+  // same as the discord side but with discord message parameters
+  const username = emojiStrip(message.author.username)
+  const discriminator = message.author.discriminator
+  const text = emojiStrip(message.cleanContent)
+
+  return c.MINECRAFT_TELLRAW_TEMPLATE
+    .replace('%username%', username)
+    .replace('%discriminator%', discriminator)
+    .replace('%message%', text)
 }
 
-var debug = c.DEBUG;
-var shulker = new Discord.Client();
-
-var rconTimeout;
-
-app.use(function(request, response, next) {
-    request.rawBody = "";
-    request.setEncoding("utf8");
-
-    request.on("data", function(chunk) {
-        request.rawBody += chunk;
-    });
-
-    request.on("end", function() {
-        next();
-    });
-});
-
-shulker.on("ready", function() {
-    var channel = c.DISCORD_CHANNEL_ID;
-    app.post(c.WEBHOOK, function(request, response) {
-        var body = request.rawBody;
-        console.log("[INFO] Recieved " + body);
-        var re = new RegExp(c.REGEX_MATCH_CHAT_MC);
-        var ignored = new RegExp(c.REGEX_IGNORED_CHAT);
-        if (!ignored.test(body)) {
-            var bodymatch = body.match(re);
-            if (debug) {
-                console.log("[DEBUG] Username: " + bodymatch[1]);
-                console.log("[DEBUG] Text: " + bodymatch[2]);
-            }
-            shulker.channels.get(channel).sendMessage(makeDiscordMessage(bodymatch));
-        }
-        response.send("");
-    });
-});
-
-shulker.on("message", function(message) {
-    if (message.channel.id === shulker.channels.get(c.DISCORD_CHANNEL_ID).id) {
-        if (message.author.id !== shulker.user.id) {
-            var client = new Rcon(c.MINECRAFT_SERVER_RCON_IP, c.MINECRAFT_SERVER_RCON_PORT); // create rcon client
-            client.auth(c.MINECRAFT_SERVER_RCON_PASSWORD, function(err){ // only authenticate when needed
-                client.command('tellraw @a ' + makeMinecraftTellraw(message), function(err, resp) {
-                    client.close(); // close the rcon connection
-                });
-            });
-        }
+const debug = c.DEBUG
+const shulker = new Discord.Client()
+
+app.use(function (request, response, next) {
+  request.rawBody = ''
+  request.setEncoding('utf8')
+
+  request.on('data', function (chunk) {
+    request.rawBody += chunk
+  })
+
+  request.on('end', function () {
+    next()
+  })
+})
+
+shulker.on('ready', function () {
+  app.post(c.WEBHOOK, function (request, response) {
+    const body = request.rawBody
+    console.log('[INFO] Recieved ' + body)
+    const re = new RegExp(c.REGEX_MATCH_CHAT_MC)
+    const ignored = new RegExp(c.REGEX_IGNORED_CHAT)
+    if (!ignored.test(body)) {
+      const bodymatch = body.match(re)
+      const username = fixUsername(bodymatch[1])
+      const message = bodymatch[2]
+      if (debug) {
+        console.log('[DEBUG] Username: ' + bodymatch[1])
+        console.log('[DEBUG] Text: ' + bodymatch[2])
+      }
+      if (c.USE_WEBHOOKS) {
+        const webhook = makeDiscordWebhook(username, message)
+        axios.post(c.WEBHOOK_URL, {
+          ...webhook
+        }, {
+          headers: {
+            'Content-Type': 'application/json'
+          }
+        })
+      } else {
+        // find the channel
+        const channel = shulker.channels.find((ch) => ch.id === c.DISCORD_CHANNEL_ID && ch.type === 'text')
+        channel.send(makeDiscordMessage(username, message))
+      }
+    }
+    response.send('')
+  })
+})
+
+shulker.on('message', function (message) {
+  if (message.channel.id === c.DISCORD_CHANNEL_ID && message.channel.type === 'text') {
+    if (c.USE_WEBHOOKS && message.webhookID) {
+      return // ignore webhooks if using a webhook
+    }
+    if (message.author.id !== shulker.user.id) {
+      if (message.attachments.length) { // skip images/attachments
+        return
+      }
+      const client = new Rcon(c.MINECRAFT_SERVER_RCON_IP, c.MINECRAFT_SERVER_RCON_PORT) // create rcon client
+      client.auth(c.MINECRAFT_SERVER_RCON_PASSWORD, function () { // only authenticate when needed
+        client.command('tellraw @a ' + makeMinecraftTellraw(message), function (err) {
+          if (err) {
+            console.log('[ERROR]', err)
+          }
+          client.close() // close the rcon connection
+        })
+      })
     }
-});
+  }
+})
 
-shulker.login(c.DISCORD_TOKEN);
+shulker.login(c.DISCORD_TOKEN)
 
-var ipaddress = process.env.OPENSHIFT_NODEJS_IP || process.env.IP || "127.0.0.1";
-var serverport = process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || c.PORT;
+const ipaddress = process.env.OPENSHIFT_NODEJS_IP || process.env.IP || '127.0.0.1'
+const serverport = process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || c.PORT
 if (process.env.OPENSHIFT_NODEJS_IP !== undefined) {
-    http.listen(serverport, ipaddress, function() {
-        console.log("[INFO] Bot listening on *:" + serverport);
-    });
+  http.listen(serverport, ipaddress, function () {
+    console.log('[INFO] Bot listening on *:' + serverport)
+  })
 } else {
-    http.listen(serverport, function() {
-        console.log("[INFO] Bot listening on *:" + c.PORT);
-    });
+  http.listen(serverport, function () {
+    console.log('[INFO] Bot listening on *:' + c.PORT)
+  })
 }