[英]Error: ENOENT: no such file or directory, scandir './commands/'
i am getting this error output, when i want to use discord:当我想使用 discord 时,我收到此错误 output:
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');
So I am trying to create a discord bot and previously it said cannot find discord.js so I did 'npm install discord.js' and that removed the discord error however it introduced this error and I don't know how to fix it.所以我正在尝试创建一个 discord 机器人,之前它说找不到 discord.js 所以我做了“npm install discord.js”并删除了 discord 错误,但它知道如何修复这个错误。 I have tried reinstalling nodejs and also reinstalling discord but nothing has changed.
我尝试重新安装 nodejs 并重新安装 discord 但没有任何改变。
Based on the information you gave me I think your just missing the commands
directory.根据您给我的信息,我认为您只是缺少
commands
目录。 In your code you are trying to read files in the commands
directory, but since it does not exist it throws the error.在您的代码中,您试图读取
commands
目录中的文件,但由于它不存在,因此会引发错误。
I assume your project structure looks like this (bases on your comment)我假设您的项目结构如下所示(基于您的评论)
/
node_modules/
main.js
package.json
If you are trying to read the files inside a directory called commands
from within the main.js
file, you need to actually create the directory first.如果您尝试从
main.js
文件中读取名为commands
的目录中的文件,则需要先实际创建该目录。 So your structure should look like this所以你的结构应该是这样的
/
node_modules/
commands/
main.js
package.json
Then to read the files inside the commands directory you can try this然后读取命令目录中的文件,你可以试试这个
const path = require('path');
const fs = require('fs');
const files = fs.readdirSync(path.resolve(__dirname, 'commands'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.