-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
84 lines (69 loc) · 2.7 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
const mineflayer = require("mineflayer");
const { randomizeTimer, Timers } = require("./src/utils/timerManager");
const { sendWebhookLog } = require("./src/utils/webhookLogger");
const { createLogger } = require("./src/utils/botLogger");
const { createMessageHandler } = require("./src/handlers/messageHandler");
const { createWindowHandler } = require("./src/handlers/windowHandler");
const { GameState } = require("./src/utils/gameState");
const { ConnectionManager } = require("./src/utils/connectionManager");
const config = require("./config.json");
const log = createLogger(config);
const gameState = new GameState();
const connectionManager = new ConnectionManager(config);
function setupBot(bot) {
connectionManager.setCurrentBot(bot);
bot.on('windowOpen', createWindowHandler(bot, log));
bot.once("login", async () => {
await connectionManager.handleConnect();
await log(`Le ${bot.username} a rejoint le serveur.`);
await sendWebhookLog(`🟢 ${bot.username} s'est connecté au serveur`, 'success');
setTimeout(() => {
if (gameState.canExecuteCommand('/skyblock') && connectionManager.canPerformAction()) {
bot.chat("/skyblock");
gameState.updateLastCommand('/skyblock');
log("Commande /skyblock envoyée");
}
}, randomizeTimer(Timers.SKYBLOCK));
});
const messageHandler = createMessageHandler(bot, log, config, connectionManager);
bot.on("message", messageHandler);
bot.on('error', async (error) => {
await log(`Erreur du bot: ${error.message}`);
await sendWebhookLog(`❌ Erreur: ${error.message}`, 'error');
});
bot.on('end', async () => {
await log("Déconnecté du serveur.");
gameState.setInSkyblock(false);
await connectionManager.handleDisconnect(log, createBotConnection);
});
return bot;
}
function createBotConnection() {
const bot = mineflayer.createBot({
host: config.server.ip,
port: config.server.port,
username: config.account.username,
version: "1.8.9",
auth: "microsoft",
plugins: {
breath: false
},
connectTimeout: 30000
});
bot.config = config;
bot.gameState = gameState;
return setupBot(bot);
}
let bot = createBotConnection();
process.on('unhandledRejection', async (error) => {
await log(`Erreur non gérée: ${error.message}`);
await sendWebhookLog(`❌ Erreur non gérée: ${error.message}`, 'error');
});
process.on('SIGINT', async () => {
await log("Arrêt du bot...");
await sendWebhookLog("🔴 Arrêt du bot", "warning");
if (bot.connected) {
bot.quit();
}
process.exit(0);
});