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

221
Views
command handler only recognize the top-most folder instead of all the folders

i have a problem, my Command Handler only recognize the top Folder inside my Commands Directory. Its supposed to show all of the available Folder in Commands Directory but it only showed the 'test' category which is the top one. any help would be really appreciated.

Folder/Directory Construction:

My Folder/Directory Construction

console.log output:

console.log Output

Command Handler Code:

const {readdirSync} = require('fs');
const ascii = require('ascii-table');
let table = new ascii("Commands");
table.setHeading('Category', 'Command', ' Load status');
var logged = false;
const path = require('node:path')

module.exports = (client) => {
readdirSync('./Commands/').forEach(dir => {
    var commands = readdirSync(`./Commands/${dir}/`).filter(file => file.endsWith('.js'));
    for(let file of commands){
        let pull = require(`../Commands/${dir}/${file}`);
        if(pull.name){
            client.commands.set(pull.name, pull);
            table.addRow(dir,file,'✔️   -> Command Loaded')
        } else {
            table.addRow(dir,file,'❌   -> Command Error')
            continue;
        }
        if(pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name))       
    }
    if(!logged) {
        console.log(table.toString())
        console.log(`[Command] Command Handler is Ready! | Total Commands: ${commands.length}`)
        logged = true
    }
});
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe you are overwriting the commands variable after each folder has been looped through. Try this:

const {readdirSync} = require('fs');
const ascii = require('ascii-table');
let table = new ascii("Commands");
table.setHeading('Category', 'Command', ' Load status');
var logged = false;
const path = require('node:path')

module.exports = (client) => {
readdirSync('./Commands/').forEach(dir => {

    var commands = []
    commands.push(readdirSync(`./Commands/${dir}/`).filter(file => file.endsWith('.js')));

    for(let file of commands){
        let pull = require(`../Commands/${dir}/${file}`);
        if(pull.name){
            client.commands.set(pull.name, pull);
            table.addRow(dir,file,'✔️   -> Command Loaded')
        } else {
            table.addRow(dir,file,'❌   -> Command Error')
            continue;
        }
        if(pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name))       
    }
    if(!logged) {
        console.log(table.toString())
        console.log(`[Command] Command Handler is Ready! | Total Commands: ${commands.length}`)
        logged = true
    }
});
}

If this doesn't help than it might still be that issue I referred to above but the edits I made might not be compatible with your code.

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!