Advertisement
Decaded

logs

May 26th, 2024
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = {
  2.     name: 'interactionCreate',
  3.     async execute(interaction, client) {
  4.         if (!interaction.isCommand()) return;
  5.  
  6.         const command = interaction.client.commands.get(interaction.commandName);
  7.  
  8.         if (!command) return;
  9.  
  10.         const settings = client.database.get('settings');
  11.         const requiredRole = settings.requiredRole;
  12.         const logChannelId = settings.logChannel;
  13.         const userId = interaction.user.id;
  14.         const userTag = interaction.user.tag;
  15.         const commandName = interaction.commandName;
  16.         const channelName = interaction.channel.name;
  17.         const channelId = interaction.channel.id;
  18.         const timestamp = new Date().toISOString();
  19.         let status = 'Not executed';
  20.  
  21.         const logData = {
  22.             userName: userTag,
  23.             commandName: commandName,
  24.             channelName: channelName,
  25.             channelId: channelId,
  26.             timestamp: timestamp,
  27.             status: status,
  28.         };
  29.  
  30.         try {
  31.             // Check if the command is in the "admin" folder
  32.             if (command.folder === 'admin') {
  33.                 const guildOwner = interaction.guild.ownerId;
  34.  
  35.                 // Log the admin command attempt
  36.                 if (logChannelId) {
  37.                     const logChannel = await client.channels.fetch(logChannelId);
  38.                     if (logChannel) {
  39.                         const logMessage = `Admin command attempt: ${commandName} by ${userTag} in ${channelName}`;
  40.                         await logChannel.send(logMessage);
  41.                     }
  42.                 }
  43.  
  44.                 // Check permissions
  45.                 if (interaction.user.id !== guildOwner && (!requiredRole || !interaction.member.roles.cache.has(requiredRole))) {
  46.                     await interaction.reply({ content: 'You do not have permission to use this command.', ephemeral: true });
  47.                     status = 'Permission denied';
  48.                     logData.status = status;
  49.                     const commandLogs = client.database.get('commandLogs');
  50.                     if (!commandLogs[userId]) {
  51.                         commandLogs[userId] = [];
  52.                     }
  53.                     commandLogs[userId].push(logData);
  54.                     client.database.set('commandLogs', commandLogs);
  55.                     return;
  56.                 }
  57.             }
  58.  
  59.             await command.execute(interaction);
  60.             status = 'Executed';
  61.         } catch (error) {
  62.             console.error(error);
  63.             await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
  64.             status = 'Error';
  65.         } finally {
  66.             // Log the command usage in the database
  67.             logData.status = status;
  68.             const commandLogs = client.database.get('commandLogs');
  69.             if (!commandLogs[userId]) {
  70.                 commandLogs[userId] = [];
  71.             }
  72.             commandLogs[userId].push(logData);
  73.             client.database.set('commandLogs', commandLogs);
  74.         }
  75.     },
  76. };
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement