【问题标题】:Discord.js I'm having problems with the v13 bot reading the commands inside Commands/Moderation category foldersDiscord.js 我在 v13 机器人读取命令/审核类别文件夹中的命令时遇到问题
【发布时间】:2021-09-08 11:54:09
【问题描述】:

v13 机器人在读取命令/审核类别文件夹中的命令时遇到问题

我只是希望机器人能够识别并打开类别文件夹中的命令,以便更有条理

命令/审核
命令/乐趣

我的代码:

const commandFiles = fs.readdirSync("./src/Commands/*/*.js")
  .filter(file => file.endsWith(".js"));

/**
 * @type {Command[]}
*/
const commands = commandFiles.map(file => require(`../Commands/*/*/${file}`));

commands.forEach(cmd => {
    console.log(`Command ${cmd.name} loaded`);
    this.commands.set(cmd.name, cmd);
});

const slashCommands = commands
  .filter(cmd => ["BOTH", "SLASH"].includes(cmd.type))
  .map(cmd => ({
      name: cmd.name.toLowerCase(),
      description: cmd.description,
      permissions: [],
      options: cmd.slashCommandOptions,
      defaultPermission: true
}));

错误:

node:internal/fs/utils:344
    throw err;
    ^

Error: ENOENT: no such file or directory, scandir './src/Commands/*/*.js'
    at Object.readdirSync (node:fs:1390:3)
    at Client.start (C:\Users\stifler\Desktop\botv13\src\Structures\Client.js:31:27)
    at Object.<anonymous> (C:\Users\stifler\Desktop\botv13\src\index.js:11:8)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -4058,
  syscall: 'scandir',
  code: 'ENOENT',
  path: './src/Commands/*/*.js'
}

【问题讨论】:

  • 你真的替换了*s吗?
  • @MrMythical 我不明白你的问题
  • 在您的文件夹名称中?
  • @MrMythical 我的文件夹名称正常。每个文件夹里面的Commands/Fun Commands/Economy都是对应的命令,但是error bot不起作用
  • 从路径中删除*,并将它们替换为您的真实目录名称。还是您只是“审查”了这个问题的路径?

标签: node.js discord discord.js


【解决方案1】:

如果您的意思是机器人读取命令文件夹中的子文件夹中的命令文件,我就是这样做的

client.commands = new Collection(); // Discord.Collection();
const commandFolders = fs.readdirSync('./commands'); // Name this as yours Command Folder name

for (const folder of commandFolders) // this loop will retrieve all subfolders
{
    const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js')); // Retrieve the cmd files inside subfolders
  
    for (const file of commandFiles) // this loop will retrieve all command files
    {
        const command = require(`./commands/${folder}/${file}`);
        client.commands.set(command.name, command) // i use client. idk if you using bot or this.
        console.log(`${command.name} is loaded`)
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2021-04-07
    • 2022-01-03
    • 2020-08-21
    • 2021-11-04
    • 1970-01-01
    • 2021-06-02
    • 2021-04-18
    • 2020-07-09
    相关资源
    最近更新 更多