forked from amalbansode/Discord-Minecraft-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
253 lines (228 loc) · 9.21 KB
/
bot.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* MinecraftDiscordBot.js — A script to power a Discord bot that checks
* the status of specified Minecraft servers.
* Original author: Amal Bansode
* Fork by: Cedric Poottaren
* https://cedric.poottaren.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Load required modules
const Discord = require('discord.js');
const config = require('./config.json');
const serv = require('./minestat');
const client = new Discord.Client();
// Load mcron variables
const mcron_loc = config["mcrcon"].path;
const mcron_host = config["mcrcon"].host;
const mcron_pwd = config["mcrcon"].password;
// Load minecraft server variables
const mc_server_addr = config["minecraft"].host;
const mc_server_name = config["minecraft"].hostname;
const mc_server_port = config["minecraft"].port;
// Number of minutes to refresh server count after (recommended >= 1 min; default = 2)
const REFRESH_INTERVAL = 2 * 60 * 1000; // 2 minutes.
// IP and port of Minecraft servers
const servIP = {
server_name: `${mc_server_addr}:${mc_server_port}`
};
let intervalId;
// Executes as long as bot is online.
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log('Ready...');
checkStatus();
// Check status every 4 minutes
intervalId = client.setInterval(checkStatus, REFRESH_INTERVAL);
});
// Executes upon reception of message.
// States IPs of Minecraft servers respectively if '!ip' is typed into chat.
client.on('message', async msg => {
if (msg.content === '!ip') {
await checkStatus();
// Building string output for '!ip' command
for (const name in servIP) {
msg.reply(`Hello, my name is ${mc_server_name}, here's my address: ${servIP[name]}` +
`\nI hope to see in-world soon!`);
}
}
});
// Teleports user to the specified coordinates
client.on('message', async msg => {
if (msg.content.startsWith('!tp')) {
if (await checkServerStatus()) {
// Building string output for '!tp' command
data = msg.content.split(' ');
let user = data[1];
let x = Number(data[2]);
let y = Number(data[3]);
let z = Number(data[4]);
if (Number.isNaN(x) || Number.isNaN(y) || Number.isNaN(z)) {
msg.reply("Nope, won't teleport you! Please check the coordinates again.");
} else {
msg.reply(`Hi, trying to teleport user: ${user}.` +
`\nLocation is set to x: ${x}, y: ${y} and z: ${z}`);
var tp = await teleportUser(user, x, y, z);
if (tp == 0) {
msg.reply(`User ${user} teleported successfully ;)`);
} else if (tp == 1) {
msg.reply(`Oops... I can't find user ${user}`);
} else {
msg.reply(`I'm so sorry.. Failed to teleport user ${user}`);
}
}
} else {
msg.reply(`Oops.. Looks like the server is offline!`);
}
}
});
// Returns the status of the server
client.on('message', async msg => {
if (msg.content === '!status') {
if (await checkServerStatus()) {
// Building string output for '!status' command
msg.reply(`I'm online and ready for you to connect!`);
} else {
msg.reply(`Server offline, please contact the Wizards!`);
}
}
});
// Broadcast msg to all in-game players
client.on('message', async msg => {
if (msg.content.startsWith('!broadcast')) {
if (await checkServerStatus()) {
// Building string output for '!broadcast' command
let _msg = "";
_msg = msg.content.split(' ')[1];
console.log(_msg);
if (_msg == null) {
msg.reply(`Nope, I won't broadcast empty messages!`);
} else {
bd = await broadcast(`[${mc_server_name}]: ${_msg}`);
if (bd == 0) {
msg.reply(`Message broadcasted successfully ;)`);
} else {
msg.reply(`Oops.. I couldn't send that out for you`);
}
}
} else {
msg.reply(`Server offline, please contact the Wizards!`);
}
}
});
// Returns the number & name of in-game players
client.on('message', async msg => {
if (msg.content === '!playing') {
if (await checkServerStatus()) {
// Building string output for '!playing' command
result = await mcrcon_cmd(`list`);
if (result != 2) {
msg.reply(result.substring(0, result.length - 5));
} else {
msg.reply(`Oops.. Can't list the players for now!`);
}
} else {
msg.reply(`Server offline, please contact the Wizards!`);
}
}
});
// The teleport user function
async function teleportUser(user, x, y, z) {
const { exec } = require('child_process');
return new Promise(resolve => {
exec(`${mcron_loc} -H ${mcron_host} -p ${mcron_pwd} 'teleport ${user} ${x} ${y} ${z}'`, (err, stdout, stderr) => {
console.log(`[info]: ${stdout}`);
console.log(`[error]: ${stderr}`);
if (err) {
// node couldn't execute the command
resolve(2);
} else {
var out = stdout.toLowerCase().concat(stderr.toLowerCase());
if (out.includes(`no entity`)) {
resolve(1);
} else if (out.includes(`teleported`)) {
resolve(0);
}
}
});
});
}
// Broadcast message to all in-game players
async function broadcast(msg) {
const { exec } = require('child_process');
return new Promise(resolve => {
exec(`${mcron_loc} -H ${mcron_host} -p ${mcron_pwd} 'say ${msg}'`, (err, stdout, stderr) => {
console.log(`[info]: ${stdout}`);
console.log(`[error]: ${stderr}`);
if (err) {
// node couldn't execute the command
resolve(2);
} else {
var out = stdout.toLowerCase().concat(stderr.toLowerCase());
if (out == "") {
resolve(0);
} else {
resolve(1);
}
}
});
});
}
// Generic mcrcon command parser
async function mcrcon_cmd(command) {
const { exec } = require('child_process');
return new Promise(resolve => {
exec(`${mcron_loc} -H ${mcron_host} -p ${mcron_pwd} '${command}'`, (err, stdout, stderr) => {
console.log(`[info]: ${stdout}`);
console.log(`[error]: ${stderr}`);
if (err) {
// node couldn't execute the command
resolve(2);
} else {
resolve(stdout);
}
});
});
}
// Query mc server for availability
async function checkServerStatus() {
return new Promise(resolve => {
for (const name in servIP) {
const [host, port] = servIP[name].split(':'); // Splitting into ip and port
serv.init(host, parseInt(port), () => {
resolve(serv.online);
});
}
});
}
// Updates the in-discord status of the server and number of players in-world
async function checkStatus() {
let statusStr = '';
for (const name in servIP) {
const [host, port] = servIP[name].split(':'); // Splitting into ip and port
serv.init(host, parseInt(port), () => {
if (serv.online) {
// Setting 'Activity' attribute of Discord bot with player counts of servers respectively.
statusStr = (`${mc_server_name} | Players online: ${serv.current_players} | running ${serv.version}`);
}
else {
statusStr = (`${mc_server_name}: Server unreachable`);
}
client.user.setActivity(statusStr);
});
}
}
// Set this in 'config.json'
// Token can be found on your Discord developer portal.
client.login(config["discord"].authtoken);