Home > Back-end >  Command.execute issue
Command.execute issue

Time:06-29

I will try to explain this the best I can, however, it is a little confusing. Here is my code for executing a slash command.

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction, Servers, Tickets, client);
    } catch (error) {
        console.error(error);
        await interaction.reply({ embeds: [errorEmbed], ephemeral: true });
    }
});

If I put client right before Servers, it allows me to use it in other commands, however, if I put it later such as after Tickets, it doesn't work. This also happens when I move Tickets or Servers to the end I can no longer use those in the commands. Any ideas on why this could be?

CodePudding user response:

If you run command.execute(interaction, Servers, Tickets, client), the order of the arguments must be the same in every command file. Even if you don't need the variables Servers or Tickets, if you want to use the client, it's the fourth argument.

It would be better to pass a single object:

command.execute({ client, interaction, Servers, Tickets })

And in your command files, destructure the object to pick the ones you need:

async execute({ client, interaction }) {
  // ...
async execute({ Servers }) {
  // ...

This way the order no longer matters, but you'll still need to update every command file with those curly brackets.

  • Related