-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
117 lines (101 loc) · 4.51 KB
/
bot.js
File metadata and controls
117 lines (101 loc) · 4.51 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
const express = require('express');
const mineflayer = require('mineflayer');
const { pathfinder, Movements, goals: { GoalBlock } } = require('mineflayer-pathfinder');
const config = require('./settings.json');
const app = express();
app.get('/', (req, res) => res.send('Bot is running!'));
app.listen(process.env.PORT || 5000, () => console.log('[Server] Keep-alive running'));
let reconnectDelay = config.utils['auto-reconnect-delay'] || 2000;
const maxDelay = config.utils['max-reconnect-delay'] || 120000;
function createBot() {
const botOptions = {
username: config['bot-account'].username,
password: config['bot-account'].password || undefined,
auth: config['bot-account'].type === 'microsoft' ? 'microsoft' : 'offline',
host: config.server.ip,
port: config.server.port,
};
if (config.server.version) botOptions.version = config.server.version;
const bot = mineflayer.createBot(botOptions);
bot.loadPlugin(pathfinder);
bot.once('spawn', () => {
console.log(`[Bot] Joined ${config.server.ip}`);
reconnectDelay = config.utils['auto-reconnect-delay'] || 2000;
if (config.utils['auto-auth'].enabled) {
const pass = config.utils['auto-auth'].password;
setTimeout(() => {
bot.chat(`/register ${pass} ${pass}`);
bot.chat(`/login ${pass}`);
console.log('[Bot] Sent auth commands');
}, 500);
}
if (config.utils['anti-afk'].enabled && config.utils['anti-afk'].sneak) {
bot.setControlState('sneak', true);
}
if (config.movement && config.movement.enabled) {
const mcData = require('minecraft-data')(bot.version);
const movements = new Movements(bot, mcData);
bot.pathfinder.setMovements(movements);
if (config.movement['circle-walk'].enabled) {
const { radius, speed } = config.movement['circle-walk'];
let angle = 0;
const origin = { x: null, z: null };
const t = setInterval(() => {
if (!bot.entity) return;
if (origin.x === null) { origin.x = bot.entity.position.x; origin.z = bot.entity.position.z; }
bot.pathfinder.setGoal(new GoalBlock(Math.round(origin.x + radius * Math.cos(angle)), bot.entity.position.y, Math.round(origin.z + radius * Math.sin(angle))));
angle = (angle + 0.5) % (Math.PI * 2);
}, speed);
bot.once('end', () => clearInterval(t));
}
if (config.movement['look-around'].enabled) {
const t = setInterval(() => {
if (!bot.entity) return;
bot.look((Math.random() - 0.5) * Math.PI * 2, (Math.random() - 0.5) * Math.PI * 0.5, false);
}, config.movement['look-around'].interval);
bot.once('end', () => clearInterval(t));
}
if (config.movement['random-jump'].enabled) {
const t = setInterval(() => {
bot.setControlState('jump', true);
setTimeout(() => bot.setControlState('jump', false), 300);
}, config.movement['random-jump'].interval);
bot.once('end', () => clearInterval(t));
}
}
if (config.utils['chat-messages'].enabled) {
const { messages, repeat, 'repeat-delay': delay } = config.utils['chat-messages'];
if (repeat && messages.length > 0) {
let idx = 0;
const t = setInterval(() => { bot.chat(messages[idx]); idx = (idx + 1) % messages.length; }, delay * 1000);
bot.once('end', () => clearInterval(t));
}
}
if (config.combat && config.combat['attack-mobs']) {
const t = setInterval(() => {
const mob = bot.nearestEntity(e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 4);
if (mob) bot.attack(mob);
}, 1000);
bot.once('end', () => clearInterval(t));
}
});
bot.on('chat', (username, message) => {
if (username === bot.username) return;
if (config.utils['chat-log']) console.log(`[Chat] <${username}> ${message}`);
if (config.chat && config.chat.respond) {
const lower = message.toLowerCase();
if (lower.includes('hello') || lower.includes('hi')) bot.chat(`Hey ${username}!`);
else if (lower.includes('bot') || lower.includes(bot.username.toLowerCase())) bot.chat(`Yes? I'm ${bot.username} :)`);
}
});
bot.on('error', err => console.error('[Bot] Error:', err.message));
bot.on('end', reason => {
console.log(`[Bot] Disconnected: ${reason}`);
if (config.utils['auto-reconnect']) {
setTimeout(createBot, reconnectDelay);
reconnectDelay = Math.min(reconnectDelay * 2, maxDelay);
}
});
bot.on('kicked', reason => console.log('[Bot] Kicked:', reason));
}
createBot();