xargana-srv/index.js

93 lines
2.5 KiB
JavaScript
Raw Normal View History

2025-04-16 21:49:44 +02:00
// Main application entry point
const path = require('path');
require('dotenv').config();
// Import the Bot class
2025-04-16 21:51:40 +02:00
const Bot = require('./discord/classes/Bot');
2025-04-17 14:11:43 +02:00
2025-04-16 21:49:44 +02:00
// Global variables to hold our services
let apiServer;
let discordBot;
async function startServices() {
try {
// Start API server
console.log('Starting API server...');
apiServer = require('./api/server');
console.log('API server started successfully');
// Initialize and start Discord bot
console.log('Starting Discord bot...');
discordBot = new Bot();
await discordBot.start();
console.log('Discord bot started successfully');
console.log('All services started - System fully operational');
} catch (error) {
console.error('Error starting services:', error);
process.exit(1);
}
}
// Handle graceful shutdown
async function shutdown(signal) {
console.log(`Received ${signal}. Shutting down gracefully...`);
2025-04-17 14:11:43 +02:00
// Shutdown Discord bot if it exists
if (discordBot) {
2025-04-16 21:49:44 +02:00
try {
await discordBot.sendShutdownNotification(`Manual shutdown triggered by ${signal}`);
2025-04-17 14:11:43 +02:00
await discordBot.stop();
2025-04-16 21:49:44 +02:00
console.log('Discord bot shutdown complete');
} catch (error) {
console.error('Error shutting down Discord bot:', error);
}
}
// Add any API server shutdown logic here if needed
console.log('Shutdown complete');
process.exit(0);
}
// Register shutdown handlers
2025-04-17 16:09:18 +02:00
process.on('SIGINT', async () => {
console.log('Received SIGINT. Shutting down gracefully...');
try {
// If you have the bot instance available, call shutdown method
if (global.discordBot) {
await global.discordBot.sendShutdownNotification("SIGINT received");
}
} catch (error) {
console.error("Error shutting down Discord bot:", error);
}
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('Received SIGTERM. Shutting down gracefully...');
try {
// If you have the bot instance available, call shutdown method
if (global.discordBot) {
await global.discordBot.sendShutdownNotification("SIGTERM received");
}
} catch (error) {
console.error("Error shutting down Discord bot:", error);
}
process.exit(0);
});
2025-04-16 21:49:44 +02:00
// Catch uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('Uncaught exception:', error);
2025-04-17 14:11:43 +02:00
if (discordBot) {
2025-04-16 21:49:44 +02:00
discordBot.sendShutdownNotification('Uncaught exception', error)
.finally(() => process.exit(1));
} else {
process.exit(1);
}
});
// Start all services
startServices();