如何为不和谐服务器发出“!kick”命令消息?

时间:2020-05-22 18:20:20

标签: node.js discord.js

我正在尝试制作自己的不协调版主bot!我需要``!kick''命令的帮助,就像我想踢人一样,我只需要使用!kick @user

开始
const discord = require('discord.js');
const client = new discord.Client;

然后

client.on('ready', () => {
    console.log('This bot is running.');
});

client.on('message', msg => {

应该有我的踢命令!

1 个答案:

答案 0 :(得分:0)

我在discordjs documentation

中找到了一些代码
// Import the discord.js module
const Discord = require('discord.js');

// Create an instance of a Discord client
const client = new Discord.Client();

/**
 * The ready event is vital, it means that only _after_ this will your bot start reacting to information
 * received from Discord
 */
client.on('ready', () => {
  console.log('I am ready!');
});

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

// Log our bot in using the token from https://discordapp.com/developers/applications/me
client.login('your token here');

此操作的主要代码是member.kick('Optional reason that will display in the audit logs'),但是您将希望将其嵌套在某些条件语句中,该条件语句检查成员是否存在以及编写命令的人是否是管理员。