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

231
Vistas
How could I automatically read all of the directories and add all files that end in .js to discord collection

This is the code I currently have, how would adapt this to check each sub-directory:

const fs = require('fs')

module.exports = (client, Discord) =>{
    const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))

    for(const file of command_files){
            const command = require(`../commands/${file}`);
            if(command.name) {
                client.commands.set(command.name, command);
            } else {
                continue
            }
         }
}

And this is the layout I have for the commands folder the folder layout

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to wrap the whole code into a function and use some recursion.

Please note that, when using recusion, a depth variable is a wise way to handle it

Something like this should do it:

const fs = require('fs')

module.exports = (client, Discord) =>{

    const depth = 3;
    const finder = (path, currentDepth = 0) => {

        if (currentDepth >= depth) {
            return; // Breaks here
        }

        const dirContent = fs.readdirSync(path);
        const command_files = dirContent.filter(file => file.endsWith('.js'));
        const folders = dirContent.filter(file => {
            const dirPath = path + file;
            // Exists + is a directory verification
            return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
        );

        for(const file of command_files){
            const filePath = '../' + path + file;
            const command = require(filePath);
            if(command.name) {
                client.commands.set(command.name, command);
            } else {
                continue
            }
         }

         // Loops through folders
         folders.map((folder) => finder(path + folder + '/', currentDepth + 1));
    }

    finder('./commands/');
}
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