當我嘗試執行此代碼時,出現錯誤。“錯誤: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”中加載每個檔案,并在呼叫時運行命令。
uj5u.com熱心網友回復:
改變這個:
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 錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365887.html
標籤:javascript 节点.js 不和谐 不和谐.js
