命令为Discord bot无法正常工作

时间:2017-11-21 10:22:27

标签: discord discord.js

我正在尝试为我的Discord机器人创建一个“帮助”命令,但似乎我无法弄清楚原因。清除命令也不起作用,但其余部分正在工作。 kick,ping,ban和say命令现在都在运行。而且我还试图找出如何让机器人日志命令使用到控制台。任何帮助将不胜感激!

client.on("message", async message => {
  // This event will run on every single message received, from any channel or DM.

  // It's good practice to ignore other bots. This also makes your bot ignore itself
  // and not get into a spam loop (we call that "botception").
  if(message.author.bot) return;

  // Also good practice to ignore any message that does not start with our prefix, 
  // which is set in the configuration file.
  if(message.content.indexOf(config.prefix) !== 0) return;

  // Here we separate our "command" name, and our "arguments" for the command. 
  // e.g. if we have the message "+say Is this the real life?" , we'll get the following:
  // command = say
  // args = ["Is", "this", "the", "real", "life?"]
  const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();

  // Let's go with a few common example commands! Feel free to delete or change those.

  if(command === "ping") {
    // Calculates ping between sending a message and editing it, giving a nice round-trip latency.
    // The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
    const m = await message.channel.send("Ping?");
    m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
  }

  if(command === "say") {
    // makes the bot say something and delete the message. As an example, it's open to anyone to use. 
    // To get the "message" itself we join the `args` back into a string with spaces: 
    const sayMessage = args.join(" ");
    // Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
    message.delete().catch(O_o=>{}); 
    // And we get the bot to say the thing: 
    message.channel.send(sayMessage);
  }

if (command === "kick") {
    let modRole = message.guild.roles.find("name", "[Owner]");
    if(message.member.roles.has(modRole.id)) { 
      let kickMember = message.guild.member(message.mentions.users.first());
      message.guild.member(kickMember).kick();
      message.channel.sendMessage("Member Kicked.");
    } else {
      return message.reply("You dont have the perms to kick members. scrub.");
    }
  }

  if(command === "ban") {
    let modRole = message.guild.roles.find("name", "[Owner]");
    if(message.member.roles.has(modRole.id)) { 
      let banMember = message.guild.member(message.mentions.users.first());
      message.guild.member(banMember).ban();
      message.channel.sendMessage("Member banned.");
    } else {
      return message.reply("You dont have the perms to ban members. scrub.");
    }

  if(command === "purge") {
    // This command removes all messages from all users in the channel, up to 100.

    // get the delete count, as an actual number.
    const deleteCount = parseInt(args[0], 10);

    // Ooooh nice, combined conditions. <3
    if(!deleteCount || deleteCount < 2 || deleteCount > 100)
      return message.reply("Please provide a number between 2 and 100 for the number of messages to delete");

    // So we get our messages, and delete them. Simple enough, right?
    const fetched = await message.channel.fetchMessages({count: deleteCount});
    message.channel.bulkDelete(fetched)
      .catch(error => message.reply(`Couldn't delete messages because of: ${error}`));
  }

  if(command === "help") {
    message.channel.send({embed: {
      color: 3447003,
      author: {
        name: client.user.username,
        icon_url: client.user.avatarURL
      },
      title: "Help",
      description: "This message contains all the info of the bot's commands",
      fields: [{
          name: "d!help",
          value: "This command can be used by everyone; displays this message"
        },
        {
          name: "d!ping",
          value: "This command can be used by everyone; it's tells the latency of the bot and the Discord API"
        },
        {
          name: "d!kick <user>",
          value: "This command can be used by [Owner]; it kicks a user"
        },
        {
          name: "d!ban <user>",
          value: "This command can be used by [Owner]; it bans a user"
        },
        {
          name: "d!purge",
          value: "This command isn't working correctly for as far as we know"
        }          
      ],
      timestamp: new Date(),
      footer: {
        text: "© DeltBot Team"
      }
    }

1 个答案:

答案 0 :(得分:0)

由于您使用的是async function,因此您获取的变量和bulkDelete必须位于await