passing 2 directories in 1 fs.readdirSync()

188 Views Asked by At

I've been on this problem for a while and I can't figure it out, I am trying to make my readdirSync to recognize the SlashCommands, MessageCommmands directory but I can't figure it out.

  • File Structure
Commands
┣ MessageCommands
┃ ┣ Economy
┃ ┃ ┣ balance.js
┃ ┃ ┣ login.js
┃ ┃ ┣ recoveraccount.js
┃ ┃ ┣ signout.js
┃ ┃ ┗ signup.js
┃ ┗ Utility
┃ ┃ ┣ ping.js
┃ ┃ ┗ setprefix.js
┗ SlashCommands
┃ ┣ Info
┃ ┃ ┗ info.js
┃ ┗ Test
┃ ┃ ┗ test.js
  • What I WAS Getting
.------------------------------------------------------.
|                       Commands                       |
|------------------------------------------------------|
| Category |      Command      |      Load status      |
|----------|-------------------|-----------------------|
| Economy  | balance.js        | ✔️  -> Command Loaded |
| Economy  | login.js          | ✔️  -> Command Loaded |
| Economy  | recoveraccount.js | ✔️  -> Command Loaded |
| Economy  | signout.js        | ✔️  -> Command Loaded |
| Economy  | signup.js         | ✔️  -> Command Loaded |
| Test     | guildname.js      | ✔️  -> Command Loaded |
| Test     | howlong.js        | ✔️  -> Command Loaded |
| Utility  | ping.js           | ✔️  -> Command Loaded |
| Utility  | setprefix.js      | ✔️  -> Command Loaded |
'------------------------------------------------------'
  • Code
const { readdirSync } = require('fs');
const ascii = require('ascii-table');
let table = new ascii("Commands");
table.setHeading('Command Type', 'Category', 'Command', ' Load status');

module.exports = (client) => {
    readdirSync('./Commands/').forEach(type, dir => {
        let commands = readdirSync(`./Commands/${type}/${dir}/`).filter(file => file.endsWith('.js'));
        for (let file of commands) {
            let pull = require(`../Commands/${type}/${dir}/${file}`);
            if (pull.name) {
                client.commands.set(pull.name, pull);
                table.addRow(type, dir, file, '✔️  -> Command Loaded')
            } else {
                table.addRow(type, dir, file, '❌  -> Command Error')
                continue;
            } if (pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name))
        }
    });
    console.log(table.toString());
    console.log(`[Command] Command Handler is Ready! | Total Commands: ${client.commands.size}`)
}
  • What I'M Expecting
.------------------------------------------------------------------------.
|                               Commands                                 |
|------------------------------------------------------------------------|
|   Command Type  | Category |      Command      |      Load status      |
|-----------------|----------|-------------------|-----------------------|
| MessageCommands | Economy  | balance.js        | ✔️  -> Command Loaded |
| MessageCommands | Economy  | login.js          | ✔️  -> Command Loaded |
| MessageCommands | Economy  | recoveraccount.js | ✔️  -> Command Loaded |
| MessageCommands | Economy  | signout.js        | ✔️  -> Command Loaded |
| MessageCommands | Economy  | signup.js         | ✔️  -> Command Loaded |
| MessageCommands | Utility  | ping.js           | ✔️  -> Command Loaded |
| MessageCommands | Utility  | setprefix.js      | ✔️  -> Command Loaded |
|  SlashCommands  |  Info    | info.js           | ✔️  -> Command Loaded |
|  SlashCommands  |  Test    | test.js           | ✔️  -> Command Loaded |
'------------------------------------------------------------------------'
1

There are 1 best solutions below

2
Heretic Monkey On

Here's some code that should work, but I didn't feel like creating the directory structure to validate it, so do try it out in your environment.

const {
  readdirSync
} = require('fs');

const table = [];

module.exports = (client) => {
  readdirSync('./Commands/').forEach(type, dir => {
    let commands = readdirSync(`./Commands/${type}/${dir}/`).filter(file => file.endsWith('.js'));
    for (let file of commands) {
      let pull = require(`../Commands/${type}/${dir}/${file}`);
      if (pull.name) {
        client.commands.set(pull.name, pull);
        table.push({
          'Command Type': type,
          Category: dir,
          Command: file,
          'Load status': '✔️  -> Command Loaded'
        });
      } else {
        table.push({
          'Command Type': type,
          Category: dir,
          Command: file,
          'Load status': '❌  -> Command Error'
        });
        continue;
      }
      if (pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name))
    }
  });
  console.table(table);
  console.log(`[Command] Command Handler is Ready! | Total Commands: ${client.commands.size}`)
}