This commit is contained in:
Xargana 2025-04-17 17:00:11 +03:00
parent c2fa7367f9
commit ff251c239f
5 changed files with 70 additions and 709 deletions

View file

@ -18,36 +18,11 @@ class Bot {
partials: ['CHANNEL', 'MESSAGE']
});
// Add reference to this bot instance on the client for access from commands
this.client.bot = this;
// THIS IS IMPORTANT: Make sure CommandManager is initialized AFTER the client
// and before using it anywhere else
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
}
// Initialize command manager
this.commandManager = new CommandManager(this.client);
// Authorized users for commands - Parse comma-separated list from env variable
this.authorizedUserIds = process.env.AUTHORIZED_USER_IDS
? process.env.AUTHORIZED_USER_IDS.split(',').map(id => id.trim())
: [];
// For backward compatibility, add the old env var if it exists
if (process.env.AUTHORIZED_USER_ID && !this.authorizedUserIds.includes(process.env.AUTHORIZED_USER_ID)) {
this.authorizedUserIds.push(process.env.AUTHORIZED_USER_ID);
}
// Parse notification recipient IDs (separate from command authorization)
this.notificationRecipientIds = process.env.NOTIFICATION_USER_IDS ?
process.env.NOTIFICATION_USER_IDS.split(',').map(id => id.trim()) :
this.authorizedUserIds; // Default to authorized users if not specified
console.log(`Authorized users configured: ${this.authorizedUserIds.length}`);
console.log(`Notification recipients configured: ${this.notificationRecipientIds.length}`);
// Authorized user ID - CHANGE THIS to your Discord user ID
this.authorizedUserId = process.env.AUTHORIZED_USER_ID;
// Setup temp directory
this.setupTempDirectory();
@ -77,17 +52,17 @@ class Bot {
this.client.once("ready", async () => {
console.log(`Logged in as ${this.client.user.tag}`);
// Add safety check before trying to register commands
if (this.commandManager && typeof this.commandManager.registerGlobalCommands === 'function') {
try {
await this.commandManager.registerGlobalCommands();
console.log("Global commands registered successfully");
} catch (error) {
console.error("Error registering global commands:", error);
}
} else {
console.error('Command manager not properly initialized - cannot register commands');
}
// Only register global commands for direct messages
await this.commandManager.registerGlobalCommands();
// Initialize and start the notification service
this.notificationService = new NotificationService(this.client, {
checkInterval: process.env.STATUS_CHECK_INTERVAL ? parseInt(process.env.STATUS_CHECK_INTERVAL) : 5000,
statusEndpoint: process.env.STATUS_ENDPOINT || 'https://blahaj.tr:2589/status'
});
await this.notificationService.initialize();
this.notificationService.start();
// Send startup notification
await this.sendStartupNotification();
@ -96,7 +71,7 @@ class Bot {
// Interaction event
this.client.on("interactionCreate", async (interaction) => {
// Only process commands if the user is authorized
if (!this.authorizedUserIds.includes(interaction.user.id)) {
if (interaction.user.id !== this.authorizedUserId) {
console.log(`Unauthorized access attempt by ${interaction.user.tag} (${interaction.user.id})`);
await interaction.reply({
content: "You are not authorized to use this bot.",
@ -145,15 +120,14 @@ class Bot {
}
};
// Notify all recipients
for (const userId of this.notificationRecipientIds) {
try {
const user = await this.client.users.fetch(userId);
await user.send({ embeds: [startupEmbed] });
console.log(`Sent startup notification to recipient: ${user.tag}`);
} catch (error) {
console.error(`Failed to send startup notification to user ${userId}:`, error.message);
}
// Only notify the authorized user
try {
const owner = await this.client.users.fetch(this.authorizedUserId);
await owner.send({ embeds: [startupEmbed] });
console.log(`Sent startup notification to authorized user: ${owner.tag}`);
} catch (error) {
console.error("Failed to send startup notification to authorized user:", error.message);
console.log("This is not critical - the bot will still function normally");
}
// Also notify in status channel if configured
@ -170,18 +144,18 @@ class Bot {
async sendShutdownNotification(reason = "Manual shutdown", error = null) {
// Create shutdown embed
const shutdownEmbed = {
title: "Bot Shutdown Notification",
title: "blahaj.tr bot status update",
description: `Bot is shutting down at <t:${Math.floor(Date.now() / 1000)}:F>`,
color: 0xFF0000,
fields: [
{
name: "Bot Name",
value: this.client?.user?.tag || "Unknown (Client unavailable)",
value: this.client.user.tag,
inline: true
},
{
name: "Shutdown Reason",
value: reason || "Unknown reason",
value: reason || "Unknown",
inline: true
},
{
@ -203,30 +177,28 @@ class Bot {
});
}
// Only attempt to send notifications if client is ready
if (this.client?.isReady()) {
// Notify all recipients
for (const userId of this.notificationRecipientIds) {
try {
const user = await this.client.users.fetch(userId);
await user.send({ embeds: [shutdownEmbed] });
console.log(`Sent shutdown notification to recipient: ${user.tag}`);
} catch (error) {
console.error(`Failed to send shutdown notification to user ${userId}:`, error.message);
}
}
// Stop notification service if running
if (this.notificationService?.isRunning) {
this.notificationService.stop();
}
// Also notify in status channel if available
if (this.notificationService?.statusChannel) {
try {
await this.notificationService.statusChannel.send({ embeds: [shutdownEmbed] });
console.log(`Sent shutdown notification to status channel: ${this.notificationService.statusChannel.name}`);
} catch (error) {
console.error("Failed to send shutdown notification to status channel:", error.message);
}
// Notify authorized user
try {
const owner = await this.client.users.fetch(this.authorizedUserId);
await owner.send({ embeds: [shutdownEmbed] });
console.log(`Sent shutdown notification to authorized user: ${owner.tag}`);
} catch (error) {
console.error("Failed to send shutdown notification to authorized user:", error.message);
}
// Also notify in status channel if available
if (this.notificationService?.statusChannel) {
try {
await this.notificationService.statusChannel.send({ embeds: [shutdownEmbed] });
console.log(`Sent shutdown notification to status channel: ${this.notificationService.statusChannel.name}`);
} catch (error) {
console.error("Failed to send shutdown notification to status channel:", error.message);
}
} else {
console.log("Client not ready, cannot send shutdown notifications");
}
}

View file

@ -1,4 +1,4 @@
const { Collection, REST, Routes, SlashCommandBuilder } = require('discord.js');
const { Collection, REST, Routes } = require('discord.js');
const fs = require('fs');
const path = require('path');
@ -9,9 +9,6 @@ class CommandManager {
this.commandFolders = ['info', 'system']; // Only include info and system commands
this.rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
this.authorizedUserId = process.env.AUTHORIZED_USER_ID;
// Add this line to load commands when the CommandManager is created
this.loadCommands();
}
async loadCommands() {
@ -51,35 +48,26 @@ class CommandManager {
async registerGlobalCommands() {
try {
console.log("Registering global commands...");
await this.loadCommands();
const commandsData = this.commands.map(command => {
const data = {
name: command.name,
description: command.description,
options: command.options || [],
// Add these lines for global availability in all contexts
integration_types: [1], // Add integration type for global availability
contexts: [0, 1, 2], // Available in all contexts (DM, GROUP_DM, GUILD)
};
// If the command has an addOptions method, call it
if (typeof command.addOptions === 'function') {
data.options = command.addOptions(new SlashCommandBuilder()).options;
}
return data;
});
if (this.commands.size === 0) {
console.log("No commands to register.");
return;
}
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
await rest.put(
const commandsData = this.commands.map(command => command.toJSON());
console.log(`Started refreshing ${commandsData.length} application (/) commands.`);
// Register as global commands for DMs
const data = await this.rest.put(
Routes.applicationCommands(this.client.user.id),
{ body: commandsData },
);
console.log(`Successfully registered ${commandsData.length} global commands`);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error('Error registering global commands:', error);
console.error(error);
}
}
@ -114,22 +102,6 @@ class CommandManager {
}
}
}
async handleAutocomplete(interaction) {
const command = this.commands.get(interaction.commandName);
if (!command || typeof command.handleAutocomplete !== 'function') {
return;
}
try {
await command.handleAutocomplete(interaction);
} catch (error) {
console.error(`Error handling autocomplete for ${interaction.commandName}:`, error);
// Respond with empty array as fallback
await interaction.respond([]);
}
}
}
module.exports = CommandManager;

View file

@ -3,28 +3,7 @@ const axios = require('axios');
class NotificationService {
constructor(client, options = {}) {
this.client = client;
// Parse notification recipient IDs (separate from command authorization)
this.notificationRecipientIds = process.env.NOTIFICATION_USER_IDS ?
process.env.NOTIFICATION_USER_IDS.split(',').map(id => id.trim()) :
[];
// For backward compatibility - if no notification IDs specified, use authorized IDs
if (this.notificationRecipientIds.length === 0) {
// Use the bot's authorizedUserIds as fallback
this.notificationRecipientIds = client.bot.authorizedUserIds ||
(process.env.AUTHORIZED_USER_IDS ?
process.env.AUTHORIZED_USER_IDS.split(',').map(id => id.trim()) :
[]);
// Add legacy single user ID for backward compatibility
if (process.env.AUTHORIZED_USER_ID && !this.notificationRecipientIds.includes(process.env.AUTHORIZED_USER_ID)) {
this.notificationRecipientIds.push(process.env.AUTHORIZED_USER_ID);
}
}
console.log(`Notification recipients configured: ${this.notificationRecipientIds.length}`);
this.authorizedUserId = process.env.AUTHORIZED_USER_ID;
this.statusChannel = null;
this.checkInterval = options.checkInterval || 10000; // Changed to 10 seconds default
this.statusEndpoint = options.statusEndpoint || 'https://blahaj.tr:2589/status';
@ -284,14 +263,14 @@ class NotificationService {
}
}
// Send to all notification recipients
for (const userId of this.notificationRecipientIds) {
// Send to owner
if (this.authorizedUserId) {
try {
const user = await this.client.users.fetch(userId);
await user.send({ embeds: [embed] });
console.log(`Status change notification sent to recipient: ${user.tag}`);
const owner = await this.client.users.fetch(this.authorizedUserId);
await owner.send({ embeds: [embed] });
console.log('Status change notification sent to owner');
} catch (error) {
console.error(`Failed to send status notification to user ${userId}: ${error.message}`);
console.error(`Failed to send status notification to owner: ${error.message}`);
}
}
}