错误:ENOENT:没有这样的文件或目录,扫描的"./命令/"



当我想使用discord时,我得到这个错误输出:

Error: ENOENT: no such file or directory, scandir './commands/'
←[90m    at Object.readdirSync (fs.js:1021:3)←[39m
at Object.<anonymous> (C:UsersDAA23OneDriveDesktopbotmain.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');

所以我试图创建一个不和谐机器人,以前它说找不到不和谐。js所以我做了'npm安装不和谐。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'));

最新更新