Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

node.js - Discord.js Aliases

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);
    });
  });
question from:https://stackoverflow.com/questions/65909940/discord-js-aliases

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...