forked from JadeMin/aterbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (73 loc) · 2.34 KB
/
index.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
const http = require('http');
const mineflayer = require('mineflayer');
const CONFIG = require("./config.json");
let connected = false;
const sleep = ms => new Promise(resovle => setTimeout(resovle, ms));
const getRandom = array => array[Math.floor(Math.random() * (array.length - 0)) + 0];
const cLog = (msg, ...args) => {
if(CONFIG.logger[0]) console.log(msg, ...args);
};
async function reconnect() {
console.log(`Trying to reconnect in ${CONFIG.retryTimes / 1000}s...\n`);
connected = false;
await sleep(CONFIG.retryTimes);
createAFKBot();
}
function createAFKBot() {
const bot = mineflayer.createBot({
host: CONFIG.host,
port: CONFIG.port,
username: CONFIG.username
});
bot.on('spawn', () => {
connected = true;
async function doMoving() {
if(connected) {
const lastAction = getRandom(CONFIG.actions);
bot.setControlState(lastAction, true); // starts the selected random action
if(Math.random() < 0.5) { // 50% chance
bot.setControlState('sprint', true);
}
cLog(`${lastAction}${bot.getControlState('sprint')? " with sprint":''}`);
await sleep(getRandom(CONFIG.actionDelays));
bot.setControlState(lastAction, false); // starts the selected random action
bot.setControlState('sprint', false);
}
await sleep(getRandom(CONFIG.actionDelays));
doMoving();
}
async function changeViewPos() {
if(connected) {
const yaw = (Math.random() * Math.PI) - (0.5 * Math.PI);
const pitch = (Math.random() * Math.PI) - (0.5 * Math.PI);
bot.look(yaw, pitch, false);
}
await sleep(getRandom(CONFIG.actionDelays));
changeViewPos();
}
changeViewPos();
doMoving();
});
bot.on('error', error => {
console.error(`AFKBot got an error: ${error}`);
reconnect();
});
bot.on('kicked', async rawResponse => {
const response = JSON.parse(rawResponse);
if(!(response instanceof Error)) {
console.error(`\n\nAFKbot is disconnected by reason: ${response?.with?.map(v=> v.text).join('\n')}`);
}
reconnect();
});
bot.on('login', () => {
console.log(`AFKBot logged in ${CONFIG.username}\n\n`);
});
}
createAFKBot();
const server = http.createServer((_request, response) => {
response.writeHead(200, {"Content-Type": 'text/html'});
response.end('Pong!');
});
server.listen(process.env.PORT || 3000, () => {
console.log('Web for AntiAFK is running...');
});