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

428
Vistas
Uncaught ReferenceError: command is not defined

I'm trying to do a command handler on a discord bot (with discord.js) but when I start the bot I get an error:

ReferenceError: command is not defined

This is the error I get

const discord = require('discord.js');
const intents = new discord.Intents();
const client = new discord.Client({ intents: 32767 });

const fs = require('fs');
const { readdirSync } = require('fs');

client.commands = new discord.Collection();
const commandFiles = fs.readdirSync('./comandos').filter(file => file.endsWith('.js'));

for(const file of commandFiles){
    const command = require(`./comandos/${file}`);
    client.commands.set(command.name, command);
}

let cmd = client.commands.find((c) => c.name === command || c.alias && c.alias.includes(command));
if(!cmd){
    cmd.execute(client, message, args);
} else {
    if(!cmd){
        return;
    }
}

const config = require('./config.js');
const prefix = config.prefix;

client.login(config.token);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

That must be in a messageCreate event:

client.on("messageCreate", message => {
  const [ command, ...args ] = message.content.split(/ +/g);
  let cmd = client.commands.get(command) || client.commands.find((c) => c.alias?.includes(command));
  if (cmd) { //you also made a mistake here, remove the "!"
    cmd.execute(client, message, args);
  }
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is that you're using const to define command inside the loop. You need to replace it with var and it should work fine:

const discord = require('discord.js');
const intents = new discord.Intents();
const client = new discord.Client({ intents: 32767 });

const fs = require('fs');
const { readdirSync } = require('fs');

client.commands = new discord.Collection();
const commandFiles = fs.readdirSync('./comandos').filter(file => file.endsWith('.js'));

for(const file of commandFiles){
    var command = require(`./comandos/${file}`);
    client.commands.set(command.name, command);
}

let cmd = client.commands.find((c) => c.name === command || c.alias && c.alias.includes(command));
if(!cmd){
    cmd.execute(client, message, args);
} else {
    if(!cmd){
        return;
    }
}

const config = require('./config.js');
const prefix = config.prefix;

client.login(config.token);

(using "let" won't work either because it is not in the global scope).

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