summary refs log tree commit diff homepage
path: root/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'index.js')
-rw-r--r--index.js117
1 files changed, 86 insertions, 31 deletions
diff --git a/index.js b/index.js
index fae323c..e7b399e 100644
--- a/index.js
+++ b/index.js
@@ -5,8 +5,8 @@ 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)
+const { Tail } = require('tail')
+const fs = require('fs')
 
 const configFile = (process.argv.length > 2) ? process.argv[2] : './config.json'
 
@@ -14,16 +14,45 @@ console.log('[INFO] Using configuration file:', configFile)
 
 const c = require(configFile)
 
-const fixUsername = (username) => username.replace(/(§[A-Z-a-z0-9])/g, '')
+let app = null
+let tail = null
+
+function fixUsername (username) {
+  return username.replace(/(§[A-Z-a-z0-9])/g, '')
+}
+
+// replace mentions with discriminator with the actual mention
+function replaceDiscordMentions (message) {
+  if (c.ALLOW_USER_MENTIONS) {
+    const possibleMentions = message.match(/@(\S+)/gim)
+    if (possibleMentions) {
+      for (let mention of possibleMentions) {
+        const mentionParts = mention.split('#')
+        let username = mentionParts[0].replace('@', '')
+        if (mentionParts.length > 1) {
+          const user = shulker.users.find(user => user.username === username && user.discriminator === mentionParts[1])
+          if (user) {
+            message = message.replace(mention, '<@' + user.id + '>')
+          }
+        }
+      }
+    }
+  }
+  return message
+}
 
 function makeDiscordMessage (username, message) {
   // make a discord message string by formatting the configured template with the given parameters
+  message = replaceDiscordMentions(message)
+
   return c.DISCORD_MESSAGE_TEMPLATE
     .replace('%username%', username)
     .replace('%message%', message)
 }
 
 function makeDiscordWebhook (username, message) {
+  message = replaceDiscordMentions(message)
+
   return {
     username: username,
     content: message,
@@ -36,32 +65,70 @@ function makeMinecraftTellraw (message) {
   const username = emojiStrip(message.author.username)
   const discriminator = message.author.discriminator
   const text = emojiStrip(message.cleanContent)
+  // hastily use JSON to encode the strings
+  const variables = JSON.parse(JSON.stringify({ username, discriminator, text }))
 
   return c.MINECRAFT_TELLRAW_TEMPLATE
-    .replace('%username%', username)
-    .replace('%discriminator%', discriminator)
-    .replace('%message%', text)
+    .replace('%username%', variables.username)
+    .replace('%discriminator%', variables.discriminator)
+    .replace('%message%', variables.text)
 }
 
 const debug = c.DEBUG
 const shulker = new Discord.Client()
 
-app.use(function (request, response, next) {
-  request.rawBody = ''
-  request.setEncoding('utf8')
+function initApp () {
+  // run a server if not local
+  if (!c.IS_LOCAL_FILE) {
+    app = express()
+    const http = require('http').Server(app)
 
-  request.on('data', function (chunk) {
-    request.rawBody += chunk
-  })
+    app.use(function (request, response, next) {
+      request.rawBody = ''
+      request.setEncoding('utf8')
 
-  request.on('end', function () {
-    next()
-  })
-})
+      request.on('data', function (chunk) {
+        request.rawBody += chunk
+      })
+
+      request.on('end', function () {
+        next()
+      })
+    })
+
+    const serverport = process.env.PORT || c.PORT
+
+    http.listen(serverport, function () {
+      console.log('[INFO] Bot listening on *:' + serverport)
+    })
+  } else {
+    if (fs.existsSync(c.LOCAL_FILE_PATH)) {
+      console.log('[INFO] Using configuration for local file at "' + c.LOCAL_FILE_PATH + '"')
+      tail = new Tail(c.LOCAL_FILE_PATH)
+    } else {
+      throw new Error('[ERROR] Local file not found at "' + c.LOCAL_FILE_PATH + '"')
+    }
+  }
+}
+
+function watch (callback) {
+  if (c.IS_LOCAL_FILE) {
+    tail.on('line', function (data) {
+      // ensure that this is a message
+      if (data.indexOf(': <') !== -1) {
+        callback(data)
+      }
+    })
+  } else {
+    app.post(c.WEBHOOK, function (request, response) {
+      callback(request.rawBody)
+      response.send('')
+    })
+  }
+}
 
 shulker.on('ready', function () {
-  app.post(c.WEBHOOK, function (request, response) {
-    const body = request.rawBody
+  watch(function (body) {
     console.log('[INFO] Recieved ' + body)
     const re = new RegExp(c.REGEX_MATCH_CHAT_MC)
     const ignored = new RegExp(c.REGEX_IGNORED_CHAT)
@@ -88,7 +155,6 @@ shulker.on('ready', function () {
         channel.send(makeDiscordMessage(username, message))
       }
     }
-    response.send('')
   })
 })
 
@@ -114,16 +180,5 @@ shulker.on('message', function (message) {
   }
 })
 
+initApp()
 shulker.login(c.DISCORD_TOKEN)
-
-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)
-  })
-} else {
-  http.listen(serverport, function () {
-    console.log('[INFO] Bot listening on *:' + c.PORT)
-  })
-}