Built-in interactionCreate Event

This event handles the creation execution of the slash commands, using the built-in slash command executing from the slash properties of a command file and also using the built-in client.commands Collection object. We will also display the slash command Id, incase you decide to delete it a server for some reason.

module.exports = {
    name: 'interactionCreate',  //discord.js event name
    async execute(interaction, client) { // execution of slash command

    //gets the user permissions level
    const level = client.getPermissionsLevel({
      author: interaction.user,
      channel: interaction.channel,
      guild: interaction.guild,
      guildMember: interaction.member,
    });

        // check if its a command then proceed
        if (!interaction.isChatInputCommand()) return;  
        // destructure and get the command name and id
        const { commandName, commandId  } = interaction;
        // get the command file
        const cmd = client.commands.get(commandName);
        
        // log who call the slash command and the id (id is use for deletion)        
        console.log(`${commandName}`, `${interaction.user.tag}`, `SLASH CMD`);
        console.log(commandId, 'ID', 'SLASH CMD');

        // execute the slash command and response of the slash command
      try {
        return cmd.interactionReply(interaction, client, level);
      } catch (e) {
        console.log("interaction execution failed", e);
      }
```
    },
};

Last updated