首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.js我在v13机器人读取Commands/Moderation类别文件夹中的命令时遇到了问题

Discord.js我在v13机器人读取Commands/Moderation类别文件夹中的命令时遇到了问题
EN

Stack Overflow用户
提问于 2021-09-03 13:21:11
回答 1查看 208关注 0票数 0

我在v13机器人读取Commands/Moderation类别文件夹中的命令时遇到了问题

我只想让机器人识别并打开分类文件夹中的命令,使其更有条理

命令/审核

命令/乐趣

我的代码:

代码语言:javascript
复制
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
}));

错误:

代码语言:javascript
复制
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'
}
EN

回答 1

Stack Overflow用户

发布于 2021-09-08 03:54:10

如果您指的是Commands文件夹的子文件夹中的bot read命令文件,我的方法如下

代码语言:javascript
复制
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`)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69045633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档