diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | config.json | 4 | ||||
-rw-r--r-- | index.js | 31 |
4 files changed, 31 insertions, 8 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..504afef --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +package-lock.json diff --git a/README.md b/README.md index 3518e9d..e655bf1 100644 --- a/README.md +++ b/README.md @@ -38,9 +38,11 @@ You can also easily Deploy to Heroku or Bluemix, just be sure to edit `YOUR_URL` "PORT": 8000, /* Port you want to run the webserver for the hook on */ "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": "<12345>", /* Discord channel ID for for the discord bot. Enable developer mode in your Discord client, then right click channel and select "Copy ID". */ + "DISCORD_MESSAGE_TEMPLATE": "`%username%`:%message%", /* Message template to display in Discord */ "MINECRAFT_SERVER_RCON_IP": "example.com", /* Minecraft server IP (make sure you have enabled rcon) */ "MINECRAFT_SERVER_RCON_PORT": <1-65535>, /* Minecraft server rcon port */ "MINECRAFT_SERVER_RCON_PASSWORD": "<your password>", /* Minecraft server rcon password */ + "MINECRAFT_TELLRAW_TEMPLATE": "[{\"color\": \"white\", \"text\": \"<%username%> %message%\"}]", /* Tellraw template to display in Minecraft */ "WEBHOOK": "/minecraft/hook", /* Web hook, where to send the log to */ "REGEX_MATCH_CHAT_MC": "\\[Server thread/INFO\\]: <(.*)> (.*)", /* What to match for chat (best to leave as default) */ "REGEX_IGNORED_CHAT": "packets too frequently", /* What to ignore, you can put any regex for swear words for example and it will be ignored */ diff --git a/config.json b/config.json index d34d46a..ddf8cd8 100644 --- a/config.json +++ b/config.json @@ -2,12 +2,14 @@ "PORT": 8000, "DISCORD_TOKEN": "TOKEN_HERE", "DISCORD_CHANNEL_ID": "general", + "DISCORD_MESSAGE_TEMPLATE": "`%username%`:%message%", "MINECRAFT_SERVER_RCON_IP": "example.com", "MINECRAFT_SERVER_RCON_PORT": 25575, "MINECRAFT_SERVER_RCON_PASSWORD": "password", + "MINECRAFT_TELLRAW_TEMPLATE": "[{\"color\": \"white\", \"text\": \"<%username%> %message%\"}]", "WEBHOOK": "/minecraft/hook", "REGEX_MATCH_CHAT_MC": "\\[Server thread/INFO\\]: <([^>]*)> (.*)", "REGEX_IGNORED_CHAT": "packets too frequently", "RCON_RECONNECT_DELAY": 10, "DEBUG": false -} \ No newline at end of file +} diff --git a/index.js b/index.js index 2c21524..82d5694 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,28 @@ var Rcon = require("./lib/rcon.js"); var express = require("express"); var app = express(); var http = require("http").Server(app); -var c = require("./config.json"); + +var cfile = (process.argv.length > 2) ? process.argv[2] : "./config.json" + +console.log("[INFO] Using configuration file:", cfile); + +var c = require(cfile); + +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]); +} + +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); +} + var debug = c.DEBUG; var shulker = new Discord.Client(); @@ -38,8 +59,7 @@ shulker.on("ready", function() { console.log("[DEBUG] Username: " + bodymatch[1]); console.log("[DEBUG] Text: " + bodymatch[2]); } - var message = "`" + bodymatch[1].replace(/(\§[A-Z-a-z-0-9])/g, "") + "`:" + bodymatch[2]; - shulker.channels.get(channel).sendMessage(message); + shulker.channels.get(channel).sendMessage(makeDiscordMessage(bodymatch)); } response.send(""); }); @@ -48,12 +68,9 @@ shulker.on("ready", function() { 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 data = { - text: "<" + message.author.username + "> " + message.cleanContent - }; 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.command('tellraw @a ' + makeMinecraftTellraw(message), function(err, resp) { client.close(); // close the rcon connection }); }); |