简体   繁体   English

discord.js 错误:ENOENT:没有这样的文件或目录,scandir './commands'

[英]discord.js Error: ENOENT: no such file or directory, scandir './commands'

I am trying to make this work, but it keeps saying that my directory for the folder is wrong, when it is correct.我努力使这项工作,但它口口声声说我的directory文件夹是错误的,当它是正确的。 I had help from another who couldn't solve this.我从另一个无法解决这个问题的人那里得到了帮助。 Can anyone help?任何人都可以帮忙吗?

Code:代码:

const { Client, Intents, Collection } = require('discord.js')
const { REST } = require('@discordjs/rest')
const { Routes } = require('discord-api-types/v9')
const Discord = require('discord.js')

const fs = require('fs')
const intents = new Discord.Intents(32767);
const client = new Discord.Client({ intents });

const config = require('./Data/config.json')

const versionNumber = "V1.0.0"

client.once("ready", () => {
    console.log('-------------------------------------------');
    console.log(`| Successfully logged in as Logic RP#7590 |`);
    console.log('-------------------------------------------');

    const commands = []
    const commands_information = new Collection();
    const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"))

    for (const file of commandFiles) {
        const command = require(`./commands/${file}`)
        console.log(`Command loaded: ${command.data.name}`)
        commands.push(command.data.toJSON())
        commands_information.set(command.data.name, command);
    }

    const rest = new REST({ version: '9' }).setToken(config.token);

    (async () => {
        try {
            console.log('Started refreshing application (/) commands.');
            await rest.put(
                Routes.applicationGuildCommands(config.botConfiguration.client, config.botConfiguration.guild),
                { body: commands },
            );
            console.log('Successfully reloaded application (/) commands.');
        } catch (error) {
            console.error(error);
        }
    })();

    client.on('interactionCreate', async interaction => {
        if (!interaction.isCommand()) return;

        const { commandName } = interaction;

        if (!commands_information.has(commandName)) return;

        try {
            await commands_information.get(commandName).execute(client, interaction, config);
        } catch (error) {
            console.error(error);
            await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
        }
    })
})

client.on("guildMemberAdd", async (member) => {
    let members = client.guilds.cache.reduce((a, g) => a + g.memberCount, 0);
    console.log(`${member.user.tag} has joined Logic RP`);
    console.log('-------------------------------------------');
    const embed = new Discord.MessageEmbed()
        .setTitle(`Welcome to the ${member.guild.name} Discord server!`)
        .setThumbnail(member.user.displayAvatarURL({ dynamic: true }))
        .setColor('GREEN')
        .addFields(
            { 
                name: "✅ Have fun!",
                value: "The most important thing for us is that YOU have fun! You can chat with others in <#898349995315576883> and have all the fun that you want or talk to the community.", 
                inline: false 
            },
            { 
                name: "📘 Check out our rules!",
                value: "If you'd like to stay in this server, we ask that you read and follow all of our rules in <#898346754926338048>. This is important.",
                inline: false 
            },
            { 
                name: "🔔 Get your roles!", 
                value: "If you'd like to get notified for certain events or things related to this server, please check out <#898350339571462155> and get the roles that you want.", 
                inline: false 
            },
        )
        .setFooter(`Logic RP | ${versionNumber} - Member #${members}`)
        .setTimestamp()
    
    client.channels.cache.get('898346590245363772').send({embeds: [embed]});
})

client.login(config.token);

//npm run dev

Directory:目录:
目录/文件

Error:错误:

-------------------------------------------
| Successfully logged in as Logic RP#7590 |
-------------------------------------------
node:internal/fs/utils:344
    throw err;
    ^

Error: ENOENT: no such file or directory, scandir './commands'
    at Object.readdirSync (node:fs:1390:3)
    at Client.<anonymous> (C:\Users\name\Desktop\Logic RP Discord Bot\src\index.js:21:29)
    at Object.onceWrapper (node:events:510:26)
    at Client.emit (node:events:390:28)
    at WebSocketManager.triggerClientReady (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:383:17)
    at WebSocketManager.checkShardsReady (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:366:10)
    at WebSocketShard.<anonymous> (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:188:14)
    at WebSocketShard.emit (node:events:390:28)
    at WebSocketShard.checkReady (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:474:12)
    at WebSocketShard.onPacket (C:\Users\name\Desktop\Logic RP Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:446:16) {
  errno: -4058,
  syscall: 'scandir',
  code: 'ENOENT',
  path: './commands'
}

The problem here is fs takes the directory where package.json is present as its base.这里的问题是fspackage.json所在的目录作为它的基础。 and from your code fs is trying to find a folder named commands in the same level as the src folder which doesn't exist.并且从您的代码fs试图找到一个名为commands的文件夹,该文件夹与不存在的src文件夹处于同一级别。

so your corrected directory would be所以你更正的目录将是

fs.readdirSync("./src/commands");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM