-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
104 lines (87 loc) · 3.04 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
100
101
102
103
104
const mineflayer = require('mineflayer');
const http = require('http');
// Configuration
const config = {
host: 'DeadBEDSMP.aternos.me', // Minecraft server IP or hostname
port: 42585, // Server port
username: 'PuchkiXD', // Bot username
version: '1.20.1', // Minecraft version
botPassword: '000000000', // Password for registration/login
httpPort: 3000, // Render port binding (HTTP server)
};
let isRegistered = false; // Tracks whether the bot has registered
// Create the bot
function createBot() {
const bot = mineflayer.createBot({
host: config.host,
port: config.port,
username: config.username,
version: config.version,
});
// Handle bot events
bot.on('login', () => {
console.log(`[Bot] Logged in as ${bot.username}`);
});
bot.on('spawn', () => {
console.log('[Bot] Spawned in the world.');
authenticateBot(bot);
});
bot.on('chat', (username, message) => {
if (username === bot.username) return; // Ignore the bot's own messages
console.log(`[Chat] ${username}: ${message}`);
});
bot.on('message', (jsonMsg) => {
console.log('[Server Message]', jsonMsg.toString());
});
bot.on('error', (err) => {
console.error(`[Bot Error] ${err.message}`);
});
bot.on('kicked', (reason, loggedIn) => {
console.error(`[Bot Kicked] Reason: ${reason}, Logged In: ${loggedIn}`);
});
bot.on('end', () => {
console.log('[Bot] Disconnected. Reconnecting...');
setTimeout(() => createBot(), 5000); // Reconnect after 5 seconds
});
return bot;
}
// Authenticate the bot (register or login)
function authenticateBot(bot) {
console.log('[Bot] Authenticating...');
setTimeout(() => {
bot.chat(`/login ${config.botPassword}`);
console.log('[Bot] Sent login command.');
}
}, 3000); // Wait 3 seconds to ensure the bot is fully spawned
}
// HTTP server to interact with the bot
function startHttpServer(bot) {
const server = http.createServer((req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const command = url.pathname.slice(1); // Extract command from URL path
if (command === 'status') {
const status = bot && bot.entity ? 'Online' : 'Offline';
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Bot Status: ${status}`);
} else if (command.startsWith('say')) {
const chatMessage = decodeURIComponent(command.slice(4));
if (bot && bot.entity) {
bot.chat(chatMessage); // Correct function call
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Bot said: "${chatMessage}"`);
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Bot is not online.');
}
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Unknown command. Available commands: /status, /say/<message>');
}
});
server.listen(config.httpPort, () => {
console.log(`[HTTP Server] Listening on port ${config.httpPort}`);
});
}
// Start the bot and HTTP server
const bot = createBot();
startHttpServer(bot);