我在v13机器人读取Commands/Moderation类别文件夹中的命令时遇到了问题
我只想让机器人识别并打开分类文件夹中的命令,使其更有条理
命令/审核
命令/乐趣
我的代码:
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'
}发布于 2021-09-08 03:54:10
如果您指的是Commands文件夹的子文件夹中的bot read命令文件,我的方法如下
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`)
}
}https://stackoverflow.com/questions/69045633
复制相似问题