StackCode
StackCode

Reputation: 1

Discord.js I'm having problems with the v13 bot reading the commands inside Commands/Moderation category folders

I'm having problems with the v13 bot reading the commands inside Commands/Moderation category folders

I just want the bot to recognize and turn on commands in category folders to be more organized

Commands/Moderation
Commands/Fun

my code:

const commandFiles = fs.readdirSync("./src/Commands/*/*.js")
  .filter(file => file.endsWith(".js"));

/**
 * @type {Command[]}
*/
const commands = commandFiles.map(file => require(`../Commands/*/*/${file}`));

commands.forEach(cmd => {
    console.log(`Command ${cmd.name} loaded`);
    this.commands.set(cmd.name, cmd);
});

const slashCommands = commands
  .filter(cmd => ["BOTH", "SLASH"].includes(cmd.type))
  .map(cmd => ({
      name: cmd.name.toLowerCase(),
      description: cmd.description,
      permissions: [],
      options: cmd.slashCommandOptions,
      defaultPermission: true
}));

Error:

node:internal/fs/utils:344
    throw err;
    ^

Error: ENOENT: no such file or directory, scandir './src/Commands/*/*.js'
    at Object.readdirSync (node:fs:1390:3)
    at Client.start (C:\Users\stifler\Desktop\botv13\src\Structures\Client.js:31:27)
    at Object.<anonymous> (C:\Users\stifler\Desktop\botv13\src\index.js:11:8)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -4058,
  syscall: 'scandir',
  code: 'ENOENT',
  path: './src/Commands/*/*.js'
}

Upvotes: 0

Views: 282

Answers (1)

Alixy Mizuki
Alixy Mizuki

Reputation: 91

If you mean the bot read command file inside the Subfolder of Commands folder, here's how i do it

client.commands = new Collection(); // Discord.Collection();
const commandFolders = fs.readdirSync('./commands'); // Name this as yours Command Folder name

for (const folder of commandFolders) // this loop will retrieve all subfolders
{
    const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js')); // Retrieve the cmd files inside subfolders
  
    for (const file of commandFiles) // this loop will retrieve all command files
    {
        const command = require(`./commands/${folder}/${file}`);
        client.commands.set(command.name, command) // i use client. idk if you using bot or this.
        console.log(`${command.name} is loaded`)
    }
}

Upvotes: 1

Related Questions