This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
main.js
655 lines (615 loc) · 21.8 KB
/
main.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// imports
const fs = require('fs');
const mc = require('minecraft-protocol'); // to handle minecraft login session
const notifier = require('node-notifier'); // Required to send desktop notifications
// someone decided to use webserver as a variable to store other data, ok.
const webserver = require('./webserver/webserver.js'); // to serve the webserver
const opn = require('open'); //to open a browser window
const {
Client,
discord,
Intents,
MessageEmbed
} = require('discord.js');
const {
DateTime
} = require("luxon");
const https = require("https");
const everpolate = require("everpolate");
const mcproxy = require("@rob9315/mcproxy");
const antiafk = require("mineflayer-antiafk");
const queueData = require("./queue.json");
const util = require("./util");
const save = "./saveid";
let config;
// This dummy var is a workaround to allow binaries
// const configPath = path.join(process.cwd(), './config/default.json');
// const data = fs.readFileSync(configPath);
try {
config = require("config");
} catch (err) {
if (String(err).includes("SyntaxError: ")) {
console.error("The syntax in your config file is not correct. Make sure you replaced all values as the README says under 'How to Install' step 5. If it still does not work, check that all quotes are closed. You can look up the json syntax online. Please note that the comments are no problem although comments are normally not allowed in json. " + err)
process.exit(1);
}
}
let mc_username;
let mc_password;
let updatemessage;
let discordBotToken;
let savelogin;
let accountType;
let launcherPath;
let c = 150;
let finishedQueue = false
let dc;
const rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
const promisedQuestion = (text) => {
return new Promise((resolve) => rl.question(text, resolve))
}
const askForSecrets = async () => {
let localConf = {};
const config_dir = process.env["NODE_CONFIG_DIR"] ?? 'config';
try {
localConf = util.readJSON(config_dir + '/local.json');
} catch (err) {
if (err.code != "ENOENT") throw err;
}
let canSave = false;
if (!(config.has("username") && config.has("mcPassword") && config.has("updatemessage"))) {
canSave = true;
accountType = ((await promisedQuestion("Account type, mojang (1) or microsoft (2) [1]: ")) === "2" ? "microsoft" : "mojang");
if (accountType == "mojang") {
mc_username = await promisedQuestion("Email: ");
mc_password = await promisedQuestion("Password: ");
} else {
mc_username = await promisedQuestion("Email: ");
mc_password = ""
}
localConf.accountType = accountType;
localConf.mcPassword = mc_password;
localConf.username = mc_username;
updatemessage = await promisedQuestion("Update Messages? Y or N [Y]: ");
localConf.updatemessage = updatemessage;
}
if ((!config.has("discordBot") || config.get("discordBot")) && !config.has("BotToken")) {
canSave = true;
discordBotToken = await promisedQuestion("BotToken, leave blank if not using discord []: ");
localConf.BotToken = discordBotToken;
}
localConf.discordBot = discordBotToken === "" ? false : config.has("discordBot") && config.get("discordBot");
if (canSave) {
savelogin = await promisedQuestion("Save login for later use? Y or N [N]: ");
if (savelogin.toLowerCase() === "y") {
fs.writeFile(config_dir + '/local.json', JSON.stringify(localConf, null, 2), (err) => {
if (err) console.log(err);
});
};
console.clear();
}
if (localConf.discordBot) {
dc = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
dc.login(discordBotToken ?? config.get('BotToken')).catch(() => {
console.warn("There was an error when trying to log in using the provided Discord bot token. If you didn't enter a token this message will go away the next time you run this program!"); //handle wrong tokens gracefully
});
dc.on('ready', () => {
dc.user.setActivity("Queue is stopped.");
fs.readFile(save, "utf8", (err, id) => {
if (!err) dc.users.fetch(id).then(user => {
dcUser = user;
});
});
});
dc.on('messageCreate', function (message) {
if (message.author.username !== dc.user.username) {
userInput(message.content, true, message);
if (dcUser == null || message.author.id !== dcUser.id) {
fs.writeFile(save, message.author.id, function (err) {
if (err) {
throw err;
}
});
}
dcUser = message.author;
}
});
}
console.log(`Finished setting up 2b2w. Type "Start" to start the queue. Type "Help" for the list of commands.`);
cmdInput();
joinOnStart();
}
if (!config.get("minecraftserver.onlinemode")) cmdInput();
else {
mc_username = config.username;
mc_password = config.mcPassword;
launcherPath = config.profilesFolder;
accountType = config.get("accountType");
discordBotToken = config.BotToken
askForSecrets();
}
let stoppedByPlayer = false;
let timedStart;
let dcUser; // discord user that controls the bot
let starttimestring;
let options;
let doing;
let interval = {};
let queueStartPlace;
let queueStartTime;
webserver.restartQueue = config.get("reconnect.notConnectedQueueEnd");
webserver.onstart(startQueuing);
webserver.onstop(stopQueing);
if (config.get("webserver")) {
let webPort = config.get("ports.web");
webserver.createServer(webPort, config.get("address.web")); // create the webserver
webserver.password = config.password
if (config.get("openBrowserOnStart")) opn('http://localhost:' + webPort); //open a browser window
}
// lets
let proxyClient; // a reference to the client that is the actual minecraft game
let client; // the client to connect to 2b2t
let server; // the minecraft server to pass packets
let conn; // connection object from mcproxy for the client variable
options = {
host: config.get("minecraftserver.hostname"),
port: config.get("minecraftserver.port"),
version: config.get("minecraftserver.version")
}
function startAntiAntiAFK() {
if (!config.has("antiAntiAFK.enabled") || !config.get("antiAntiAFK.enabled")) return;
if (proxyClient != null || !webserver.isInQueue || !finishedQueue) return;
conn.bot.afk.start();
}
function cmdInput() {
rl.question("$ ", (cmd) => {
userInput(cmd, false);
cmdInput();
});
}
// function to disconnect from the server
function stop() {
webserver.isInQueue = false;
finishedQueue = false
webserver.queuePlace = "None";
webserver.ETA = "None";
if (client) {
client.end(); // disconnect
}
if (proxyClient) {
proxyClient.end("Stopped the proxy."); // boot the player from the server
}
if (server) {
server.close(); // close the server
}
}
// function to start the whole thing
function startQueuing() {
stopQueing();
doing = "auth";
if (config.get("minecraftserver.onlinemode")) {
options.username = mc_username;
options.password = mc_password;
options.profilesFolder = launcherPath;
options.auth = accountType;
} else {
options.username = config.get("minecraftserver.username");
}
conn = new mcproxy.Conn(options); // connect to 2b2t
client = conn.bot._client;
conn.bot.loadPlugin(antiafk);
conn.bot.afk.setOptions(config.get("antiAntiAFK").get("config"));
join();
}
function join() {
let lastQueuePlace = "None";
let notisend = false;
let positionError = false;
let displayEmail = config.get("displayEmail")
let notificationsEnabled = config.get("desktopNotifications.enabled");
const threshold = config.get("desktopNotifications.threshold");
doing = "queue"
webserver.isInQueue = true;
startAntiAntiAFK(); //for non-2b2t servers
activity("Starting the queue...");
client.on("packet", (data, meta) => { // each time 2b2t sends a packet
switch (meta.name) {
case "playerlist_header":
if (!finishedQueue && true) { // if the packet contains the player list, we can use it to see our place in the queue
let messageheader = data.header;
let positioninqueue = "None";
try {
positioninqueue = messageheader.split("ue")[2].split("\\")[0].slice(9);
} catch (e) {
if (e instanceof TypeError && (positionError !== true)) {
console.log("Reading position in queue from tab failed! Is the queue empty, or the server isn't 2b2t?");
positionError = true;
}
}
if (positioninqueue !== "None") positioninqueue = Number(positioninqueue);
webserver.queuePlace = positioninqueue; // update info on the web page
if (lastQueuePlace === "None" && positioninqueue !== "None") {
queueStartPlace = positioninqueue;
queueStartTime = DateTime.local();
}
if (positioninqueue !== "None" && lastQueuePlace !== positioninqueue) {
let totalWaitTime = getWaitTime(queueStartPlace, 0);
let timepassed = getWaitTime(queueStartPlace, positioninqueue);
let ETAmin = (totalWaitTime - timepassed) / 60;
server.favicon = config.has("favicon") ? config.get("favicon") : fs.readFileSync("favicon.png").toString("base64");
server.motd = `Place in queue: ${webserver.queuePlace} ETA: ${webserver.ETA}`; // set the MOTD because why not
webserver.ETA = Math.floor(ETAmin / 60) + "h " + Math.floor(ETAmin % 60) + "m";
webserver.finTime = new Date((new Date()).getTime() + ETAmin * 60000);
if (config.get("userStatus")) {
//set the Discord Activity
const name = displayEmail?options.username:client.username;
logActivity("P: " + positioninqueue + " E: " + webserver.ETA + " - " + name);
} else {
logActivity("P: " + positioninqueue + " E: " + webserver.ETA);
}
if (config.get("notification.enabled") && positioninqueue <= config.get("notification.queuePlace") && !notisend && config.discordBot && dcUser != null) {
sendDiscordMsg(dcUser, "Queue", "The queue is almost finished. You are in Position: " + webserver.queuePlace);
notisend = true;
}
if (positioninqueue <= threshold && notificationsEnabled){
notifier.notify({// Send the notification
title: 'Your queue is ' + threshold + '!',
message: 'Your queue is ' + threshold + '!',
sound: true,
wait: true});
notificationsEnabled = false};// The flag is set to false to prevent the notification from being shown again
}
lastQueuePlace = positioninqueue;
}
break;
case "chat":
if (finishedQueue === false) { // we can know if we're about to finish the queue by reading the chat message
// we need to know if we finished the queue otherwise we crash when we're done, because the queue info is no longer in packets the server sends us.
let chatMessage = JSON.parse(data.message).text;
if (chatMessage == 'Queued for server main.' || chatMessage == 'You are already queued to server main.')
console.log("2B2T says: " + chatMessage);
if (chatMessage == "Connected to the server.") {
if (config.get("expandQueueData")) {
queueData.place.push(queueStartPlace);
let timeQueueTook = DateTime.local().toSeconds() - queueStartTime.toSeconds();
let b = Math.pow((0 + c) / (queueStartPlace + c), 1 / timeQueueTook);
queueData.factor.push(b);
fs.writeFile("queue.json", JSON.stringify(queueData), "utf-8", (err) => {
log(err);
});
}
if (webserver.restartQueue && proxyClient == null) { //if we have no client connected and we should restart
stop();
reconnect();
} else {
finishedQueue = true;
startAntiAntiAFK();
webserver.queuePlace = "FINISHED";
webserver.ETA = "NOW";
logActivity("Queue is finished");
}
}
}
break;
}
});
// set up actions in case we get disconnected.
const onDisconnect = () => {
if (proxyClient) {
proxyClient.end("Connection reset by 2b2t server.\nReconnecting...");
proxyClient = null
}
stop();
if (!stoppedByPlayer) {
log(`Connection reset by 2b2t server. Reconnecting...`);
if (!config.has("MCpassword") && !config.has("password")) log("If this ^^ message shows up repeatedly, it is likely a problem with your token being invalidated. Please start minecraft manually or use credential authentication instead.");
}
if (config.reconnect.onError) setTimeout(reconnect, 30000);
}
client.on('end', onDisconnect);
client.on('error', onDisconnect);
server = mc.createServer({ // create a server for us to connect to
'online-mode': config.get("whitelist"),
encryption: true,
host: config.get("address.minecraft"),
port: config.get("ports.minecraft"),
version: config.MCversion,
'max-players': maxPlayers = 1
});
server.on('login', (newProxyClient) => { // handle login
if (config.whitelist && client.uuid !== newProxyClient.uuid) {
newProxyClient.end("not whitelisted!\nYou need to use the same account as 2b2w or turn the whitelist off");
return;
}
newProxyClient.on('packet', (_, meta, rawData) => { // redirect everything we do to 2b2t
filterPacketAndSend(rawData, meta, client);
});
newProxyClient.on("end", () => {
proxyClient = null;
startAntiAntiAFK();
})
conn.bot.afk.stop().then(() => {
conn.sendPackets(newProxyClient);
conn.link(newProxyClient);
proxyClient = newProxyClient;
});
});
}
function log(logmsg) {
if (config.get("logging")) {
fs.appendFile('2bored2wait.log', DateTime.local().toLocaleString({
hour: '2-digit',
minute: '2-digit',
hour12: false
}) + " " + logmsg + "\n", err => {
if (err) console.error(err)
})
}
let line = rl.line;
process.stdout.write("\033[F\n" + logmsg + "\n$ " + line);
}
function reconnect() {
doing = "reconnect";
if (stoppedByPlayer) stoppedByPlayer = false;
else {
logActivity("Reconnecting... ");
reconnectLoop();
}
}
function reconnectLoop() {
mc.ping({
host: config.minecraftserver.hostname,
port: config.minecraftserver.port
}, (err) => {
if (err) setTimeout(reconnectLoop, 3000);
else startQueuing();
});
}
//function to filter out some packets that would make us disconnect otherwise.
//this is where you could filter out packets with sign data to prevent chunk bans.
function filterPacketAndSend(data, meta, dest) {
if (meta.name !== "keep_alive" && meta.name !== "update_time") { //keep alive packets are handled by the client we created, so if we were to forward them, the minecraft client would respond too and the server would kick us for responding twice.
dest.writeRaw(data);
}
}
function activity(string) {
dc?.user?.setActivity(string);
}
//the discordBot part starts here.
function userInput(cmd, DiscordOrigin, discordMsg) {
// this makes no sense, some commands reply to discord bot some log to console?
cmd = cmd.toLowerCase();
switch (cmd) {
case "help":
case "commands":
console.log(" help: Lists available commands.");
console.log(" start 14:00: Start queue at 2pm.");
console.log(" play 8:00: Tries to calculate the right time to join so you can play at 8:00am.");
console.log(" start: Starts the queue.");
console.log(" loop: Restarts the queue if you are not connect at the end of it");
console.log(" loop status: Lets you know if you have reconnect on or off.")
console.log(" update: Sends an update to the current channel with your position and ETA.");
console.log(" url: displays the github url");
console.log(" stop: Stops the queue.");
console.log(" exit or quit: Exits the application.");
console.log(" stats: Displays your health and hunger.");
break;
case "stats":
try {
if (conn.bot.health == undefined && conn.bot.food == undefined){
console.log("Unknown.")
break;}
else
{if (conn.bot.health == 0)
console.log("Health: DEAD");
else
console.log("Health: " + Math.ceil(conn.bot.health)/2 + "/10");
if (conn.bot.food == 0)
console.log("Hunger: STARVING");
else
console.log("Hunger: " + conn.bot.food/2 + "/10");}
} catch (err)
{console.log(`Start 2B2W first with "Start".`)}
break;
case "url":
console.log("https://github.com/themoonisacheese/2bored2wait");
break;
case "loop":
console.log("Syntax: status, enable, disable");
break;
case "loop status":
if (webserver.restartQueue)
console.log("Loop is enabled");
else
console.log("Loop is disabled");
break;
case "loop enable":
if (webserver.restartQueue)
console.log("Loop is already enabled!");
else {
webserver.restartQueue = true
console.log("Enabled Loop");
}
break;
case "loop disable":
if (!webserver.restartQueue)
console.log("Loop is already disabled!");
else {
webserver.restartQueue = false
console.log("Disabled Loop");
}
break;
case "start":
startQueuing();
msg(DiscordOrigin, discordMsg, "Queue", "Queue is starting up");
break;
case "exit":
case "quit":
return process.exit(0);
case "update":
switch (doing) {
case "queue":
msg(DiscordOrigin, discordMsg, "Reconnecting", `Position: ${webserver.queuePlace} \n Estimated time until login: ${webserver.ETA}`);
console.log("Position: " + webserver.queuePlace + " Estimated time until login: " + webserver.ETA);
break;
case "timedStart":
msg(DiscordOrigin, discordMsg, "Timer", "Timer is set to " + starttimestring);
break;
case "reconnect":
msg(DiscordOrigin, discordMsg, "Reconnecting", "2b2t is currently offline. Trying to reconnect");
break;
case "auth":
let authMsg = "Authentication";
msg(DiscordOrigin, discordMsg, authMsg, authMsg);
break;
case "calcTime":
msg(DiscordOrigin, discordMsg, "Calculating time", "Calculating the time, so you can play at " + starttimestring);
break;
}
break;
case "stop":
switch (doing) {
case "queue":
stopQueing();
stopMsg(DiscordOrigin, discordMsg, "Queue");
break;
case "timedStart":
clearTimeout(timedStart);
stopMsg(DiscordOrigin, discordMsg, "Timer");
break;
case "reconnect":
clearInterval(interval.reconnect);
stopMsg(DiscordOrigin, discordMsg, "Reconnecting");
break;
case "auth":
clearInterval(interval.auth);
stopMsg(DiscordOrigin, discordMsg, "Authentication");
break;
case "calcTime":
clearInterval(interval.calc);
stopMsg(DiscordOrigin, discordMsg, "Time calculation");
break;
}
break;
default:
if (/start (\d|[0-1]\d|2[0-3]):[0-5]\d$/.test(cmd)) {
doing = "timedStart"
timedStart = setTimeout(startQueuing, timeStringtoDateTime(cmd).toMillis() - DateTime.local().toMillis());
activity("Starting at " + starttimestring);
msg(DiscordOrigin, discordMsg, "Timer", "Queue is starting at " + starttimestring);
} else if (/^play (\d|[0-1]\d|2[0-3]):[0-5]\d$/.test(cmd)) {
timeStringtoDateTime(cmd);
calcTime(cmd);
msg(DiscordOrigin, discordMsg, "Time calculator", "The perfect time to start the queue will be calculated, so you can play at " + starttimestring);
activity("You can play at " + starttimestring);
} else msg(DiscordOrigin, discordMsg, "Error", `Unknown command. Type "Help" for the list of commands.`);
}
}
function stopMsg(discordOrigin, discordMsg, stoppedThing) {
msg(discordOrigin, discordMsg, stoppedThing, stoppedThing + " is **stopped**");
activity(stoppedThing + " is stopped.");
}
function msg(discordOrigin, msg, title, content) {
if (discordOrigin) sendDiscordMsg(msg.channel, title, content);
else console.log(content);
}
function sendDiscordMsg(channel, title, content) {
const MessageEmbed = {
color: 3447003,
author: {
name: dc.user.username,
icon_url: dc.user.avatarURL
},
fields: [{
name: title,
value: content
}],
timestamp: new Date(),
footer: {
icon_url: dc.user.avatarURL,
text: "Author: MrGeorgen"
}
}
if (config.get("dc_chat")) {
channel.send({
embeds: [MessageEmbed]
}).catch(() => {
console.warn(`There was a permission error! Please make sure your bot has perms to talk.`); //handle wrong tokens gracefully
})};
}
function timeStringtoDateTime(time) {
starttimestring = time.split(" ");
starttimestring = starttimestring[1];
let starttime = starttimestring.split(":");
let startdt = DateTime.local().set({
hour: starttime[0],
minute: starttime[1],
second: 0,
millisecond: 0
});
if (startdt.toMillis() < DateTime.local().toMillis()) startdt = startdt.plus({
days: 1
});
return startdt;
}
function calcTime(msg) {
https.get('https://2b2t.io/api/queue', function (res) {
doing = "calcTime"
interval.calc = setInterval(function () {
https.get("https://2b2t.io/api/queue", (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on("end", () => {
data = JSON.parse(data);
let queueLength = data[0][1];
let playTime = timeStringtoDateTime(msg);
let waitTime = getWaitTime(queueLength, 0);
if (playTime.toSeconds() - DateTime.local().toSeconds() < waitTime) {
startQueuing();
clearInterval(interval.calc);
console.log(waitTime);
}
});
}).on("error", (err) => {
log(err)
});
}, 60000);
}).on('error', function (e) {
console.log(`2b2t.io is currently offline. Please try again later to use the "play" command.`)
});
}
function stopQueing() {
stoppedByPlayer = true;
stop();
}
function logActivity(update) {
activity(update);
log(update);
}
function joinOnStart() {
if (config.get("joinOnStart")) setTimeout(startQueuing, 1000);
}
function getWaitTime(queueLength, queuePos) {
let b = everpolate.linear(queueLength, queueData.place, queueData.factor)[0];
return Math.log((queuePos + c) / (queueLength + c)) / Math.log(b); // see issue 141
}
process.on('uncaughtException', err => {
const boxen = require("boxen")
console.error(err);
console.log(boxen(`Something went wrong! Feel free to contact us on discord or github! \n\n Github: https://github.com/themoonisacheese/2bored2wait \n\n Discord: https://discord.next-gen.dev/`, {title: 'Something Is Wrong', titleAlignment: 'center', padding: 1, margin: 1, borderStyle: 'bold', borderColor: 'red', backgroundColor: 'red', align: 'center'}));
console.log('Press any key to exit');
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));
});
module.exports = {
startQueue: startQueuing,
filterPacketAndSend: filterPacketAndSend,
stop: stopQueing,
};