Discord 如何包含多个命令文件

Discord 如何包含多个命令文件,discord,discord.js,Discord,Discord.js,我正在使用一个包含大量命令的discord机器人,因此我想将命令分为不同的类别,例如“ModerationCommand”、“MusicCommand”等。但是,我无法导入超过1个文件夹的命令。我希望从名为“extras”的文件夹中导入命令,但我不确定如何将其添加到我的代码中。这是我从名为“commands”的文件夹导入文件的代码 for(命令文件的常量文件){ const command=require(join(uu dirname,“commands”,`${file}`)); clien

我正在使用一个包含大量命令的discord机器人,因此我想将命令分为不同的类别,例如“ModerationCommand”、“MusicCommand”等。但是,我无法导入超过1个文件夹的命令。我希望从名为“extras”的文件夹中导入命令,但我不确定如何将其添加到我的代码中。这是我从名为“commands”的文件夹导入文件的代码

for(命令文件的常量文件){
const command=require(join(uu dirname,“commands”,`${file}`));
client.commands.set(command.name,command);
}
client.on(“message”,异步(message)=>{
if(message.author.bot)返回;
如果(!message.guild)返回;
const prefixRegex=new RegExp(`^(`escapeRegex(PREFIX)})\\s*`);
if(!prefixRegex.test(message.content))返回;
const[,matchedPrefix]=message.content.match(prefixRegex);
const args=message.content.slice(matchedPrefix.length).trim().split(+/);
const commandName=args.shift().toLowerCase();
常量命令=
client.commands.get(commandName)||
client.commands.find((cmd)=>cmd.aliases&&cmd.aliases.includes(commandName));
如果(!命令)返回;

如果有人能建议一种从多个文件夹导入命令的方法,我将不胜感激

这是我发现的最好的方法,使用:

//获取命令文件夹中的所有文件夹
const commandFolders=fs.readdirSync('./commands');
用于(commandFolders的常量文件夹){
//获取每个命令文件夹中的所有文件
const commandFiles=fs.readdirSync(`./commands/${folder}`);
for(命令文件的常量文件){
const command=require(`./commands/${folder}/${file}`);
client.commands.set(command.name,command);
}
}

它抛出了一个错误:ENOTDIR不是一个目录,scandir,它是正确的目录,对吗?根据此文件在文件结构中的位置,请尝试
。/commands
也可以。一切正常,commands文件夹位于index.js文件夹中,这与我尝试添加其他命令文件夹时遇到的问题相同,也许我应该停止又来了?
for (const file of commandFiles) {
  const command = require(join(__dirname, "commands", `${file}`));
  client.commands.set(command.name, command);
}

client.on("message", async (message) => {
  if (message.author.bot) return;
  if (!message.guild) return;

  const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(PREFIX)})\\s*`);
  if (!prefixRegex.test(message.content)) return;

  const [, matchedPrefix] = message.content.match(prefixRegex);

  const args = message.content.slice(matchedPrefix.length).trim().split(/ +/);
  const commandName = args.shift().toLowerCase();

  const command =
    client.commands.get(commandName) ||
    client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes(commandName));

  if (!command) return;