This commit is contained in:
Xargana 2025-04-17 16:37:22 +03:00
parent daf79b8403
commit cf7098a94b
2 changed files with 60 additions and 26 deletions

View file

@ -23,7 +23,13 @@ class Bot {
// THIS IS IMPORTANT: Make sure CommandManager is initialized AFTER the client // THIS IS IMPORTANT: Make sure CommandManager is initialized AFTER the client
// and before using it anywhere else // and before using it anywhere else
this.commandManager = new CommandManager(this.client); try {
this.commandManager = new CommandManager(this.client);
console.log("Command Manager initialized successfully");
} catch (error) {
console.error("Failed to initialize Command Manager:", error);
this.commandManager = null; // Set to null instead of leaving undefined
}
// Authorized users for commands - Parse comma-separated list from env variable // Authorized users for commands - Parse comma-separated list from env variable
this.authorizedUserIds = process.env.AUTHORIZED_USER_IDS this.authorizedUserIds = process.env.AUTHORIZED_USER_IDS
@ -73,7 +79,12 @@ class Bot {
// Add safety check before trying to register commands // Add safety check before trying to register commands
if (this.commandManager && typeof this.commandManager.registerGlobalCommands === 'function') { if (this.commandManager && typeof this.commandManager.registerGlobalCommands === 'function') {
await this.commandManager.registerGlobalCommands(); try {
await this.commandManager.registerGlobalCommands();
console.log("Global commands registered successfully");
} catch (error) {
console.error("Error registering global commands:", error);
}
} else { } else {
console.error('Command manager not properly initialized - cannot register commands'); console.error('Command manager not properly initialized - cannot register commands');
} }
@ -165,7 +176,7 @@ class Bot {
fields: [ fields: [
{ {
name: "Bot Name", name: "Bot Name",
value: this.client.user.tag, value: this.client?.user?.tag || "Unknown (Client unavailable)",
inline: true inline: true
}, },
{ {
@ -192,30 +203,30 @@ class Bot {
}); });
} }
// Stop notification service if running // Only attempt to send notifications if client is ready
if (this.notificationService?.isRunning) { if (this.client?.isReady()) {
this.notificationService.stop(); // Notify all recipients
} for (const userId of this.notificationRecipientIds) {
try {
// Notify all recipients const user = await this.client.users.fetch(userId);
for (const userId of this.notificationRecipientIds) { await user.send({ embeds: [shutdownEmbed] });
try { console.log(`Sent shutdown notification to recipient: ${user.tag}`);
const user = await this.client.users.fetch(userId); } catch (error) {
await user.send({ embeds: [shutdownEmbed] }); console.error(`Failed to send shutdown notification to user ${userId}:`, error.message);
console.log(`Sent shutdown notification to recipient: ${user.tag}`); }
} catch (error) {
console.error(`Failed to send shutdown notification to user ${userId}:`, error.message);
} }
}
// Also notify in status channel if available // Also notify in status channel if available
if (this.notificationService?.statusChannel) { if (this.notificationService?.statusChannel) {
try { try {
await this.notificationService.statusChannel.send({ embeds: [shutdownEmbed] }); await this.notificationService.statusChannel.send({ embeds: [shutdownEmbed] });
console.log(`Sent shutdown notification to status channel: ${this.notificationService.statusChannel.name}`); console.log(`Sent shutdown notification to status channel: ${this.notificationService.statusChannel.name}`);
} catch (error) { } catch (error) {
console.error("Failed to send shutdown notification to status channel:", error.message); console.error("Failed to send shutdown notification to status channel:", error.message);
}
} }
} else {
console.log("Client not ready, cannot send shutdown notifications");
} }
} }

View file

@ -51,8 +51,31 @@ async function shutdown(signal) {
} }
// Register shutdown handlers // Register shutdown handlers
process.on('SIGINT', () => shutdown('SIGINT')); process.on('SIGINT', async () => {
process.on('SIGTERM', () => shutdown('SIGTERM')); 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);
});
// Catch uncaught exceptions // Catch uncaught exceptions
process.on('uncaughtException', (error) => { process.on('uncaughtException', (error) => {