[英]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.