-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0Bot.js
546 lines (435 loc) · 16.3 KB
/
0Bot.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// 0Bot
if (process.argv.length < 3) {
console.log('Usage : node <scriptname>.js <host>');
process.exit(1);
}
const server = process.argv[2];
const fs = require('fs');
const mineflayer = require('mineflayer');
const armorManager = require('mineflayer-armor-manager');
const autoEat = require('mineflayer-auto-eat').plugin;
const pathFinder = require('mineflayer-pathfinder').pathfinder;
const Movements = require('mineflayer-pathfinder').Movements;
// ReSharper disable once InconsistentNaming
const { GoalNear } = require('mineflayer-pathfinder').goals;
const { autototem } = require('mineflayer-auto-totem');
const tpsPlugin = require('./tps.js')(mineflayer);
// ReSharper disable twice InconsistentNaming
const { ActivityType, EmbedBuilder } = require('discord.js');
const discord = require('discord.js');
const dotenv = require('dotenv');
dotenv.config();
try { setTerminalTitle(hideEmail(process.env.minecraftUsername) + ' | ' + server) } catch (e) { }
const discordLink = process.env.discordLink;
const changeTopicToStats = true;
const logStatusToChat = false;
const doAdvertising = true;
const promoted = process.env.promotedServer;
const intervalDiscordTopic = 60 * 5; // seconds
const intervalMove = 5; // seconds
const intervalEquip = 5; // seconds
const intervalBroadcast = 60 * 2; // seconds
let lastTimeMoved = -1; // ts
let lastTimeEquipped = -1; // ts
let lastTimeBroadcast = -1; // ts
const maxrandom = 5; // 0-5 seconds added to movement interval (randomly)
let moving = 0;
const channels = require('./channels.json');
const broadcasts = fs.readFileSync('promotion.txt').toString().split('\n');
let session;
try { session = JSON.parse(fs.readFileSync('session.json', 'utf8')) } catch (e) { }
const minecraftLoginData = {
host: server,
port: 25565,
username: process.env.minecraftUsername,
password: process.env.minecraftPassword,
verbose: true,
version: '1.12.2',
auth: 'microsoft',
session
};
let defaultMove;
// DISCORD BOT
const discordBot = new discord.Client({
intents: [
discord.GatewayIntentBits.Guilds,
discord.GatewayIntentBits.GuildMessages
]
});
let minecraftConnected = false;
let discordConnected = false;
// DISCORD BOT
discordBot.on('ready', () => {
console.log(`Logged in as Discord bot ${discordBot.user.tag}.`);
discordConnected = true;
// Channel Topic
setInterval(updateDiscordTopic, intervalDiscordTopic * 1000);
discordBot.user.setPresence({
activities: [{ name: '0b0t.org', type: ActivityType.Playing }],
status: 'dnd',
});
if (logStatusToChat) sendToDiscordChat(`:white_check_mark: **Bridge for server '${server}' has started**`, true);
if (!minecraftConnected) setDiscordChannelTopic(`:hourglass: ${server} | Connecting...`);
});
// MINECRAFT BOT
let minecraftBot;
// CHAT DISCORD -> GAME
discordBot.on('message', msg => {
if (discordConnected && msg.author.id !== discordBot.user.id) {
if (msg.channel.id === channels[server]) {
if (msg.content.startsWith('!')) {
if (msg.content.toLowerCase() === '!help') {
sendToDiscordChat(':arrow_forward: Commands are: !help');
} else {
sendToDiscordChat(':warning: Unknown Command!');
}
} else {
const message = `[${msg.author.username}]: \`${msg.content}`;
minecraftBot.chat(message.substr(0, 240));
}
}
}
});
discordBot.login(process.env.discordToken); // TODO: do not ignore returned promise then()
relog();
function relog() {
try { minecraftBot.quit(); } catch (e) { }
console.log(`Attempting to (re)connect to ${server}...`);
minecraftBot = mineflayer.createBot(minecraftLoginData);
bindEvents(minecraftBot);
}
// auto equip
function autoEquip() {
if (!minecraftConnected) return;
try {
minecraftBot.armorManager.equipAll();
}
catch (err) { console.log(`Minecraft bot auto equip failed: \n${err}`); }
}
function updateDiscordTopic() {
if (changeTopicToStats && minecraftConnected && minecraftBot.players !== undefined && minecraftBot.players != null) {
setDiscordChannelTopic(`:white_check_mark: ${server} | ${sizeOf(minecraftBot.players)}/${minecraftBot.game.maxPlayers} Players online | TPS: ${minecraftBot.getTps()}`);
}
}
function bindEvents(bot) {
// NAVIGATION
bot.loadPlugin(pathFinder);
// TPS
bot.loadPlugin(tpsPlugin);
// AUTOEAT
bot.loadPlugin(autoEat);
// ARMOR MANAGER
bot.loadPlugin(armorManager);
// AUTO TOTEM
bot.loadPlugin(autototem);
// SAVED SESSION
bot._client.on('session', () => {
fs.writeFileSync('session.json', JSON.stringify(bot._client.session));
});
bot.on('tpRequest', function (username) {
if (isWhitelisted(username)) {
bot.chat(`/tpy ${username}`);
console.log(`${username} teleported.`);
}
else {
console.log(`${username} is not allowed to teleport.`);
}
});
// MINECRAFT BOT LOGIN
bot.on('login', function () {
console.log(`Logged in as Minecraft bot ${hideEmail(process.env.minecraftUsername)}!`);
});
// BOT SPAWN
bot.on('spawn', function () {
const minecraftData = require('minecraft-data')(bot.version);
defaultMove = new Movements(bot, minecraftData);
defaultMove.allow1by1towers = false;
defaultMove.canDig = false;
defaultMove.canOpenDoors = false;
defaultMove.allowSprinting = false;
bot.autoEat.options.priority = 'foodPoints';
bot.autoEat.options.bannedFood = [];
bot.autoEat.options.eatingTimeout = 3;
// hp stops regenerating when 17 hunger points are reached
bot.autoEat.options.startAt = 17.5;
bot.autoEat.enable();
// TPA REQUEST
bot.chatAddPattern(/^([a-zA-Z0-9_]{3,16}) wants to teleport to you\.$/, 'tpRequest', 'tpa request');
minecraftConnected = true;
updateDiscordTopic();
});
// CHAT GAME -> DISCORD
bot.on('chat', function (username, message) {
if (username === bot.username) return;
if (username === 'queue') {
setDiscordChannelTopic(`:hourglass: ${server} | Queue: ${message}`);
} else if (username === 'whispers') {
// Ignore
} else if (username === '15m') {
// Ignore
} else if (message.toLowerCase().startsWith('/register')) {
bot.chat(`/register ${process.env.authMePassword} ${process.env.authMePassword}`);
} else if (message.toLowerCase().startsWith('/login')) {
bot.chat(`/login ${process.env.authMePassword}`);
} else if (message.toLowerCase().startsWith('/email')) {
// Ignore
} else if (message.toLowerCase() === '~discord' || message.toLowerCase() === '!discord' || message.toLowerCase() === '?discord') {
bot.chat(`> Join ${discordLink} for the ${server} discord chat bridge.`);
} else if (message.toLowerCase() === '~tps' || message.toLowerCase() === '!tps' || message.toLowerCase() === '?tps') {
bot.chat(`> Current approximate tps: ${minecraftBot.getTps()}`);
} else if (message.toLowerCase() === '~leave' && isWhitelisted(username.toLowerCase())) {
bot.quit();
} else if (discordConnected) {
const color = posMod(hashCode(username), 16777215);
const embed = new EmbedBuilder()
.setAuthor({
name: username,
iconURL: 'https://minotar.net/avatar/' + username
})
.setDescription(message)
.setColor(color)
.setFooter({ text: utcDateTime() });
sendToDiscordChat({ embeds: [embed] });
}
});
// DEATH
bot.on('death', () => {
console.log(`Bot died at ([${bot.game.dimension}] ${bot.entity.position.x.toFixed(1)}, ${bot.entity.position.y.toFixed(1)}, ${bot.entity.position.z.toFixed(1)}).`);
});
// TICK
bot.on('physicsTick', async () => {
bot.autototem.equip();
});
// BOT CRONJOBS
bot.on('time', function () {
if (!minecraftConnected) return;
let randomadd;
// Auto equip
if (lastTimeEquipped < 0) {
lastTimeEquipped = bot.time.age;
} else {
if (bot.time.age - lastTimeEquipped > intervalEquip * 20) {
autoEquip();
lastTimeEquipped = bot.time.age;
}
}
// Look around and move
if (lastTimeMoved < 0) {
lastTimeMoved = bot.time.age;
} else {
randomadd = Math.random() * 20 + maxrandom;
const interval = intervalMove * 20 + randomadd;
if (bot.time.age - lastTimeMoved > interval) {
if (moving === 1) {
bot.setControlState('sneak', true);
bot._client.write('arm_animation', { hand: 0 });
// Movement
try {
if (minecraftConnected) {
const pos = bot.entity.position.offset(getRandomInt(-2, 2), 0, getRandomInt(-2, 2));
bot.pathfinder.setMovements(defaultMove);
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, 1));
}
}
catch (err) { console.log(`Minecraft bot navigation failed: \n${err}`); }
moving = 0;
} else {
const yaw = Math.random() * Math.PI - (0.5 * Math.PI);
const pitch = Math.random() * Math.PI - (0.5 * Math.PI);
bot.setControlState('sneak', false);
bot.look(yaw, pitch, false);
moving = 1;
}
lastTimeMoved = bot.time.age;
}
}
// Broadcast
if (doAdvertising) {
if (lastTimeBroadcast < 0) {
lastTimeBroadcast = bot.time.age;
} else {
randomadd = Math.random() * 20 + maxrandom;
const intervalbc = intervalBroadcast * 20 + randomadd;
if (bot.time.age - lastTimeBroadcast > intervalbc) {
let message = broadcasts[Math.floor(Math.random() * broadcasts.length)];
message = message.replace('%server%', server);
message = message.replace('%promoted%', promoted);
message = message.replace('%discord%', discordLink);
message = message.replace(/[\r\n]/g, '');
bot.chat(`> ${message}`);
lastTimeBroadcast = bot.time.age;
}
}
}
});
// BOT KICK
bot.on('kicked', function (reason) {
minecraftConnected = false;
const jsonReason = JSON.parse(reason);
console.log(`Minecraft bot was kicked for ${jsonReason.text}`);
setDiscordChannelTopic(`:cold_face: ${server} | Kicked`);
if (jsonReason.text.toLowerCase() === 'timed out') {
sendToDiscordChat(`:cold_face: **Bridge for server '${server}' timed out.**`, true);
}
else {
sendToDiscordChat(`:angry: **Bridge for server '${server}' was kicked with the reason: ${jsonReason.text}**`, true);
}
});
// BOT DISCO
bot.on('end', function () {
minecraftConnected = false;
console.log('Minecraft bot has ended! Attempting to reconnect in 30 s...');
setDiscordChannelTopic(`:warning: ${server} | Disconnected`);
sendToDiscordChat(`:warning: **Bridge for server '${server}' disconnected! Attempting to reconnect in 30 s...**`, true);
setTimeout(relog, 30000);
});
// BOT ERROR
bot.on('error', function (err) {
minecraftConnected = false;
console.log(`Minecraft bot error '${err.errno}'.`);
setDiscordChannelTopic(`:sos: ${server} | Error`);
if (err.code === undefined && logStatusToChat) {
setTimeout(relog, 5 * 60 * 1000);
console.log('Undefined error: Maybe invalid credentials OR bot needs to wait because it relogged too quickly.');
}
else {
sendToDiscordChat(`:sos: **Bridge for server '${server}' had the error: ${err.errno}**`, true);
}
});
// PLUGIN METHODS
bot.on('autoeat_started', () => {
console.log(`Started auto eat at ${bot.health.toFixed(1)} hp and ${bot.food.toFixed(1)} food.`);
});
bot.on('autoeat_stopped', () => {
console.log(`Stopped auto eat at ${bot.health.toFixed(1)} hp and ${bot.food.toFixed(1)} food.`);
});
}
function setDiscordChannelTopic(topic) {
if (!discordConnected) return;
const channel = getDiscordChannel(server);
if (channel != null && channel.topic !== topic && topic != null) {
topic = `${topic} | Last Update: ${utcDateTime()} UTC`;
getDiscordChannel(server).setTopic(topic); // TODO: do not ignore returned promise then()
}
}
function sendToDiscordChat(message, isStatus = false) {
try {
const channel = getDiscordChannel(server);
if (channel != null && (!isStatus || logStatusToChat)) {
channel.send(message);
}
} catch (err) {
console.log(err.message);
}
}
// NODE EXCEPTION
process.on('uncaughtException', function (err) {
if (err.message.includes('rejected transaction')) {
console.log(`${err.message}`);
return;
}
console.log(`${err.stack}`);
process.exit();
});
// DISCORD CHANNEL
function getDiscordChannel(channel) {
let ret;
let first = true;
const channelId = channels[channel];
do {
if (first) {
first = false;
} else {
return console.log(`Channel '${channel}' is not ready yet...`);
}
ret = discordBot.channels.cache.get(channelId);
} while (typeof ret === 'undefined' || ret == null);
return ret;
}
// EXCEPTION
// so the program will not close instantly
process.stdin.resume();
function exitHandler() {
discordConnected = false;
minecraftConnected = false;
setDiscordChannelTopic(`:octagonal_sign: ${server} | Offline`);
try {
const channel = getDiscordChannel(server);
if (channel != null && logStatusToChat) {
channel.send(`:octagonal_sign: **Bridge for server '${server}' has stopped**`);
}
} catch (e) { }
process.exit(1);
}
// do something when App is closing
process.on('exit', exitHandler.bind(null, {}));
// clicking the 'X' on Windows
process.on('SIGHUP', exitHandler.bind(null, {}));
// catches Ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {}));
// catches "kill PID" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, {}));
process.on('SIGUSR2', exitHandler.bind(null, {}));
// HELPER
function requireUncached(module) {
delete require.cache[require.resolve(module)];
return require(module);
}
function isWhitelisted(username) {
return requireUncached('./whitelist.json').includes(username.toLowerCase());
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
/**
* Size of an array
* @param data array
*/
function sizeOf(data) {
let size = 0;
for (let { } in data) {
size++;
}
return size;
}
/**
* formatted UTC time
*/
function utcDateTime() {
const m = new Date();
return m.getUTCFullYear() + "/" +
(`0${m.getUTCMonth() + 1}`).slice(-2) + "/" +
(`0${m.getUTCDate()}`).slice(-2) + " " +
(`0${m.getUTCHours()}`).slice(-2) + ":" +
(`0${m.getUTCMinutes()}`).slice(-2) + ":" +
(`0${m.getUTCSeconds()}`).slice(-2);
}
/**
* Java String#hashCode
*/
function hashCode(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
}
function posMod(x, m) {
return (x % m + m) % m;
}
function setTerminalTitle(title) {
process.stdout.write(
`${String.fromCharCode(27)}]0;${title}${String.fromCharCode(7)}`
);
}
function hideEmail(email) {
return email.replace(/(.{2})(.*)(?=@)/,
function (_gp1, gp2, gp3) {
for (let i = 0; i < gp3.length; i++) {
gp2 += '*';
} return gp2;
}
);
};