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

169
Visualizações
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 Respostas
Responde à pergunta

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 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