有人可以帮助cmd未定义

时间:2020-02-20 22:29:21

标签: discord.js

有人可以帮忙吗,我不知道如何解决 这是我得到的代码低于尝试运行命令处理程序的错误,我认为您聪明的人可以提前感谢

错误:index.js:33 let commandfile = client.commands.get(command.slice(prefix.length)); ^

ReferenceError:未定义命令

  const Discord = require('discord.js');
    const { prefix, token} = require ('./config.json');
    const client = new Discord.Client();
    const fs = require("fs");
    client.commands = new Discord.Collection();

    fs.readdir("./commands/", (err, files) => {
        if(err) console.log(err);

        let jsfile = files.filter(f => f.split(".").pop() === "js")
        if(jsfile.length <= 0){
          console.log("Couldn't find commands.");
          return;
        }

        jsfile.forEach((f, i) => {
            let props = require(`./commands/${f}`);
            console.log(`${f} loaded!`);
            client.commands.set(props.help.name, props);
        });
    });

    // The ready event is vital, it will tell you if your bot is on it the console. 
    client.on('ready', () => {
        console.log(`Logged in as ${client.user.tag}!`);
    })


    client.on('message', message => {
       //Ignore messages that aren't from a guild
      if (!message.guild) return;

      let commandfile = client.commands.get(command.slice(prefix.length));
      if(commandfile) commandfile.run(bot,message,args);

    //   // KICK COMMAND

    //   // If the message content starts with "!kick"
    //   if (message.content.startsWith('!kick')) {
    //     // Assuming we mention someone in the message, this will return the user
    //     // Read more about mentions over at https://discord.js.org/#/docs/main/stable/class/MessageMentions
    //     const user = message.mentions.users.first();
    //     // If we have a user mentioned
    //     if (user) {
    //       // Now we get the member from the user
    //       const member = message.guild.member(user);
    //       // If the member is in the guild
    //       if (member) {
    //         /**
    //          * Kick the member
    //          * Make sure you run this on a member, not a user!
    //          * There are big differences between a user and a member
    //          */
    //         member.kick('Optional reason that will display in the audit logs').then(() => {
    //           // We let the message author know we were able to kick the person
    //           message.reply(`Successfully kicked ${user.tag}`);
    //         }).catch(err => {
    //           // An error happened
    //           // This is generally due to the bot not being able to kick the member,
    //           // either due to missing permissions or role hierarchy
    //           message.reply('I was unable to kick the member');
    //           // Log the error
    //           console.error(err);
    //         });
    //       } else {
    //         // The mentioned user isn't in this guild
    //         message.reply('That user isn\'t in this guild!');
    //       }
    //     // Otherwise, if no user was mentioned
    //     } else {
    //       message.reply('You didn\'t mention the user to kick!');
    //     }
    //   }

        // KICK COMMAND

      // If the message content starts with "!ban"
      if (message.content.startsWith('!ban')) {
        // Assuming we mention someone in the message, this will return the user
        // Read more about mentions over at https://discord.js.org/#/docs/main/stable/class/MessageMentions
        const user = message.mentions.users.first();
        // If we have a user mentioned
        if (user) {
          // Now we get the member from the user
          const member = message.guild.member(user);
          // If the member is in the guild
          if (member) {
            /**
             * ban the member
             * Make sure you run this on a member, not a user!
             * There are big differences between a user and a member
             */
            member.ban('Optional reason that will display in the audit logs').then(() => {
              // We let the message author know we were able to kick the person
              message.reply(`Successfully banned ${user.tag}`);
            }).catch(err => {
              // An error happened
              // This is generally due to the bot not being able to kick the member,
              // either due to missing permissions or role hierarchy
              message.reply('I was unable to ban the member');
              // Log the error
              console.error(err);
            });
          } else {
            // The mentioned user isn't in this guild
            message.reply('That user isn\'t in this guild!');
          }
        // Otherwise, if no user was mentioned
        } else {
          message.reply('You didn\'t mention the user to ban!');
        }
      }
    });





    client.login(token);


1 个答案:

答案 0 :(得分:0)

在第33行,您尝试引用名为command的属性,但未在任何地方定义该属性。
您希望命令从哪里来/您希望它来自何处?似乎缺少的步骤是检查message属性,对其进行处理并将其用作命令,例如similar to this

  // 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);