-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (51 loc) · 1.91 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
const mineflayer = require('mineflayer');
const { pathfinder } = require('mineflayer-pathfinder');
const config = require('./config.json');
const { format } = require('date-fns');
const axios = require('axios');
let bot = null;
let botended = '';
console.clear();
function createBot() {
bot = mineflayer.createBot({
host: config.minecraft.server_ip,
username: config.minecraft.username,
auth: "microsoft",
version: "1.19.4",
autoJump: false, // Disable autojump
});
bot.loadPlugin(pathfinder);
bot.once('spawn', () => {
console.log(`Bot Spawned at X: ${bot.entity.position.x}, Y: ${bot.entity.position.y}, Z: ${bot.entity.position.z}`);
console.log('____________________________________CHAT________________________________________');
});
bot.on('chat', (username, message) => {
if (username === bot.username) return; // Ignore messages sent by the bot itself
const currentDate = new Date();
const formattedDate = currentDate.toLocaleDateString();
const formattedTime = currentDate.toLocaleTimeString();
const content = `**___${formattedDate} | ${formattedTime}___** ${username}: ${message}`;
sendToDiscordWebhook(config.discord.main_webhook, content);
console.log(content);
});
bot.on('kicked', (reason, loggedIn) => {
console.log(`Bot was kicked for reason: ${reason}`);
botended = `Bot Kicked: ${reason} Reconnecting...`;
setTimeout(createBot, 15000);
});
bot.once('end', () => {
console.log('Minecraft bot disconnected. Attempting to reconnect...');
setTimeout(createBot, 15000);
});
}
async function sendToDiscordWebhook(webhookUrl, message) {
try {
await axios.post(webhookUrl, {
content: message,
});
console.log('Message sent to Discord successfully.');
} catch (error) {
console.error('Failed to send message to Discord:', error);
}
}
createBot();