繁体   English   中英

Discord.js 错误:EISDIR:对目录的非法操作,请阅读

[英]Discord.js Error: EISDIR: illegal operation on a directory, read

当我尝试执行此代码时,出现错误。 “错误:EISDIR:对目录的非法操作,读取”。

第 18 行:第 19 列

const { Client, Intents, Collection } = require('discord.js')

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

const fs = require('fs')

const bot = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES ] })

bot.commands = new Collection()

var cmdFiles = fs.readFileSync('./cmd').filter(f => f.endsWith(".js"))

for(const f in cmdFiles) {
    const cmd = require(`./commands/${f}`)

    bot.commands.set(cmd.help.name, cmd)
}

bot.once("ready", () => {
    console.log('Bot is ready!')
})

bot.on("messageCreate", async message => {
    if(message.author.bot) return;

    var prefix = config.prefix

    if(!message.content.startsWith(prefix)) return;

    var array = message.content.split(" ");

    var command = array[0];

    const data = bot.commands.get(command.slice(prefix.length))

    if(!data) return;

    var args = array.slice(1)

    try {
        await data.run(bot, message, args)
    } catch(e) {
        await message.channel.send(e)
        await console.log(e)
    }
})

bot.login(config.token)

是的,所有配置内容都已定义。 我已经尝试搜索此错误,但没有得到任何我需要的东西。

我想要做的是从数组列表中的目录'cmd'中加载每个文件,并在调用时运行命令。

改变这个:

var cmdFiles = fs.readFileSync('./cmd').filter(f => f.endsWith(".js"));

对此:

var cmdFiles = fs.readdirSync('./cmd').filter(f => f.endsWith(".js"));

正如您的问题所述, ./cmd是一个目录,您无法使用fs.readFileSync()列出目录中的文件。 您将使用fs.readdirSync()来做到这一点。

fs.readFileSync()尝试将目录作为文件打开并读取其内容。 由于它不是文件,因此您会收到 EISDIR 错误。

暂无
暂无

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

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