-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminecraftBot.js
More file actions
97 lines (91 loc) · 3.17 KB
/
minecraftBot.js
File metadata and controls
97 lines (91 loc) · 3.17 KB
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
const Logger = require("./Logger");
console.info = m => {
if (m.startsWith('To sign in, use a web browser')) {
const authCode = /code (\w{8})/g.exec(m)[1];
return Logger.info(`Manual authentication required, Visit https://www.microsoft.com/link and your code is **${authCode}**`);
}
console.log(m);
}
const mineflayer = require('mineflayer');
module.exports = class MinecraftBot {
constructor() {
this.noReconnect = "no-restart";
this.opts = {
checkTimeoutInterval: 1000 * 60 * 3, // 3min
username: process.env.USER,
auth: 'microsoft',
host: process.env.SERVER_IP,
version: "1.18.2",
};
this.bot = null;
this.log = Logger;
this.config = {
"anti-afk": {
enabled: false,
sneak: false
},
"auto-reconnect": true,
"auto-reconnect-min-delay": 1000 * 60 * 2, // Min delay to 2 mins
"auto-reconnect-max-delay": 1000 * 60 * 5 // Max delay to 5 mins
};
}
registerEvent() {
if(!this.bot) return;
this.bot.once('spawn', this.spawn.bind(this));
this.bot.on('end', this.botDown.bind(this));
this.bot.on('error', this.log.error);
this.bot.on('death', this.death.bind(this));
this.bot.on('kicked', this.kicked.bind(this));
}
// Listeners
spawn = () => {
if (!this.opts['password'])
this.opts['password'] = process.env.PASS;
this.bot.chat(process.env.CHAT_PASS);
this.log.info("Bot has joined the server!");
// Lame anti-afk but I dont need it rn so who cares
if (this.config["anti-afk"].enabled) {
this.bot.setControlState('jump', true);
if (this.config.utils['anti-afk'].sneak)
this.bot.setControlState('sneak', true);
}
}
death = () =>
this.log.info(`Bot has been died and was respawned ${this.bot.entity.position}`);
kicked = reason =>
this.log.info( `Bot was kicked from the server. Reason: \n${reason})`);
botDown = reason => {
this.bot = null;
if (reason !== this.noReconnect && this.config["auto-reconnect"]) {
const delay = this.getRandom(this.config["auto-reconnect-max-delay"], this.config["auto-reconnect-min-delay"]);
this.log.info(`Restarting bot in ${Math.round(delay / (1000 * 60))} minutes!`);
this.timeout = setTimeout(this.join.bind(this), delay);
}
}
// Methods
join() {
if (this.bot) return;
this.bot = mineflayer.createBot(this.opts);
this.registerEvent();
}
reconnect() {
if(!this.bot) return;
this.leaveServer();
this.join();
}
shutdown() {
if (!this.bot) return;
this.leaveServer();
this.bot.end(this.noReconnect);
}
leaveServer() {
if (!this.bot) return;
if (this.timeout)
clearTimeout(this.timeout)
this.bot.quit(this.noReconnect);
this.log.info("Bot has left the server!");
}
getRandom(max, min) {
return Math.random() * (max - min) + min;
}
}