diff options
Diffstat (limited to '')
-rw-r--r-- | config.json | 7 | ||||
-rw-r--r-- | index.js | 38 | ||||
-rw-r--r-- | lib/rcon.js | 115 | ||||
-rw-r--r-- | package.json | 3 |
4 files changed, 130 insertions, 33 deletions
diff --git a/config.json b/config.json index 07e3895..d34d46a 100644 --- a/config.json +++ b/config.json @@ -1,13 +1,12 @@ { "PORT": 8000, - "DISCORD_EMAIL": "example@example.com", - "DISCORD_PASSWORD": "password123", - "DISCORD_CHANNEL": "general", + "DISCORD_TOKEN": "TOKEN_HERE", + "DISCORD_CHANNEL_ID": "general", "MINECRAFT_SERVER_RCON_IP": "example.com", "MINECRAFT_SERVER_RCON_PORT": 25575, "MINECRAFT_SERVER_RCON_PASSWORD": "password", "WEBHOOK": "/minecraft/hook", - "REGEX_MATCH_CHAT_MC": "\\[Server thread/INFO\\]: <(.*)> (.*)", + "REGEX_MATCH_CHAT_MC": "\\[Server thread/INFO\\]: <([^>]*)> (.*)", "REGEX_IGNORED_CHAT": "packets too frequently", "RCON_RECONNECT_DELAY": 10, "DEBUG": false diff --git a/index.js b/index.js index 435ac15..8dcff4d 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ 'use strict'; var Discord = require("discord.js"); -var Rcon = require("rcon"); +var Rcon = require("./lib/rcon.js"); var express = require("express"); var app = express(); var http = require("http").Server(app); @@ -10,29 +10,8 @@ var c = require("./config.json"); var debug = c.DEBUG; var shulker = new Discord.Client(); -var client = new Rcon(c.MINECRAFT_SERVER_RCON_IP, c.MINECRAFT_SERVER_RCON_PORT, c.MINECRAFT_SERVER_RCON_PASSWORD); var rconTimeout; -client.on("auth", function() { - console.log("[INFO] Authenticated with " + c.MINECRAFT_SERVER_RCON_IP + ":" + c.MINECRAFT_SERVER_RCON_PORT); -}).on("response", function(str) { - if (debug && str) { - console.log("[DEBUG] Got response: " + str); - } -}).on("end", function() { - console.log("[INFO] Rcon closed!"); -}).on("error", function() { - if (typeof rconTimeout === 'undefined') { - client.disconnect(); - rconTimeout = setTimeout(function() { - client.connect(); - rconTimeout = undefined; - }, c.RCON_RECONNECT_DELAY * 1000); - } -}); - -client.connect(); - app.use(function(request, response, next) { request.rawBody = ""; request.setEncoding("utf8"); @@ -47,7 +26,7 @@ app.use(function(request, response, next) { }); shulker.on("ready", function() { - var channel = shulker.channels.get("name", c.DISCORD_CHANNEL).id; + var channel = c.DISCORD_CHANNEL_ID; app.post(c.WEBHOOK, function(request, response) { var body = request.rawBody; console.log("[INFO] Recieved " + body); @@ -59,7 +38,7 @@ shulker.on("ready", function() { console.log("[DEBUG] Username: " + bodymatch[1]); console.log("[DEBUG] Text: " + bodymatch[2]); } - var message = "**" + bodymatch[1] + "**: " + bodymatch[2]; + var message = "`" + bodymatch[1].replace(/(\ยง[A-Z-a-z-0-9])/g, "") + "`:" + bodymatch[2]; shulker.channels.get("id", channel).sendMessage(message); } response.send(""); @@ -70,14 +49,19 @@ shulker.on("message", function(message) { if (message.channel.id === shulker.channels.get("name", c.DISCORD_CHANNEL).id) { if (message.author.id !== shulker.user.id) { var data = { - text: "<" + message.author.username + "> " + message.content + text: "<" + message.author.username + "> " + message.cleanContent }; - client.send('tellraw @a ["",' + JSON.stringify(data) + ']'); + 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 ["",' + JSON.stringify(data) + ']', function(err, resp) { + client.close(); // close the rcon connection + }); + }) } } }); -shulker.login(c.DISCORD_EMAIL, c.DISCORD_PASSWORD); +shulker.loginWithToken(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; diff --git a/lib/rcon.js b/lib/rcon.js new file mode 100644 index 0000000..f75b481 --- /dev/null +++ b/lib/rcon.js @@ -0,0 +1,115 @@ +// Credits to M4GNV5 for this library to reduce dependancies + +var net = require("net"); +var c = require("../config.json"); +var debug = c.DEBUG; + +module.exports = Rcon; + +function Rcon(ip, port) +{ + var self = this; + self.rconTimeout = undefined; + self.nextId = 0; + self.connected = false; + self.authed = false; + self.packages = []; + + self.socket = net.connect(port, ip, function() { + self.connected = true; + console.log("[INFO] Authenticated with " + ip + ":" + port); + }); + + self.socket.on("data", function(data) + { + var length = data.readInt32LE(0); + var id = data.readInt32LE(4); + var type = data.readInt32LE(8); + var response = data.toString("ascii", 12, data.length - 2); + + if(self.packages[id]) + { + self.packages[id](type, response); + } + else + { + console.log("unexpected rcon response", id, type, response); + } + }).on("end", function() + { + if(debug) { + console.log("[DEBUG] Rcon closed!"); + } + }); +} +Rcon.timeout = 5000; + +Rcon.prototype.close = function() +{ + this.connected = false; + this.socket.end(); +} + +Rcon.prototype.auth = function(pw, cb) +{ + var self = this; + self.pass = pw; + self.cb = cb; + + if(self.authed) + throw new Error("already authed"); + + if(self.connected) + doAuth(); + else + self.socket.on("connect", doAuth); + + function doAuth() + { + self.sendPackage(3, pw, cb); + } +}; + +Rcon.prototype.command = function(cmd, cb) +{ + this.sendPackage(2, cmd, cb); +}; + +Rcon.prototype.sendPackage = function(type, payload, cb) +{ + var self = this; + var id = self.nextId; + self.nextId++; + + if(!self.connected) + throw new Error("Cannot send package while not connected"); + + var length = 14 + payload.length; + var buff = new Buffer(length); + buff.writeInt32LE(length - 4, 0); + buff.writeInt32LE(id, 4); + buff.writeInt32LE(type, 8); + + buff.write(payload, 12); + buff.writeInt8(0, length - 2); + buff.writeInt8(0, length - 1); + + self.socket.write(buff); + + var timeout = setTimeout(function() + { + delete self.packages[id]; + cb("Server sent no request in " + Rcon.timeout / 1000 + " seconds"); + }, Rcon.timeout); + + self.packages[id] = function(type, response) + { + clearTimeout(timeout); + var err = type >= 0 ? false : "Server sent package code " + type; + if(debug) + { + console.log("[DEBUG] Recieved response: "+response); + } + cb(err, response, type); + } +} \ No newline at end of file diff --git a/package.json b/package.json index 654d5ba..ceeb121 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,7 @@ "homepage": "https://github.com/destruc7i0n/shulker#readme", "dependencies": { "discord.js": "^6.1.0", - "express": "^4.13.3", - "rcon": "^1.0.1" + "express": "^4.13.3" }, "devDependencies": { "jshint": "^2.9.1-rc2" |