1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/*jslint bitwise: true, node: true */
'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);
var cfile = (process.argv.length > 2) ? process.argv[2] : "./config.json"
console.log("[INFO] Using configuration file:", cfile);
var c = require(cfile);
function makeTellraw(message) {
// make a tellraw string by formatting the configured template with the given message
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();
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]);
}
var message = "`" + bodymatch[1].replace(/(\ยง[A-Z-a-z-0-9])/g, "") + "`:" + bodymatch[2];
shulker.channels.get(channel).sendMessage(message);
}
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 ' + makeTellraw(message), function(err, resp) {
client.close(); // close the rcon connection
});
});
}
}
});
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;
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);
});
}
|