This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
forked from themoonisacheese/2bored2wait
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
931 lines (866 loc) · 29.4 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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
// imports
const jsonminify = require("node-json-minify"); // to remove comments from the config.json, because normally comments in json are not allowed
const fs = require('fs');
const PubSub = require('pubsub-js');
const mc = require('minecraft-protocol'); // to handle minecraft login session
const webserver = require('./webserver/webserver.js'); // to serve the webserver
const opn = require('open'); //to open a browser window
require('request').debug = true
const {
Client,
Collection,
GatewayIntentBits,
} = 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/util");
const Location = require("./util/location")
const Player = require("./util/player");
const save = "./saveid";
const config_dir = process.env["NODE_CONFIG_DIR"] ?? 'config';
var guild;
var config;
// This dummy var is a workaround to allow binaries
const path = require('path');
// 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);
}
}
const GUILD_ID = config.guildId
var mc_username;
var mc_password;
var updatemessage;
var discordBotToken;
var savelogin;
var whitelistUUIDs = [];
var whitelistNames = Array.from(config.whitelist.users);
var accountType;
let conn;
let proxyClient;
let client;
var location = new Location()
let inRange;
let reconnectTimeout = config.reconnect.timeout
let reconnectMult = config.reconnect.multiplier;
let playersSeen = new Set();
let server;
let launcherPath;
let c = 150;
let finishedQueue = false
let dc;
let localConf = {};
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 () => {
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") {
saveConfig();
};
console.clear();
}
if (localConf.discordBot) {
dc = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
});
dc.commands = new Collection();
const commandsPath = path.join(__dirname, 'util/commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
dc.commands.set(command.data.name, command);
}
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!");
});
dc.once('ready', async () => {
guild = await dc.guilds.fetch(GUILD_ID)
})
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('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
}
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
if (config.whitelist.enabled) {
util.getUUIDForWhitelist(whitelistNames).then(res => {
whitelistUUIDs = res
askForSecrets();
})
} else {
askForSecrets();
}
}
var stoppedByPlayer = false;
var timedStart;
let dcUser; // discord user that controls the bot
var totalWaitTime;
var starttimestring;
var options = {
username: "",
password: "",
profilesFolder: "",
auth: ""
};
var doing;
let interval = {};
let queueStartPlace;
let queueStartTime;
webserver.restartQueue = config.get("reconnect.notConnectedQueueEnd");
webserver.onstart(() => { // set up actions for the webserver
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
}
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";
options = {
host: config.get("minecraftserver.hostname"),
port: config.get("minecraftserver.port"),
version: config.get("minecraftserver.version")
}
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;
var PositionError = false;
let displayEmail = config.get("displayEmail")
doing = "queue"
webserver.isInQueue = true;
startAntiAntiAFK(); //for non-2b2t servers
activity("Starting the queue...");
client.once("login", (data, meta) => {
if (typeof (config.lastlaunch) != "undefined") {
if (config.lastlaunch.onlinemode == config.get("minecraftserver.onlinemode")) {
if (config.lastlaunch.username == client.username) {
return
}
}
}
if (!config.get("minecraftserver.onlinemode")) {
playerIcon = `https://mc-heads.net/avatar/Steve/64.png`
} else {
playerIcon = `https://mc-heads.net/avatar/${client.username}/64.png`
}
guild.members.cache.get(dc.user.id).setNickname(client.username)
dc.user.setAvatar(playerIcon)
localConf.lastlaunch = {
"onlinemode": config.get("minecraftserver.onlinemode"),
"username": client.username
}
saveConfig();
})
client.on("packet", (data, meta) => { // each time 2b2t sends a packet
inRange = []
Object.keys(conn.bot.entities).forEach(key => {
if (conn.bot.entities[key].type == "player") {
if (conn.bot.entities[key].username != client.username) {
let player = new Player()
player.x = conn.bot.entities[key].position.x;
player.y = conn.bot.entities[key].position.y;
player.z = conn.bot.entities[key].position.z;
player.name = conn.bot.entities[key].username;
inRange.push(player)
if (!playersSeen.has(conn.bot.entities[key].username)) {
playersSeen.add(conn.bot.entities[key].username)
}
} else if (conn.bot.entities[key].username == client.username) {
location.x = conn.bot.entities[key].position.x;
location.y = conn.bot.entities[key].position.y;
location.z = conn.bot.entities[key].position.z;
}
}
});
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('text')[5].replace(/\D/g, '');
} 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.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
if (displayEmail) {
logActivity("P: " + positioninqueue + " E: " + webserver.ETA + " - " + options.username);
} else {
logActivity("P: " + positioninqueue + " E: " + webserver.ETA + " - " + client.username);
}
} 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;
}
}
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 == "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.enabled) {
if (!whitelistUUIDs.map(user => user.id == newProxyClient.uuid.replaceAll('-', '')).includes(true)) {
newProxyClient.end("Not whitelisted!");
return;
}
}
else if (client.uuid !== newProxyClient.uuid) {
newProxyClient.end("Not whitelisted!");
return;
}
newProxyClient.on('packet', (data, 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 saveConfig() {
fs.writeFile(config_dir + '/local.json', JSON.stringify(localConf, null, 2), (err) => {
if (err) console.log(err);
});
}
function reconnectLoop() {
mc.ping({
host: config.minecraftserver.hostname,
port: config.minecraftserver.port
}, (err) => {
if (err) {
setTimeout(reconnectLoop, reconnectTimeout * reconnectMult);
reconnectMult += 1;
console.log(reconnectMult);
}
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, channel) {
let splitCmd = cmd.split(" ")
cmd = cmd.toLowerCase();
if (cmd.includes(" ")) {
cmd = cmd.split(" ")[0]
}
switch (cmd) {
case "help":
case "commands":
msg(DiscordOrigin, discordMsg, "Help", " help: Lists available commands.\n start 14:00: Start queue at 2pm.\n play 8:00: Tries to calculate the right time to join so you can play at 8:00am.\n start: Starts the queue.\n loop: Restarts the queue if you are not connect at the end of it,\n loop status: Lets you know if you have reconnect on or off.\n update: Sends an update to the current channel with your position and ETA.\n url: displays the github url.\n stop: Stops the queue.\n exit or quit: Exits the application.\n stats: Displays your health and hunger.");
break;
case "status":
case "stats":
try {
if (conn.bot.health == undefined && conn.bot.food == undefined) {
msg(DiscordOrigin, discordMsg, "Status", "Unknown.")
break;
}
else {
if (conn.bot.health == 0)
msg(DiscordOrigin, discordMsg, "Status", "Health: DEAD");
else
msg(DiscordOrigin, discordMsg, "Status", "Health: " + Math.ceil(conn.bot.health) / 2 + "/10");
if (conn.bot.food == 0)
msg(DiscordOrigin, discordMsg, "Status", "Hunger: STARVING");
else
msg(DiscordOrigin, discordMsg, "Status", "Hunger: " + conn.bot.food / 2 + "/10");
}
} catch (err) { msg(DiscordOrigin, discordMsg, "Error", `Start 2B2W first with "Start".`) }
break;
case "url":
msg(DiscordOrigin, discordMsg, "URL", "https://github.com/themoonisacheese/2bored2wait");
break;
case "loop":
if (splitCmd[1] = "status") {
if (JSON.stringify(webserver.restartQueue) == "true")
msg(DiscordOrigin, discordMsg, "Loop", "Loop is enabled");
else
msg(DiscordOrigin, discordMsg, "Loop", "Loop is disabled");
break;
}
else if (splitCmd[1] = "enable") {
if (JSON.stringify(webserver.restartQueue) == "true")
msg(DiscordOrigin, discordMsg, "Loop", "Loop is already enabled!");
else {
webserver.restartQueue = true
msg(DiscordOrigin, discordMsg, "Loop", "Enabled Loop");
}
break;
}
else if (splitCmd[1] = "disable") {
if (JSON.stringify(webserver.restartQueue) == "false")
msg(DiscordOrigin, discordMsg, "Loop", "Loop is already disabled!");
else {
webserver.restartQueue = false
msg(DiscordOrigin, discordMsg, "Loop", "Disabled Loop");
}
break;
}
msg(DiscordOrigin, discordMsg, "Syntax", "Syntax: status, enable, disable");
break;
case "start":
startQueuing();
msg(DiscordOrigin, discordMsg, "Queue", "Queue is starting up");
break;
case "exit":
case "quit":
return process.exit(0);
case "whitelist":
if (splitCmd[1] == "add") {
check = util.getUUIDForWhitelist(splitCmd[2])
check.then(promise => {
if (promise[0] != undefined) {
whitelistUUIDs.push(promise[0])
msg(DiscordOrigin, discordMsg, "Whitelist", `Added ${splitCmd[2]} to Whitelist!`)
} else {
msg(DiscordOrigin, discordMsg, "Whitelist", "Username wrong?");
}
})
} else if (splitCmd[1] == "delete" || splitCmd[1] == "remove") {
check = util.getUUIDForWhitelist(splitCmd[2])
check.then(usrObj => {
if (usrObj[0] != undefined) {
whitelistUUIDs = whitelistUUIDs.filter(({ id }) => id !== usrObj[0].id)
msg(DiscordOrigin, discordMsg, "Whitelist", `Removed ${splitCmd[2]} from Whitelist!`)
} else {
msg(DiscordOrigin, discordMsg, "Whitelist", "Username wrong?");
}
})
} else if (splitCmd[1] == "list") {
msg(DiscordOrigin, discordMsg, "Whitelist", "Listing all whitelisted Users" + whitelistUUIDs.map(accounts => "\n" + accounts.name + ""));
} else {
msg(DiscordOrigin, discordMsg, "Whitelist", "Wrong Syntax for Whitelist command!")
}
break;
case "update":
switch (doing) {
case "queue":
msg(DiscordOrigin, discordMsg, "Reconnecting", `Position: ${webserver.queuePlace} \n Estimated time until login: ${webserver.ETA}`);
break;
case "connected":
msg(DiscordOrigin, discordMsg, "Connected", `Proxy is connected!`);
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":
let calcMsg =
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 "connected":
stopQueing();
stopMsg(DiscordOrigin, discordMsg, "Connected");
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 formatPlayerList(playerList) {
if (playerList.size == 0 || !playerList) {
return "No players seen this session so far!"
} else {
let usernameString = "**Players:**\n"
playerList.forEach(player => {
usernameString = `${usernameString}\n${player}`
})
return usernameString
}
}
function getNamesFromPlayers(arr) {
if (arr.size == 0) {
return false
} else {
let usernameArr = []
arr.forEach(player => {
usernameArr.push(player.name)
})
return usernameArr
}
}
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 {
var eachLine = content.split('\n');
for (var i = 0, l = eachLine.length; i < l; i++) {
if (eachLine[i].length != 0) {
console.log(`${eachLine[i]}`);
}
}
}
}
function sendDiscordMsg(channel, title, content) {
const MessageEmbed = {
color: 3447003,
author: {
name: client.username,
icon_url: playerIcon
},
fields: [{
name: title,
value: content
}],
timestamp: new Date()
}
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.`)
});
}
var mcLoop = function (mcLoop, interaction) {
let subCommand = interaction.options.getSubcommand()
if (subCommand == "status") {
if (JSON.stringify(webserver.restartQueue) == "true")
interaction.editReply("Loop is enabled");
else
interaction.editReply("Loop is disabled");
}
else if (subCommand == "enable") {
if (JSON.stringify(webserver.restartQueue) == "true")
interaction.editReply("Loop is already enabled!");
else {
webserver.restartQueue = true
interaction.editReply("Enabled Loop");
}
}
else if (subCommand == "disable") {
if (JSON.stringify(webserver.restartQueue) == "false")
interaction.editReply("Loop is already disabled!");
else {
webserver.restartQueue = false
interaction.editReply("Disabled Loop");
}
}
}
PubSub.subscribe('mcLoop', mcLoop);
var mcStats = function (mcStats, interaction) {
try {
if (conn.bot.health == undefined && conn.bot.food == undefined) {
interaction.editReply("Unknown.")
}
else {
if (conn.bot.health == 0)
interaction.editReply("Health: DEAD");
else
interaction.editReply("Health: " + Math.ceil(conn.bot.health) / 2 + "/10 \nHunger: " + conn.bot.food / 2 + "/10");
}
} catch (err) { interaction.editReply(`Start 2B2W first with "Start".`) }
}
PubSub.subscribe('mcStats', mcStats);
var sendMcMsg = function (mcMsg, data) {
conn.bot.chat(data)
}
PubSub.subscribe('sendMcMsg', sendMcMsg);
var seenPlayers = function (seen, interaction) {
interaction.editReply(formatPlayerList(playersSeen))
}
PubSub.subscribe('seen', seenPlayers);
var startProxy = function (start, interaction) {
startQueuing()
interaction.editReply("Queue is starting up!")
}
PubSub.subscribe('start', startProxy);
var stopProxy = function (start, interaction) {
stopQueing()
interaction.editReply("Stoping the proxy!")
}
PubSub.subscribe('stop', stopProxy);
var exitProxy = function (start, interaction) {
interaction.editReply("Exiting the proxy!").then(() => {
return process.exit(0)
})
}
PubSub.subscribe('exit', exitProxy);
var visualRange = function (visualRange, interaction) {
interaction.editReply(formatPlayerList(getNamesFromPlayers(inRange)))
}
PubSub.subscribe('visualRange', visualRange);
var manageWhitelist = function (whitelist, interaction) {
let username = interaction.options.getString("username")
if (interaction.options.getSubcommand() === "add") {
check = util.getUUIDForWhitelist(username)
check.then(promise => {
if (promise[0] != undefined) {
console.log("Adding fo real");
whitelistUUIDs.push(promise[0])
interaction.editReply(`Added ${username} to Whitelist!`)
} else {
interaction.editReply("Username wrong?");
}
})
} else if (interaction.options.getSubcommand() === "remove") {
check = util.getUUIDForWhitelist(username)
check.then(promise => {
if (promise[0] != undefined) {
whitelistUUIDs = whitelistUUIDs.filter(({ id }) => id !== promise.id)
interaction.editReply(`Removed ${username} from Whitelist!`)
} else {
interaction.editReply("Username wrong?");
}
})
} else {
if (!config.whitelist.enabled) {
interaction.editReply(`Listing all whitelisted users: \n${client.username}`)
} else {
interaction.editReply("Listing all whitelisted users:" + whitelistUUIDs.map(accounts => "\n" + accounts.name + ""))
}
}
}
PubSub.subscribe('whitelist', manageWhitelist)
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: function () {
startQueuing();
},
filterPacketAndSend: function () {
filterPacketAndSend();
},
stop: function () {
stopQueing();
},
sendMcMsg: function () {
sendMcMsg();
}
};