-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
261 lines (226 loc) · 6.15 KB
/
index.js
File metadata and controls
261 lines (226 loc) · 6.15 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
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
//Add mineflayer-simple-voice-chat from github.com/forester302/mineflayer-simple-voice-chat to the directory
const mineflayer = require('mineflayer');
const { pathfinder, Movements } = require('mineflayer-pathfinder');
const autoeat = require('mineflayer-auto-eat').plugin;
const simple_voice_chat = require('./mineflayer-simple-voice-chat').plugin;
const config = require('config');
//const webserver = require('./webserver');
// define global variables
let bot = null;
const events = {
};
const keywords = {
};
let currentevent = 'default';
let targetedplayer = '';
let targetedlocation = [0, 0, 0];
// eslint-disable-next-line no-unused-vars, prefer-const
let timer = 0;
let shouldreconnect = config.get('options.autoreconnect');
let restart = false;
function initbot() {
bot = mineflayer.createBot({
host: config.get('server.host'),
port: config.get('server.port'),
auth: config.get('auth.type'),
version: config.get('server.version'),
});
bot.loadPlugin(pathfinder);
bot.loadPlugin(autoeat);
if (config.get('options.music.enabled')) {
bot.loadPlugin(simple_voice_chat);
}
//if (config.get('options.webui.enabled')) {
// webserver.init(events, keywords, bot);
//}
}
function reconnect() {
try {
console.log('reconnecting');
if (config.get('options.viewer.enabled')) {
bot.viewer.close();
}
startbot();
}
catch {
console.log('failed to connect');
}
}
function initEvents() {
const fs = require('node:fs');
const path = require('node:path');
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
let valid = false;
if ('id' in event && 'execute' in event) {
events[event.id] = event.execute;
valid = true;
}
if ('keyword' in event && 'init' in event) {
keywords[event.keyword] = event.init;
valid = true;
}
if (!valid) {
console.log(`${filePath} is invalid`);
}
}
}
function parsereturn(dict) {
if (typeof dict == 'undefined') return;
if ('currentevent' in dict) {
currentevent = dict.currentevent;
}
if ('targetedlocation' in dict) {
targetedlocation = dict.targetedlocation;
}
if ('targetedplayer' in dict) {
targetedplayer = dict.targetedplayer;
}
if ('timer' in dict) {
timer = dict.timer;
}
if ('trigger' in dict) {
parsereturn(keywords[dict.trigger]({
bot: bot,
username: dict.username,
message: dict.message,
targetedplayer: targetedplayer,
targetedlocation: targetedlocation,
timer: timer,
}))
}
}
function startbot() {
initbot();
bot.on('physicsTick', () => {
/*let dict = webserver.tick({
bot: bot,
targetedplayer: targetedplayer,
targetedlocation: targetedlocation,
timer: timer
});
parsereturn(dict);*/
dict = events[currentevent]({
bot: bot,
targetedplayer: targetedplayer,
targetedlocation: targetedlocation,
timer: timer,
});
parsereturn(dict);
});
bot.on('whisper', (username, message) => {
if (username == 'me' || username == bot.username) return;
message = message.split(' ');
//Add ability to reload events without restarting the bot
if (message[0] == "reload") {
initEvents()
console.log("Reloaded Events")
return
}
if (message[0] in keywords) {
if (!config.get("options.commanding-players").includes(username)) {
bot.chat(`/msg ${username} ${config.get("options.no-permissions-message")}`)
return;
}
const dict = keywords[message[0]]({
bot: bot,
username: username,
message: message,
targetedplayer: targetedplayer,
targetedlocation: targetedlocation,
timer: timer,
});
parsereturn(dict);
}
else {
bot.chat(`/msg ${username} ${config.get('options.reply-message')}`);
}
});
bot.once('spawn', () => {
restart = false;
const mcData = require('minecraft-data')(bot.version);
const movements = new Movements(bot, mcData);
bot.pathfinder.setMovements(movements);
bot.autoEat.options.useOffhand = true;
if (config.get('options.viewer.enabled')) {
require('./functions/viewer').init(bot);
}
if (config.get('options.afk.on-join')) {
bot.chat(config.get('options.afk.command'));
}
});
bot.on('kicked', (reason) => {
console.log(`Kicked for ${JSON.parse(reason)['text']}`);
const kickkeywords = ['banned', 'lag', 'afk', 'Lag', 'AFK', 'LAG', 'Banned', 'Farm', 'farm', 'location'];
if (String(reason) == 'undefined') return;
for (const word of kickkeywords) {
if (JSON.parse(reason)['text'].includes(word)) {
console.log('Aborting Reconnect');
shouldreconnect = false;
break;
}
}
});
bot.on('kicked', (reason) => {
console.log(`Kicked for ${JSON.parse(reason)['text']}`);
const reconnectkeywords = ['Server is restarting', 'restart', 'restarting', 'Restarting Server', 'Server closed'];
if (String(reason) == 'undefined') return;
for (const word of reconnectkeywords) {
if (JSON.parse(reason)['text'].includes(word)) {
console.log('Server is restarting, waiting 5 minutes')
shouldreconnect = false;
restart = true;
break;
}
}
});
// eslint-disable-next-line no-unused-vars
bot.on('end', (reason) => {
if (shouldreconnect) {
setTimeout(reconnect, 10000);
}
if (restart) {
setTimeout(reconnect, 300000);
console.log('i made it here, waiting 5 minutes');
}
});
}
async function asyncConsole() {
const readline = require('readline');
const readLineAsync = () => {
const rl = readline.createInterface({
input: process.stdin
});
return new Promise((resolve) => {
rl.prompt();
rl.on('line', (line) => {
rl.close();
resolve(line);
});
});
};
while (true) {
let message = (await readLineAsync()).split(' ');
console.log(message)
if (message[0] in keywords) {
const dict = keywords[message[0]]({
bot: bot,
username: "console",
message: message,
targetedplayer: targetedplayer,
targetedlocation: targetedlocation,
timer: timer,
});
parsereturn(dict);
}
else {
console.log(`${config.get('options.reply-message')}`);
}
}
}
asyncConsole()
initEvents();
startbot()