When i try executing this code i get an error. "Error: EISDIR: illegal operation on a directory, read".
Line 18 : Column 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)
Yes all config things are defined. I've tried searching for this error but got nothing that i need.
What i want to do is load every file from the directory 'cmd' in a array list and run a command if it is called.
CodePudding user response:
Change this:
var cmdFiles = fs.readFileSync('./cmd').filter(f => f.endsWith(".js"));
to this:
var cmdFiles = fs.readdirSync('./cmd').filter(f => f.endsWith(".js"));
As your question states, ./cmd
is a directory and you can't list the files in a directory with fs.readFileSync()
. You would use fs.readdirSync()
to do that.
fs.readFileSync()
tries to open the directory as a file and read its contents. Since it's not a file, you get the EISDIR error.