Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

discord - 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: []
}
question from:https://stackoverflow.com/questions/65952704/typeerror-cmd-run-is-not-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...