Discord.js 别名

问题描述

我想在我的代码添加别名,但我不知道如何嵌入它。你有想法吗?

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);
    });
  });

解决方法

您可以将命令文件设置为在其中包含别名,如下所示:

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

然后,如果您想通过别名查找命令,可以使用:

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

您可以在此guide

中找到有关使用别名的更多信息