使用FS,我试图加载一个存在的目录,但是,每当我尝试将其加载到文件夹中时,它给我一个找不到它的错误,我是否称该目录错误?
const { readdirSync } = require('fs');
module.exports = (client) => {
const load = dirs => {
const commands = readdirSync(`../Commands/${dirs}/`).filter(d => d.endsWith('.js'));
for(let file of commands) {
const pull = require(`../Commands/${dirs}/${file}`)
client.commands.set(pull.config.name, pull)
if(pull.config.aliases) pull.config.aliases.forEach(a => client.aliases.set(a, pull.config.name))
}
}
[ "Developer", "Fun", "Images", "Info", "Moderation", "Settings", "Utility" ].forEach(x => load(x))
}
目录的外观:
Scout Dev
> Core
>> Commands
>>> Developer
>>> Fun
>>> Images
>>> Info
>>> Moderation
>>> Settings
>>> Utility
>> Handlers
>>> commandHandler.js (the main code)
>> Events
错误:
Error: ENOENT: no such file or directory, scandir '../Commands/Developer/'
答案 0 :(得分:0)
在寻址命令./
中指的是同一级别的文件夹,但是在您的情况下,主代码中的命令文件夹不在同一级别,因此您应该使用:
readdirSync(__dirname + `/../Commands/${dirs})
实际上,它适用于dir
部分。
对于require part
,您必须使用一些静态路径
例如:
const path = __dirname.replace('Handlers','Commands') +`/${dirs}/${file}`;
const pull = require(path);