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

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

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 Relatório

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