Error: ENOENT: no such file or directory, scandir './commands/'

im not experienced at coding but i’m trying to make a command handler file for my discord bot but this message shows up

my code:

const Discord = require(‘discord.js’);
const bot = new Discord.Client();
const token = “my token”;
const PREFIX = “.”;

const fs = require(‘fs’);
bot.commands = new Discord.Collection();

const commandFiles = fs.readdirSync(’./commands/’).filter(file => file.endsWith(’.js’));
for(const file of commandFiles){
const command = require(./commands/${file});

bot.commands.set(command.name, command);

}

bot.on(‘ready’, () => {
console.log(“The bot is active and ready to go!”);
});

bot.on(‘message’, message => {
let args = message.content.substring(PREFIX.length).split(" ");

switch (args[0]) {

    case "greet":
        bot.commands.get('greet').execute(message, args);
    break;

    case "hello":
        bot.commands.get('draft').execute(message, args);
    break;
}

});

bot.login(token);

don’t know how to fix it.

Please avoid swearing on the forum, or replying to your own post with non-constructive comments.

const Discord = require(‘discord.js’);
const bot = new Discord.Client();
const token = “my token”;
const PREFIX = “.”;

const fs = require(‘fs’);
bot.commands = new Discord.Collection();

const commandFiles = fs.readdirSync(’. / commands / ’).filter(file => file.endsWith(’.js’));
for (const file of commandFiles) {
    const command = require(. / commands / $ {
        file
    });

    bot.commands.set(command.name, command);
}

bot.on(‘ready’, () => {
    console.log(“The bot is active and ready to go!”);
});

bot.on(‘message’, message => {
    let args = message.content.substring(PREFIX.length).split(" ");

    switch (args[0]) {

        case "greet":
            bot.commands.get('greet').execute(message, args);
            break;

        case "hello":
            bot.commands.get('draft').execute(message, args);
            break;
    }
});

bot.login(token);

Some stuff I noticed:

  1. You are using unicode curly quotation marks. Use "" or '' instead.
  2. for (const file of commandFiles) {... - file might be being mutated, so you can try using let file instead?

Furthermore, I don’t know what errors you are having. Please post a screenshot or copy/paste it into a comment!

3 Likes

sorry for swearing, won’t happen again. also, i apologize for the late response.

here’s a screenshot of my code:

don’t understand what you’re trying to tell me when you say

  1. for (const file of commandFiles) {... - file might be being mutated, so you can try using let file instead?

Oh I just meant for you to try using let file instead of const file. Could you send screenshots of the errors you are getting?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.