This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 478
/
bot.js
183 lines (149 loc) · 5.34 KB
/
bot.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
const mineflayer = require('mineflayer');
const Movements = require('mineflayer-pathfinder').Movements;
const pathfinder = require('mineflayer-pathfinder').pathfinder;
const { GoalBlock, GoalXZ } = require('mineflayer-pathfinder').goals;
const config = require('./settings.json');
const loggers = require('./logging.js');
const logger = loggers.logger;
function createBot() {
const bot = mineflayer.createBot({
username: config['bot-account']['username'],
password: config['bot-account']['password'],
auth: config['bot-account']['type'],
host: config.server.ip,
port: config.server.port,
version: config.server.version,
});
bot.loadPlugin(pathfinder);
const mcData = require('minecraft-data')(bot.version);
const defaultMove = new Movements(bot, mcData);
bot.settings.colorsEnabled = false;
bot.pathfinder.setMovements(defaultMove);
bot.once('spawn', () => {
logger.info("Bot joined to the server");
if (config.utils['auto-auth'].enabled) {
logger.info('Started auto-auth module');
let password = config.utils['auto-auth'].password;
setTimeout(() => {
bot.chat(`/register ${password} ${password}`);
bot.chat(`/login ${password}`);
}, 500);
logger.info(`Authentication commands executed`);
}
if (config.utils['chat-messages'].enabled) {
logger.info('Started chat-messages module');
let messages = config.utils['chat-messages']['messages'];
if (config.utils['chat-messages'].repeat) {
let delay = config.utils['chat-messages']['repeat-delay'];
let i = 0;
setInterval(() => {
bot.chat(`${messages[i]}`);
if (i + 1 === messages.length) {
i = 0;
} else i++;
}, delay * 1000);
} else {
messages.forEach((msg) => {
bot.chat(msg);
});
}
}
const pos = config.position;
if (config.position.enabled) {
logger.info(
`Starting moving to target location (${pos.x}, ${pos.y}, ${pos.z})`
);
bot.pathfinder.setGoal(new GoalBlock(pos.x, pos.y, pos.z));
}
if (config.utils['anti-afk'].enabled) {
if (config.utils['anti-afk'].sneak) {
bot.setControlState('sneak', true);
}
if (config.utils['anti-afk'].jump) {
bot.setControlState('jump', true);
}
if (config.utils['anti-afk']['hit'].enabled) {
let delay = config.utils['anti-afk']['hit']['delay'];
let attackMobs = config.utils['anti-afk']['hit']['attack-mobs']
setInterval(() => {
if(attackMobs) {
let entity = bot.nearestEntity(e => e.type !== 'object' && e.type !== 'player'
&& e.type !== 'global' && e.type !== 'orb' && e.type !== 'other');
if(entity) {
bot.attack(entity);
return
}
}
bot.swingArm("right", true);
}, delay);
}
if (config.utils['anti-afk'].rotate) {
setInterval(() => {
bot.look(bot.entity.yaw + 1, bot.entity.pitch, true);
}, 100);
}
if (config.utils['anti-afk']['circle-walk'].enabled) {
let radius = config.utils['anti-afk']['circle-walk']['radius']
circleWalk(bot, radius);
}
}
});
bot.on('chat', (username, message) => {
if (config.utils['chat-log']) {
logger.info(`<${username}> ${message}`);
}
});
bot.on('goal_reached', () => {
if(config.position.enabled) {
logger.info(
`Bot arrived to target location. ${bot.entity.position}`
);
}
});
bot.on('death', () => {
logger.warn(
`Bot has been died and was respawned at ${bot.entity.position}`
);
});
if (config.utils['auto-reconnect']) {
bot.on('end', () => {
setTimeout(() => {
createBot();
}, config.utils['auto-reconnect-delay']);
});
}
bot.on('kicked', (reason) => {
let reasonText = JSON.parse(reason).text;
if(reasonText === '') {
reasonText = JSON.parse(reason).extra[0].text
}
reasonText = reasonText.replace(/§./g, '');
logger.warn(`Bot was kicked from the server. Reason: ${reasonText}`)
}
);
bot.on('error', (err) =>
logger.error(`${err.message}`)
);
}
function circleWalk(bot, radius) {
// Make bot walk in square with center in bot's wthout stopping
return new Promise(() => {
const pos = bot.entity.position;
const x = pos.x;
const y = pos.y;
const z = pos.z;
const points = [
[x + radius, y, z],
[x, y, z + radius],
[x - radius, y, z],
[x, y, z - radius],
];
let i = 0;
setInterval(() => {
if(i === points.length) i = 0;
bot.pathfinder.setGoal(new GoalXZ(points[i][0], points[i][2]));
i++;
}, 1000);
});
}
createBot();