Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Unable to get a subfolder in directory

Hi I'm trying to load files within different folders for example

-- Main directory
  -- bot folder
    - index.js
    - package.json
    - package-lock.json
    -- commands
        -- fun
            - kick.js
            - ban.js
        -- some other category

However in my code I'm only seem to be able to load the .js files in the Commands folder rather than the sub folders.

Here is the code I'm working with:

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

    const commands = commandFiles.map(file => require(`../Commands/${file}`));

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

Help appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create multiple collections, one for each of your subfolders. Here is what I've done for my bot, and it's perfectly working :

client.commandsAdmin = new Discord.Collection();
client.commandsUser = new Discord.Collection();

const commandsAdminFiles = fs.readdirSync('./commands/admin').filter(file => file.endsWith('.js'));
const commandsUserFiles = fs.readdirSync('./commands/user').filter(file => file.endsWith('.js'));


for(const file of commandsAdminFiles){
  const command = require(`./commands/admin/${file}`);
  client.commandsAdmin.set(command.name, command);
}

for(const file of commandsUserFiles){
  const command = require(`./commands/user/${file}`);
  client.commandsUser.set(command.name, command);
}

When processing the command in your code, you would have to check if it belongs to one of the collections, then get which collection does contain the command, and then use client.commandsFun.get(command).execute(...) for example, if the command belong to the 'fun' subfolder.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!