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

254
Vistas
I'm unable to push my bot's slash commands to an array. Why?

When I try to push my bot's slash commands to an array which I want to use to register my commands, it doesn't seem to get pushed, as when I console.log the array, it returns an empty array. But when I log each command individually, it logs properly. Why?

Here is the code I use to push my bot's commands to the array:

const commands = []


fs.readdirSync("./commands").forEach(dir => {
  fs.readdir(`./commands/${dir}`, (err, files) => {
    if (err) throw err;

    const jsFiles = files.filter(file => file.endsWith(".js"));

    if (jsFiles.length <= 0)
      return console.log("[COMMAND HANDLER] - Cannot find any commands!");

    jsFiles.forEach(file => {
      const command = require(`./commands/${dir}/${file}`);
      
      commands.push(command)
      
});
console.log(commands)

module.exports = commands
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The readdir function is asynchronous. If you want to use this code with minimal changes then replace it with readdirSync.

Here is the docs with more info: https://nodejs.org/api/fs.html#fsreaddirsyncpath-options

about 4 years ago · Juan Pablo Isaza Denunciar

0

Replace this:

fs.readdirSync("./commands").forEach(dir => {
  fs.readdir(`./commands/${dir}`, (err, files) => {
    if (err) throw err;

    const jsFiles = files.filter(file => file.endsWith(".js"));

    if (jsFiles.length <= 0)
      return console.log("[COMMAND HANDLER] - Cannot find any commands!");

    jsFiles.forEach(file => {
      const command = require(`./commands/${dir}/${file}`);
      
      commands.push(command)
      
});

with this:

const cmdDirectories = fs.readdirSync(`./commands`)
for (const dir of cmdDirectories) {
  const cmdFiles = fs.readdirSync(`./commands/${dir}`).filter(file => file.endsWith(".js"));

  if (cmdFiles.length <= 0)
      return console.log("[COMMAND HANDLER] - Cannot find any commands!");

  for (const file of cmdFiles) {
    const command = require(`./commands/${dir}/${file}`);
    commands.push(command)
  }
}

This solution was found purely by experimentation, I do not know how/why this worked. If someone does know how/why this worked, please leave a comment.

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