首页 > 解决方案 > 如何为不和谐机器人创建踢命令?

问题描述

你好,看过这个的人。我需要帮助来为我的不和谐机器人创建踢命令。我正在使用discord.jsnode.js。我确实有像const Discord = require('discord.js'); const Client = new Discord.Client();. 我正在使用 Visual Studio Code 对其进行编码。我真的不明白还能做什么。我曾尝试在 YouTube 上寻求帮助,但每次尝试似乎都不再有效。有人可以指导我吗?感谢:D

标签: node.jsjsondiscorddiscord.js

解决方案


这里...

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

  // 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/master/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!");
    }
  }
});

(来自discord.js github)


推荐阅读