Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

419
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!