Ata Ege
Ata Ege

Reputation: 40

TypeError: cmd.run is not a function

When I'm trying to make a Discord.js Command Handler I'm taking this error. How can I fix this? I checked my app.js there is no problem.

My binding code:

    // Ignore all bots
    if (message.author.bot) return;

    // Ignore messages not starting with the prefix (in config.json)
    if (message.content.indexOf(client.config.prefix) !== 0) return;

    // Our standard argument/command name definition.
    const args = message.content.slice(client.config.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    // Grab the command data from the client.commands Enmap
    const cmd = client.commands.get(command) || client.aliases.get(command);
    // If that command doesn't exist, silently exit and do nothing
    if (!cmd) return;
    // Run the command
    cmd.run(client, message, args);

And this is a base command:

const Discord = require('discord.js');

exports.run = (client, message, args) => {
}
module.exports.config = {
   name: "",
   aliases: []
}

Upvotes: 0

Views: 1963

Answers (1)

Pentium1080Ti
Pentium1080Ti

Reputation: 1056

Your run function needs to be inside of the module.exports, as this is the easiest way.

For example:

module.exports = {
   name: "",
   aliases: [],
   run(client, message, args) {
      // Code here
   }
}

Then this will allow you to call cmd.run(client, message, args);. You can read more about command handling in this guide here

Upvotes: 2

Related Questions