Skip to content Skip to sidebar Skip to footer

Typeerror: Cannot Read Property Of 'execute' Of Undefined When Trying To Execute A Command File

I am making a discord bot with a simple command handler. I have never used a command handler before so I don't really know a lot about these sorts of functions and things like that

Solution 1:

To answer your question, your code doesn't work because for one you need to call it like this client.commands.get('declineAlly').execute(message, msg); and you always run client.commands.get('./commands/allyApplication').execute(message, msg); because of the else, meaning that your code doesn't even get to the point where you define your commands. Additionally you always pass an empty string to the command. What you also have done here, which is in itself not wrong, is that you have to manually add each command to the handler. Thats not really efficient.

So lets fix that.

Lets start at the top. Your code to set the commands into the collection works fine. So lets get to the root of your issue, the client.on('message', message part. in the following code snippets I always use message instead of msg.

At the start you should do two things. First check if the channel is a DM and if so return.

if (message.guild === null) {
    return message.reply("Hey there, no reason to DM me anything. I won't answer anyway :wink:"); //example sentence
}

And check if the user who sends this message is a bot. That way other bots can't use yours.

if (message.author.bot) return;

What you should do next is set a prefix value, in your case that would be ! and check if a message starts with said prefix.

const prefix = '!';
if (!message.content.startsWith(prefix)) {
    return;
}

Now that we have checked if the message is actually a command we can slice off the prefix and convert the rest of the message into an array that we will call args.

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

Next we need to take the first element of that array, remove it and store it as our command value. We also convert it to lowercase.

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

Next we need to check if the message actually has some arguments. Thats important so the rest of this code doesn't get executed if the message is just a simple !.

if (cmd.length === 0) return;

After that it's time to get the command from our command collection.

letcommand = client.commands.get(cmd);

Once that is done we need to check if the command actually exists. If it does not then we return with an error.

if(!command)return message.reply(`\`${prefix + cmd}\` doesn't exist!`);

Once we have confirmed that the command exists it's time to execute that command.

command.execute(message, args);

And there you have it. The command handler is finished. We are still not done yet though. Before you can use the commands we need to change something there.

  • First, from this point on you will call the command with the name of the command and not with something else, like you have in your code.
  • Secondly you need to make sure the name of the command is completly lowercase. Thats because we converted the command to lowercase in the handler.

Lastly we should change the command a little bit so it becomes easier to read.

module.exports = {
    name: 'declineally',
    description: 'Declines allies.',
    execute: (message, msg) => {
        // the rest of your code
    }
} 

After all of this, your client.on('message' event should look a litte something like this:

client.on('message', message => {
    // check if the message comes through a DMif (message.guild === null) {
        return message.reply("Hey there, no reason to DM me anything. I won't answer anyway :wink:");
    }
    // check if the author is a botif (message.author.bot) return;
    
    // set a prefix and check if the message starts with itconst prefix = "!";
    if (!message.content.startsWith(prefix)) {
        return;
    }

    // slice off the prefix and convert the rest of the message into an arrayconst args = message.content.slice(prefix.length).trim().split(/ +/g);
    
    // convert all arguments to lowercaseconst cmd = args.shift().toLowerCase();

    // check if there is a message after the prefixif (cmd.length === 0) return;

    // look for the specified command in the collection of commandslet command = client.commands.get(cmd);

    // if there is no command we return with an error messageif (!command) return message.reply(`\`${prefix + cmd}\` doesn't exist!`);

    // finally run the command
    command.execute(message, args);

});

Post a Comment for "Typeerror: Cannot Read Property Of 'execute' Of Undefined When Trying To Execute A Command File"