-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathbot-authme-first.js
More file actions
463 lines (402 loc) · 14.7 KB
/
bot-authme-first.js
File metadata and controls
463 lines (402 loc) · 14.7 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
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
const mineflayer = require('mineflayer');
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder');
// Configuration - Edit these values for your server
const config = {
server: {
host: 'localhost', // Change to your server IP
port: 25565,
version: '1.20.4' // Change to your server version
},
bot: {
username: 'AFKBot', // Change to your desired bot name
auth: 'offline', // 'offline', 'microsoft', or 'mojang'
password: '', // Minecraft account password (if using premium auth)
authmePassword: 'change_this_password' // AuthMe password for /register and /login
},
serverCommands: {
enabled: true,
joinServer: '/server survival', // Command to join specific server AFTER AuthMe
delay: 3000 // Wait 3 seconds after AuthMe before sending server command
},
features: {
autoReconnect: {
enabled: true,
delay: 5000
},
movement: {
enabled: true,
coordinates: {
x: 0, // Change to your desired AFK coordinates
y: 64,
z: 0
}
},
antiAFK: {
enabled: true,
jump: true,
sneak: false,
look: true,
interval: 30000 // 30 seconds
},
chatMessages: {
enabled: false,
interval: 300000, // 5 minutes
messages: [
'Still here!',
'AFK farming...',
'Bot is active'
]
},
chatLog: {
enabled: true
}
}
};
let bot;
let isAuthenticated = false;
let loginAttempts = 0;
let serverJoined = false;
let authmeCompleted = false;
const maxLoginAttempts = 3;
function createBot() {
console.log('🤖 Creating bot...');
const botOptions = {
host: config.server.host,
port: config.server.port,
username: config.bot.username,
version: config.server.version,
hideErrors: false
};
// Add authentication based on type
if (config.bot.auth === 'microsoft') {
botOptions.auth = 'microsoft';
} else if (config.bot.auth === 'mojang' && config.bot.password) {
botOptions.password = config.bot.password;
botOptions.auth = 'mojang';
} else {
botOptions.auth = 'offline';
}
bot = mineflayer.createBot(botOptions);
// Load pathfinder plugin
bot.loadPlugin(pathfinder);
bot.once('spawn', () => {
console.log(`✅ Bot ${bot.username} successfully joined the server!`);
// Reset all status variables
isAuthenticated = false;
loginAttempts = 0;
serverJoined = false;
authmeCompleted = false;
// Set up movement settings
const mcData = require('minecraft-data')(bot.version);
const defaultMove = new Movements(bot, mcData);
bot.pathfinder.setMovements(defaultMove);
// FIRST: Attempt AuthMe authentication
console.log('📋 Step 1: Starting AuthMe authentication...');
setTimeout(() => {
attemptAuthMeLogin();
}, 3000);
});
// Manual AuthMe authentication handling
bot.on('chat', (username, message, translate, jsonMsg, matches) => {
if (config.features.chatLog.enabled && username !== bot.username) {
console.log(`💬 [${username}] ${message}`);
}
// Handle server messages for AuthMe
if (username === bot.username) return;
const lowerMessage = message.toLowerCase();
// Server join success detection (for survival server)
if (serverJoined && lowerMessage.includes('survival') &&
(lowerMessage.includes('joined') || lowerMessage.includes('connected') || lowerMessage.includes('welcome'))) {
console.log('🌍 Successfully joined survival server!');
console.log('📋 Step 3: Starting bot activities...');
setTimeout(startBotActivities, 2000);
}
// Common AuthMe registration messages
if ((lowerMessage.includes('register') || lowerMessage.includes('registration')) &&
(lowerMessage.includes('password') || lowerMessage.includes('/register') || lowerMessage.includes('command'))) {
console.log('🔐 Registration required detected');
setTimeout(() => {
const password = config.bot.authmePassword;
bot.chat(`/register ${password} ${password}`);
console.log('📝 Sent registration command');
}, 1500);
}
// Common AuthMe login messages
else if ((lowerMessage.includes('login') || lowerMessage.includes('log in')) &&
(lowerMessage.includes('password') || lowerMessage.includes('/login') || lowerMessage.includes('command'))) {
console.log('🔑 Login required detected');
setTimeout(() => {
bot.chat(`/login ${config.bot.authmePassword}`);
console.log('🔓 Sent login command');
loginAttempts++;
}, 1500);
}
// AuthMe Success messages - THEN join survival server
else if ((lowerMessage.includes('successfully') || lowerMessage.includes('welcome') || lowerMessage.includes('logged')) &&
(lowerMessage.includes('logged') || lowerMessage.includes('registered') || lowerMessage.includes('authenticated'))) {
console.log('✅ AuthMe authentication successful!');
isAuthenticated = true;
authmeCompleted = true;
// NOW join the survival server after AuthMe success
if (config.serverCommands.enabled && config.serverCommands.joinServer) {
console.log('📋 Step 2: AuthMe completed, now joining survival server...');
setTimeout(() => {
joinSpecificServer();
}, config.serverCommands.delay);
} else {
// If no server command, just start activities
setTimeout(startBotActivities, 2000);
}
}
// Failed login messages
else if (lowerMessage.includes('wrong password') ||
lowerMessage.includes('incorrect password') ||
lowerMessage.includes('invalid password')) {
console.log('❌ AuthMe login failed - wrong password');
if (loginAttempts < maxLoginAttempts) {
console.log(`🔄 Retrying login (${loginAttempts}/${maxLoginAttempts})...`);
setTimeout(() => {
bot.chat(`/login ${config.bot.authmePassword}`);
loginAttempts++;
}, 3000);
} else {
console.log('🚫 Max login attempts reached');
}
}
// Timeout messages
else if (lowerMessage.includes('timeout') ||
(lowerMessage.includes('time') && lowerMessage.includes('up')) ||
lowerMessage.includes('too slow')) {
console.log('⏰ AuthMe timeout detected');
if (!authmeCompleted) {
setTimeout(attemptAuthMeLogin, 2000);
}
}
// Already registered messages
else if (lowerMessage.includes('already') && lowerMessage.includes('registered')) {
console.log('ℹ️ Already registered, attempting login...');
setTimeout(() => {
bot.chat(`/login ${config.bot.authmePassword}`);
console.log('🔓 Sent login command after registration notice');
}, 1500);
}
// Error messages that might indicate we need to try AuthMe again
else if (lowerMessage.includes('not authenticated') || lowerMessage.includes('please login')) {
console.log('⚠️ Authentication required message detected');
if (!authmeCompleted) {
setTimeout(attemptAuthMeLogin, 1000);
}
}
});
bot.on('error', (err) => {
console.error('❌ Bot error:', err.message);
});
bot.on('kicked', (reason) => {
console.log('⚠️ Bot was kicked:', reason);
if (config.features.autoReconnect.enabled) {
console.log(`🔄 Reconnecting in ${config.features.autoReconnect.delay / 1000} seconds...`);
setTimeout(createBot, config.features.autoReconnect.delay);
}
});
bot.on('end', () => {
console.log('🔌 Bot disconnected from server');
if (config.features.autoReconnect.enabled) {
console.log(`🔄 Reconnecting in ${config.features.autoReconnect.delay / 1000} seconds...`);
setTimeout(createBot, config.features.autoReconnect.delay);
}
});
bot.on('death', () => {
console.log('💀 Bot died and respawned');
setTimeout(() => {
if (authmeCompleted && serverJoined) {
startBotActivities();
} else if (authmeCompleted && !serverJoined) {
joinSpecificServer();
} else {
attemptAuthMeLogin();
}
}, 3000);
});
// Handle pathfinder events
bot.on('goal_reached', () => {
console.log('🎯 Reached target location!');
});
bot.on('path_update', (r) => {
if (r && r.visitedNodes && r.time) {
const nodesPerTick = (r.visitedNodes * 50 / r.time).toFixed(2);
console.log(`🗺️ Pathfinding: ${r.visitedNodes} nodes, ${nodesPerTick} nodes/s, ${r.time.toFixed(2)} ms`);
}
});
return bot;
}
function joinSpecificServer() {
console.log(`🌍 Now joining survival server with: ${config.serverCommands.joinServer}`);
bot.chat(config.serverCommands.joinServer);
console.log(`📤 Sent server join command: ${config.serverCommands.joinServer}`);
serverJoined = true;
// If no response indicating successful server join after 10 seconds, start activities anyway
setTimeout(() => {
if (authmeCompleted && !serverJoined) {
console.log('⚠️ No server join confirmation, starting activities anyway...');
startBotActivities();
}
}, 10000);
}
function attemptAuthMeLogin() {
if (authmeCompleted) {
console.log('ℹ️ AuthMe already completed, skipping...');
return;
}
console.log('🔐 Attempting AuthMe authentication...');
// Try registration first, then login
setTimeout(() => {
const password = config.bot.authmePassword;
bot.chat(`/register ${password} ${password}`);
console.log('📝 Attempted registration');
}, 2000);
setTimeout(() => {
bot.chat(`/login ${config.bot.authmePassword}`);
console.log('🔑 Attempted login');
loginAttempts++;
}, 4000);
// If no AuthMe response after 15 seconds, assume it's completed and proceed
setTimeout(() => {
if (!authmeCompleted) {
console.log('⚠️ No AuthMe response detected, assuming authentication completed...');
isAuthenticated = true;
authmeCompleted = true;
if (config.serverCommands.enabled && config.serverCommands.joinServer) {
console.log('📋 Step 2: Proceeding to join survival server...');
setTimeout(() => {
joinSpecificServer();
}, config.serverCommands.delay);
} else {
startBotActivities();
}
}
}, 15000);
}
function startBotActivities() {
if (!authmeCompleted) {
console.log('⚠️ Cannot start activities - AuthMe not completed yet');
return;
}
console.log('🎮 Starting bot activities on survival server...');
// Move to specified coordinates
if (config.features.movement.enabled) {
const { x, y, z } = config.features.movement.coordinates;
console.log(`🚶 Moving to coordinates: ${x}, ${y}, ${z}`);
try {
const goal = new goals.GoalBlock(x, y, z);
bot.pathfinder.setGoal(goal);
} catch (error) {
console.log('⚠️ Pathfinding error:', error.message);
}
}
// Start anti-AFK activities
if (config.features.antiAFK.enabled) {
console.log('🎯 Starting anti-AFK activities');
startAntiAFK();
}
// Start chat messages
if (config.features.chatMessages.enabled) {
console.log('💭 Starting periodic chat messages');
startChatMessages();
}
}
function startAntiAFK() {
const antiAfkConfig = config.features.antiAFK;
setInterval(() => {
if (!bot || !bot._client || bot._client.state !== 'play') return;
try {
if (antiAfkConfig.jump) {
bot.setControlState('jump', true);
setTimeout(() => {
if (bot && bot.setControlState) {
bot.setControlState('jump', false);
}
}, 100);
}
if (antiAfkConfig.sneak) {
bot.setControlState('sneak', true);
setTimeout(() => {
if (bot && bot.setControlState) {
bot.setControlState('sneak', false);
}
}, 200);
}
if (antiAfkConfig.look) {
const yaw = (Math.random() - 0.5) * Math.PI;
const pitch = (Math.random() - 0.5) * Math.PI / 2;
bot.look(yaw, pitch);
}
console.log('🔄 Anti-AFK action performed');
} catch (error) {
console.log('⚠️ Anti-AFK error (bot may be disconnected):', error.message);
}
}, antiAfkConfig.interval);
}
function startChatMessages() {
const chatConfig = config.features.chatMessages;
let messageIndex = 0;
setInterval(() => {
if (!bot || !bot._client || bot._client.state !== 'play') return;
try {
if (chatConfig.messages.length > 0 && authmeCompleted) {
bot.chat(chatConfig.messages[messageIndex]);
console.log(`💬 Sent chat message: ${chatConfig.messages[messageIndex]}`);
messageIndex = (messageIndex + 1) % chatConfig.messages.length;
}
} catch (error) {
console.log('⚠️ Chat message error (bot may be disconnected):', error.message);
}
}, chatConfig.interval);
}
// Start the bot
console.log('🤖 Starting Minecraft AFK Bot with correct AuthMe → Server flow...');
console.log('📋 Bot Flow:');
console.log(' 1️⃣ Connect to server');
console.log(' 2️⃣ Complete AuthMe authentication');
console.log(' 3️⃣ Join survival server (/server survival)');
console.log(' 4️⃣ Start AFK activities');
console.log('');
console.log('📋 Configuration:');
console.log(` Server: ${config.server.host}:${config.server.port}`);
console.log(` Version: ${config.server.version}`);
console.log(` Username: ${config.bot.username}`);
console.log(` Auth: ${config.bot.auth}`);
console.log(` AuthMe Password: ${config.bot.authmePassword ? '[SET]' : '[NOT SET - PLEASE CONFIGURE]'}`);
console.log(` Server Command: ${config.serverCommands.enabled ? config.serverCommands.joinServer : 'Disabled'}`);
console.log(` Movement: ${config.features.movement.enabled ? 'Enabled' : 'Disabled'}`);
console.log(` Anti-AFK: ${config.features.antiAFK.enabled ? 'Enabled' : 'Disabled'}`);
console.log('');
if (config.bot.authmePassword === 'change_this_password') {
console.log('⚠️ WARNING: Please change the AuthMe password in the configuration!');
console.log('');
}
createBot();
// Graceful shutdown
process.on('SIGINT', () => {
console.log('🛑 Shutting down bot...');
if (bot) {
bot.quit('Bot shutting down');
}
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('🛑 Received SIGTERM, shutting down bot...');
if (bot) {
bot.quit('Bot shutting down');
}
process.exit(0);
});
process.on('uncaughtException', (error) => {
console.error('💥 Uncaught Exception:', error);
if (config.features.autoReconnect.enabled) {
console.log('🔄 Restarting bot due to uncaught exception...');
setTimeout(createBot, 5000);
}
});
process.on('unhandledRejection', (reason, promise) => {
console.error('💥 Unhandled Rejection at:', promise, 'reason:', reason);
});