{ if (err) return" /> { if (err) return" /> { if (err) return"/>

Discord.js Aliases

922 Views Asked by At

i would like to add aliases in my code but i dont know how to embed it. You have an idea ?

fs.readdir("./commands/", (err, files) => {
    if (err) return console.error(err);
    files.forEach(file => {
      if (!file.endsWith(".js")) return;
      let props = require(`./commands/${file}`);
    let commandName = file.split(".")[0];
      console.log(`Attempting to load command ${commandName}`);
      client.commands.set(commandName, props);
    });
  });
1

There are 1 best solutions below

2
Pentium1080Ti On

You can setup your command files to have aliases in them like this:

module.exports = {
    name: 'command',
    description: 'description',
    aliases: ['cmd', 'another alias'],
    execute(message, args) {
        // ...
    }
};

Then if you wanted to find a command by it's alias you can use:

client.commands.find(cmd => cmd.aliases.includes("an alias"));

You can find more about using aliases in this guide