'my command handler only recognize the top folder

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
    }
});
}


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1