当我想使用不和谐时,我收到此错误输出:
Error: ENOENT: no such file or directory, scandir './commands/'
←[90m at Object.readdirSync (fs.js:1021:3)←[39m
at Object.<anonymous> (C:\Users\DAA23\OneDrive\Desktop\bot\main.js:11:25)
←[90m at Module._compile (internal/modules/cjs/loader.js:1063:30)←[39m
←[90m at Object.Module._extensions..js
(internal/modules/cjs/loader.js:1092:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:928:32)←[39m
←[90m at Function.Module._load
(internal/modules/cjs/loader.js:769:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain]
(internal/modules/run_main.js:72:12)←[39m
←[90m at internal/main/run_main_module.js:17:47←[39m {
errno: ←[33m-4058←[39m,
syscall: ←[32m'scandir'←[39m,
code: ←[32m'ENOENT'←[39m,
path: ←[32m'./commands/'←[39m
}
const Discord = require('discord.js')
const client = new Discord.Client();
const prefix = '*';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file =>
file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('ur bot is on');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) ||message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'loop'){
message.channel.send('-loop');
};
});
client.login('xxxxx.xxxxxx');
所以我正在尝试创建一个不和谐的机器人,之前它说找不到discord.js,所以我做了“npm install Discord.js”并删除了不和谐的错误,但是它引入了这个错误,我不知道如何修理它。我尝试过重新安装nodejs并重新安装discord,但没有任何改变。
最佳答案
根据您给我的信息,我认为您只是缺少 commands
目录。在您的代码中,您尝试读取 commands
目录中的文件,但由于它不存在,因此会引发错误。
我假设您的项目结构如下(基于您的评论)
/
node_modules/
main.js
package.json
如果您尝试从 main.js
文件中读取名为 commands
的目录内的文件,则需要首先创建该目录。所以你的结构应该是这样的
/
node_modules/
commands/
main.js
package.json
然后要读取命令目录中的文件,您可以尝试这样做
const path = require('path');
const fs = require('fs');
const files = fs.readdirSync(path.resolve(__dirname, 'commands'));
关于javascript - 错误 : ENOENT: no such file or directory, 扫描目录 './commands/',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65858441/