Command not defined when trying to send random embeds (Discord.js)

Hey, so I’m trying to code a Discord bot so that when the command is sent, the embed is sent with a random picture. Here’s what I did:

const images = [“Image1Url”, “Image2Url”, “Image3Url”];

const image = images[Math.floor(Math.random() * images.length)];

if (command === testembed) {

const random = new Discord.MessageEmbed()

  .setTitle('Test')

  .setImage(image)

message.channel.send(random);

}

There are no errors apparently, but when I try to run it in cmd, I get this:

ReferenceError: command is not defined

So yeah, I guess I didn’t define it. How do I fix this? Thanks. Also, I’m an absolute beginner at coding, so keep that in mind.

Well we will need to see the rest of the event you have this code in. I assume you have this code inside of a listener. Something like the following…

client.on('message', (receivedMessage) => {
    if (receivedMessage.author == client.user) { // Prevent bot from responding to its own messages
        return
    }
    
    if (receivedMessage.content.startsWith("!")) {
        // Ok we have a message (that started with a "!" character) signaling a command.
        // Here we would split off the first word (the one with the "!") and set your "command" variable to it
    }
})

So with this code above you see that we are listening to messages in the channel. We first check to make sure it is not from the bot itself and secondly we look for the message to start with “!” (you can set this to whatever command character you would like to use). Once we know the message is an apparent command, we can split the message into its specific command and compare that with “testembed”. From there you should be pretty good to continue.

I hope this helps. :slight_smile:

So basically, I use a command handler where I have a main file and for every command I want to add, I create a separate file- so, I have a file called “testembed.js” for the testembed command. Under my main file, I have something similar to what you outlined:

client.on(‘message’, message =>{

if(!message.content.startsWith(prefix) || message.author.bot)return;

const args = message.content.slice(prefix.length).split(/ +/);

const command = args.shift().toLowerCase();

if(command === ‘testembed’){

    client.commands.get('testembed').execute(message, args, Discord);

}

});

Then, in my separate testembed.js file, I have all the code which I showed in the main post.

(Note: I’ve used other more simple commands using the above layout, so I don’t believe the issue is in my main file- I think it’s in my testembed.js file. I could very well be wrong though.)

Ok well you define command in the listener, but I would hypothesize that you are not passing command on to the testembed.js file. I see you pass message, args and Discord. I believe this is setup assuming that testembed knows it is the command testbed, so it doesn’t need to check “command” anymore. Command is defined here in the message callback, but never passed on to the other file.

Get what I am saying? :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.