-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmc.js
More file actions
142 lines (122 loc) · 4.31 KB
/
Copy pathmc.js
File metadata and controls
142 lines (122 loc) · 4.31 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
const readline = require('readline');
const dns = require('dns');
class MinecraftBot {
constructor(username, host, port, version, connectCommand) {
this.username = username;
this.host = host;
this.port = port;
this.version = version;
this.connectCommand = connectCommand;
this.bot = null;
this.rl = null;
this.reconnectInterval = 5000; // Reconnection interval in milliseconds
this.movingLeft = true; // Start direction
}
// Init bot instance
async initBot() {
const mineflayer = require('mineflayer');
this.bot = mineflayer.createBot({
username: this.username,
host: this.host,
port: this.port,
version: this.version
});
this.initEvents();
this.bot.on('end', () => {
this.log('Bot disconnected. Attempting to reconnect...');
setTimeout(() => {
this.initBot();
}, this.reconnectInterval);
});
}
// Logger
log(...msg) {
console.log(`[${this.username}]`, ...msg);
}
// Chat intake logger
chatLog(username, message) {
this.log(`<${username}> ${message}`);
}
// Send a chat message
sendChatMessage(message) {
if (this.bot) {
this.bot.chat(message);
this.chatLog(this.username, message);
} else {
this.log('Bot is not connected.');
}
}
// Initialize bot events
initEvents() {
this.bot.on('login', () => {
this.log('Bot connected successfully.');
this.sendChatMessage(this.connectCommand); // Send the specified command on connection
});
this.bot.on('message', (message) => {
const username = message.toAnsi();
const content = message.toString().substr(username.length + 1);
this.chatLog(username, content);
});
this.bot.on('error', (err) => {
this.log('Bot error:', err);
});
// Anti-AFK movement (left-right movement with random direction change)
this.startAntiAfkMovement();
}
// Anti-AFK left-right movement with random direction changes
startAntiAfkMovement() {
setInterval(() => {
// Randomly change direction every few seconds
this.movingLeft = Math.random() > 0.5;
if (this.movingLeft) {
this.bot.setControlState('right', false);
this.bot.setControlState('left', true);
} else {
this.bot.setControlState('left', false);
this.bot.setControlState('right', true);
}
}, 1000); // Adjust the interval for smoother or more delayed movement
}
// Connect to the server
async connectToServer(domain) {
dns.resolve4(domain, async (err, addresses) => {
if (err) {
this.log('Error resolving domain:', err);
return;
}
if (addresses.length === 0) {
this.log('No IP address found for the domain:', domain);
return;
}
await this.initBot();
this.initReadline();
});
}
// Initialize readline interface
initReadline() {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
this.rl.on('line', (input) => {
this.sendChatMessage(input);
});
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter server domain: ', (domain) => {
rl.question('Enter the server port: ', (port) => {
rl.question('Enter your Minecraft username: ', (username) => {
rl.question('Enter Minecraft version: ', (version) => {
rl.question('Enter the chat/command to send upon connection: ', (connectCommand) => {
rl.close();
const bot = new MinecraftBot(username, domain, port, version, connectCommand);
bot.connectToServer(domain);
});
});
});
});
});