Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
Discord.JS Command Handler Sub-Folders

I currently have a Discord.JS command handler that loads all commands in the command folder. However it is getting a bit messy now due to all the commands, therefore I have been trying to implement Sub-Folders. This would clean up the files inside the commands folder, organizing them into folder groups.

I currently have this code for loading the commands in the same folder.

But I am stuck on how I could go into each folder in the commands directory and load the commands. If someone could help me, that would be appreciated.


const allCommands = fs.readdirSync('./commands');
for (const command of allCommands) {
    try {
        const loadedCommand = require(`./commands/${command}`);
        commands.set(loadedCommand.name, loadedCommand);
        
        for(const alias of loadedCommand.aliases || []) 
            commands.set(alias, { ...loadedCommand, alias: true });
        
        logger.info(`Loaded command ${loadedCommand.name} (${command})`);
    } catch (error) {
        logger.error(`Failed to load command ${command}. **Please report the following error:**`);
        logger.error(error);
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Move everything into its own function with the path as a parameter. Every time you find a folder, call the function with the folder path.

(function readdir(path='./commands') {
    const allCommands = fs.readdirSync(path);
    for (const command of allCommands) {
        if(fs.statSync(`${path}/${command}`).isDirectory()) {
            readdir(`${path}/${command}`);
            continue;
        }

        try {
            const loadedCommand = require(`${path}/${command}`);
            commands.set(loadedCommand.name, loadedCommand);
            
            for(const alias of loadedCommand.aliases || []) 
                commands.set(alias, { ...loadedCommand, alias: true });
            
            logger.info(`Loaded command ${loadedCommand.name} (${path}/${command})`);
        } catch (error) {
            logger.error(`Failed to load command ${path}/${command}. **Please report the following error:**`);
            logger.error(error);
        }
    }
})();
about 4 years ago · Juan Pablo Isaza Relatório

0

Honestly, I don't recommend importing this way. You might have better luck if you import each of your commands into a seperate javascript file then export each of your commands from there, it should make your controller level code much easier to read

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda