Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda