diff options
author | destruc7i0n <6181960+destruc7i0n@users.noreply.github.com> | 2018-08-30 10:59:26 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-30 10:59:26 -0400 |
commit | e3f47e35372617d9d520b52543c499d21b186c22 (patch) | |
tree | 7b8cee8403ce5d8c0aea68149d46ee58efaa2c0d /index.js | |
parent | Merge pull request #21 from destruc7i0n/remove-ci (diff) | |
parent | Allow configurable discord format (diff) | |
download | shulker-e3f47e35372617d9d520b52543c499d21b186c22.tar.gz shulker-e3f47e35372617d9d520b52543c499d21b186c22.zip |
Merge pull request #29 from Arcensoth/master
Allow configurable message formats
Diffstat (limited to '')
-rw-r--r-- | index.js | 31 |
1 files changed, 24 insertions, 7 deletions
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 }); }); |