client.on('interactionCreate', async (interaction) => { try { if (interaction.isCommand()) { if (!interaction.guild || !interaction.channel) return; const botPermissions = interaction.channel.permissionsFor(interaction.guild.members.me); if (!botPermissions?.has(PermissionFlagsBits.SendMessages)) return; const db = global.mongoClient.db('discordBot'); const settings = await db.collection('botSettings').findOne({ _id: 'botSettings' }); const bannedCollection = db.collection('commandBans'); const isMaintenance = settings?.maintenance; if (isMaintenance && interaction.user.id !== ownerId) { return interaction.reply({ content: '🛠️ The bot is currently under maintenance. Please try again later.', ephemeral: true, }); } const command = client.commands.get(interaction.commandName); if (command) { let subcommand = ''; if (interaction.options?.getSubcommand) { try { subcommand = interaction.options.getSubcommand(); } catch (error) { subcommand = ''; } } const isBanned = await bannedCollection.findOne({ commandName: interaction.commandName, serverId: interaction.guild.id }); if (isBanned) { const bannedEmbed = new EmbedBuilder() .setColor('Red') .setTitle('🚫 Command Banned') .setDescription(`The command \`/${interaction.commandName}\` is banned in this server.`) .setFooter({ text: 'If you think this is unfair, report it in the support server: discord.gg/JmXdVQDwt2' }); return interaction.reply({ embeds: [bannedEmbed], ephemeral: true }); } await command.execute(interaction, client); const logChannelId = '1334497445173788702'; const logChannel = client.channels.cache.get(logChannelId); if (logChannel) { const commandNameToLog = subcommand ? `${interaction.commandName} ${subcommand}` : `${interaction.commandName}`; const embed = new EmbedBuilder() .setColor('Blue') .setTitle('Command Executed') .addFields( { name: 'Command', value: `\`/${commandNameToLog}\`` }, { name: 'User', value: `${interaction.user.tag} <@${interaction.user.id}>`, inline: true }, { name: 'Server', value: `${interaction.guild?.name || 'DMs'} (${interaction.guildId || 'N/A'})`, inline: true }, { name: 'Channel', value: `${interaction.channel?.name || 'DMs'} <#${interaction.channelId}>`, inline: true } ) .setTimestamp(); await logChannel.send({ embeds: [embed] }); } } else{ try { const fallbackHandler = require(`./commands/slash/${interaction.commandName}.js`); if (fallbackHandler && fallbackHandler.execute) { await fallbackHandler.execute(interaction, client); } } catch (err) { console.error(`No handler found for /${interaction.commandName}`); } } } else if(interaction.isButton()){ const db = global.mongoClient.db('discordBot'); const settings = await db.collection('botSettings').findOne({ _id: 'botSettings' }); const isMaintenance = settings?.maintenance; if (isMaintenance && interaction.user.id !== ownerId) { return interaction.reply({ content: '🛠️ The bot is currently under maintenance. Please try again later.', ephemeral: true, }); } } } catch (error) { console.error('Error in interactionCreate event:', error); const errorChannel = interaction.client.channels.cache.get(ERROR_CHANNEL_ID); if (errorChannel) { errorChannel.send(`⚠️ **Error in \`interactionCreate\`:**\n\`\`\`js\n${error.stack}\`\`\``); } if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: 'An error occurred while processing your interaction.', ephemeral: true }); } else { await interaction.reply({ content: 'An error occurred while processing your interaction.', ephemeral: true }); } } });