forevergaming

Untitled

Jul 26th, 2025
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. client.on('interactionCreate', async (interaction) => {
  2.     try {
  3.         if (interaction.isCommand()) {
  4.             if (!interaction.guild || !interaction.channel) return;
  5.  
  6.             const botPermissions = interaction.channel.permissionsFor(interaction.guild.members.me);
  7.             if (!botPermissions?.has(PermissionFlagsBits.SendMessages)) return;
  8.        
  9.             const db = global.mongoClient.db('discordBot');
  10.             const settings = await db.collection('botSettings').findOne({ _id: 'botSettings' });
  11.             const bannedCollection = db.collection('commandBans');
  12.             const isMaintenance = settings?.maintenance;
  13.  
  14.             if (isMaintenance && interaction.user.id !== ownerId) {
  15.                 return interaction.reply({
  16.                     content: '🛠️ The bot is currently under maintenance. Please try again later.',
  17.                     ephemeral: true,
  18.                 });
  19.             }
  20.  
  21.             const command = client.commands.get(interaction.commandName);
  22.  
  23.             if (command) {
  24.                 let subcommand = '';
  25.  
  26.                 if (interaction.options?.getSubcommand) {
  27.                     try {
  28.                         subcommand = interaction.options.getSubcommand();
  29.                     } catch (error) {
  30.                         subcommand = '';
  31.                     }
  32.                 }
  33.                 const isBanned = await bannedCollection.findOne({
  34.                     commandName: interaction.commandName,
  35.                     serverId: interaction.guild.id
  36.                 });
  37.  
  38.                 if (isBanned) {
  39.                 const bannedEmbed = new EmbedBuilder()
  40.                     .setColor('Red')
  41.                     .setTitle('🚫 Command Banned')
  42.                     .setDescription(`The command \`/${interaction.commandName}\` is banned in this server.`)
  43.                     .setFooter({ text: 'If you think this is unfair, report it in the support server: discord.gg/JmXdVQDwt2' });
  44.  
  45.                 return interaction.reply({ embeds: [bannedEmbed], ephemeral: true });
  46.                 }
  47.  
  48.                 await command.execute(interaction, client);
  49.  
  50.                 const logChannelId = '1334497445173788702';
  51.                 const logChannel = client.channels.cache.get(logChannelId);
  52.  
  53.                 if (logChannel) {
  54.                     const commandNameToLog = subcommand
  55.                         ? `${interaction.commandName} ${subcommand}`
  56.                         : `${interaction.commandName}`;
  57.  
  58.                     const embed = new EmbedBuilder()
  59.                         .setColor('Blue')
  60.                         .setTitle('Command Executed')
  61.                         .addFields(
  62.                             { name: 'Command', value: `\`/${commandNameToLog}\`` },
  63.                             { name: 'User', value: `${interaction.user.tag} <@${interaction.user.id}>`, inline: true },
  64.                             { name: 'Server', value: `${interaction.guild?.name || 'DMs'} (${interaction.guildId || 'N/A'})`, inline: true },
  65.                             { name: 'Channel', value: `${interaction.channel?.name || 'DMs'} <#${interaction.channelId}>`, inline: true }
  66.                         )
  67.                         .setTimestamp();
  68.  
  69.                     await logChannel.send({ embeds: [embed] });
  70.                 }
  71.             } else{
  72.               try {
  73.                     const fallbackHandler = require(`./commands/slash/${interaction.commandName}.js`);
  74.                     if (fallbackHandler && fallbackHandler.execute) {
  75.                         await fallbackHandler.execute(interaction, client);
  76.                     }
  77.                 } catch (err) {
  78.                     console.error(`No handler found for /${interaction.commandName}`);
  79.                 }
  80.             }
  81.         } else if(interaction.isButton()){
  82.             const db = global.mongoClient.db('discordBot');
  83.             const settings = await db.collection('botSettings').findOne({ _id: 'botSettings' });
  84.             const isMaintenance = settings?.maintenance;
  85.  
  86.             if (isMaintenance && interaction.user.id !== ownerId) {
  87.                 return interaction.reply({
  88.                     content: '🛠️ The bot is currently under maintenance. Please try again later.',
  89.                     ephemeral: true,
  90.                 });
  91.             }            
  92.         }
  93.     } catch (error) {
  94.         console.error('Error in interactionCreate event:', error);
  95.         const errorChannel = interaction.client.channels.cache.get(ERROR_CHANNEL_ID);
  96.  
  97.         if (errorChannel) {
  98.             errorChannel.send(`⚠️ **Error in \`interactionCreate\`:**\n\`\`\`js\n${error.stack}\`\`\``);
  99.         }
  100.  
  101.         if (interaction.replied || interaction.deferred) {
  102.             await interaction.followUp({ content: 'An error occurred while processing your interaction.', ephemeral: true });
  103.         } else {
  104.             await interaction.reply({ content: 'An error occurred while processing your interaction.', ephemeral: true });
  105.         }
  106.     }
  107. });
Advertisement
Add Comment
Please, Sign In to add comment