This commit is contained in:
Xargana 2025-04-17 16:30:51 +03:00
parent 94a9f8f91f
commit 1f23eaf49b
4 changed files with 642 additions and 39 deletions

View file

@ -18,11 +18,26 @@ class Bot {
partials: ['CHANNEL', 'MESSAGE']
});
// Initialize command manager
this.commandManager = new CommandManager(this.client);
// Add reference to this bot instance on the client for access from commands
this.client.bot = this;
// 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())
: [];
// Authorized user ID - CHANGE THIS to your Discord user ID
this.authorizedUserId = process.env.AUTHORIZED_USER_ID;
// 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}`);
// Setup temp directory
this.setupTempDirectory();
@ -120,14 +135,15 @@ class Bot {
}
};
// 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");
// 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);
}
}
// Also notify in status channel if configured
@ -182,13 +198,15 @@ class Bot {
this.notificationService.stop();
}
// 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);
// 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);
}
}
// Also notify in status channel if available

View file

@ -48,26 +48,35 @@ class CommandManager {
async registerGlobalCommands() {
try {
await this.loadCommands();
console.log("Registering global commands...");
if (this.commands.size === 0) {
console.log("No commands to register.");
return;
}
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;
});
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(
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
await rest.put(
Routes.applicationCommands(this.client.user.id),
{ body: commandsData },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
console.log(`Successfully registered ${commandsData.length} global commands`);
} catch (error) {
console.error(error);
console.error('Error registering global commands:', error);
}
}
@ -102,6 +111,22 @@ 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,7 +3,28 @@ const axios = require('axios');
class NotificationService {
constructor(client, options = {}) {
this.client = client;
this.authorizedUserId = 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()) :
[];
// 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.statusChannel = null;
this.checkInterval = options.checkInterval || 10000; // Changed to 10 seconds default
this.statusEndpoint = options.statusEndpoint || 'https://blahaj.tr:2589/status';
@ -263,14 +284,14 @@ class NotificationService {
}
}
// Send to owner
if (this.authorizedUserId) {
// Send to all notification recipients
for (const userId of this.notificationRecipientIds) {
try {
const owner = await this.client.users.fetch(this.authorizedUserId);
await owner.send({ embeds: [embed] });
console.log('Status change notification sent to owner');
const user = await this.client.users.fetch(userId);
await user.send({ embeds: [embed] });
console.log(`Status change notification sent to recipient: ${user.tag}`);
} catch (error) {
console.error(`Failed to send status notification to owner: ${error.message}`);
console.error(`Failed to send status notification to user ${userId}: ${error.message}`);
}
}
}