我正在尝试创建一个适度的机器人,我面临一个错误,这表明我的项目中没有命令和setWelcome文件,即使我这样做了。这是完全的错误-
node:internal/fs/utils:344
throw err;
^
Error: ENOTDIR: not a directory, scandir './commands/setWelcome.js'
at Object.readdirSync (node:fs:1390:3)
at Object.<anonymous> (/home/runner/Him/index.js:12:26)
at Module._compile (node:internal/modules/cjs/loader:1095:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
errno: -20,
syscall: 'scandir',
code: 'ENOTDIR',
path: './commands/setWelcome.js'
}
这是我在index.js中的代码-
const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
client.commands = new Discord.Collection();
const commandFolders = fs.readdirSync('./commands');
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}
client.once('ready', (c) => {
console.log(`Logged in as ${c.user.tag}`);
});
client.on('messageCreate', message => {
//My command handler code here
});
client.login(process.env.TOKEN);
而且,我的setWelcome.js文件完全是空的
这是我的项目目录-
index.js//file
commands//folder and then inside commands
setWelcome.js //file
是的,我确实有package.json和Packy-lock.json文件。我真的很感激任何帮助我的人,谢谢!顺便问一下..。我在这个项目中使用repl.it
发布于 2021-11-05 11:36:06
您似乎试图以两种相互冲突的方式遍历您的文件夹(您正在使用两个嵌套的for
循环)。如果commands
文件夹包含包含目标文件的子文件夹,这将是有意义的。但是,由于命令文件直接位于commands
文件夹中,一个循环就足够了。
readdirSync
函数需要一个指向目录的路径,并返回fs
documentation中描述的目录内容列表,但是./commands/${folder}
已经是commands
文件夹中文件的路径。以下内容应有所帮助:
\\create a list of files in command directory that end with .js
const commandFiles = fs.readdirSync(`./commands/`).filter(file => file.endsWith('.js'));
\\loop over items in the list
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
注:可能应该省略./commands/
中的尾反斜杠。如果是,请发表评论。
https://stackoverflow.com/questions/69852098
复制相似问题