forked from souravkl11/raganork-md
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (84 loc) · 2.86 KB
/
Copy pathindex.js
File metadata and controls
99 lines (84 loc) · 2.86 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
const path = require("path");
const fs = require("fs");
if (fs.existsSync("./config.env")) {
require("dotenv").config({ path: "./config.env" });
}
const { suppressLibsignalLogs } = require("./core/helpers");
suppressLibsignalLogs();
const { initializeDatabase } = require("./core/database");
const { BotManager } = require("./core/manager");
const config = require("./config");
const { SESSION, logger } = config;
const http = require("http");
const {
ensureTempDir,
TEMP_DIR,
initializeKickBot,
cleanupKickBot,
} = require("./core/helpers");
async function main() {
ensureTempDir();
logger.info(`Created temporary directory at ${TEMP_DIR}`);
console.log(`Raganork v${require("./package.json").version}`);
console.log(`- Configured sessions: ${SESSION.join(", ")}`);
logger.info(`Configured sessions: ${SESSION.join(", ")}`);
if (SESSION.length === 0) {
const warnMsg =
"⚠️ No sessions configured. Please set SESSION environment variable.";
console.warn(warnMsg);
logger.warn(warnMsg);
return;
}
try {
await initializeDatabase();
console.log("- Database initialized");
logger.info("Database initialized successfully.");
} catch (dbError) {
console.error(
"🚫 Failed to initialize database or load configuration. Bot cannot start.",
dbError
);
logger.fatal(
"🚫 Failed to initialize database or load configuration. Bot cannot start.",
dbError
);
process.exit(1);
}
const botManager = new BotManager();
const shutdownHandler = async (signal) => {
console.log(`\nReceived ${signal}, shutting down...`);
logger.info(`Received ${signal}, shutting down...`);
cleanupKickBot();
await botManager.shutdown();
process.exit(0);
};
process.on("SIGINT", () => shutdownHandler("SIGINT"));
process.on("SIGTERM", () => shutdownHandler("SIGTERM"));
await botManager.initializeBots();
console.log("- Bot initialization complete.");
logger.info("Bot initialization complete");
initializeKickBot();
const startServer = () => {
const PORT = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
if (req.url === "/health") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("OK");
} else {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Raganork Bot is running!");
}
});
server.listen(PORT, () => {
logger.info(`Web server listening on port ${PORT}`);
});
};
if (process.env.USE_SERVER !== "false") startServer();
}
if (require.main === module) {
main().catch((error) => {
console.error(`Fatal error in main execution: ${error.message}`, error);
logger.fatal({ err: error }, `Fatal error in main execution`);
process.exit(1);
});
}