!kick命令代码有一些问题

时间:2020-09-01 17:21:39

标签: javascript node.js discord discord.js

我的Discord.js kick命令遇到问题。

我的代码:

const Discord = require('discord.js');

const { prefix, token } = require('../config.json');

module.exports = {
 name: 'kick',
 description: 'kick users',
 execute(message, args) {
  if (!message.member.hasPermission('KICK_MEMBERS')) {
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, You are not allowed to use this command.`,
     footer: {
      text: ` | Required permission: KICK_MEMBERS`,
     },
    },
   });
  }
  if (!message.guild.me.permissions.has('KICK_MEMBERS')) {
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, I am not allowed to use this command.`,
     footer: {
      text: ` | Required permission: KICK_MEMBERS`,
     },
    },
   });
  }

  if (!args[0]) {
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, You need to mention a user first.`,
     footer: {
      text: ` | Example: !kick @Bot`,
     },
    },
   });
  }
  const member =
   message.mentions.members.first() || message.guild.members.cache.get(args[0]);
  if (member.user.id === message.author.id) {
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, You cannot expel yourself.`,
     footer: {
      text: ` | Example: !kick @Bot`,
     },
    },
   });
  }
  try {
   member.kick();
   message.channel.send(`${member} has been kicked!`);
  } catch (e) {
   return message.channel.send(`User isn't in this server!`);
  }
 },
};

忽略代码不完整,我仍在考虑嵌入的设计!

我正在尝试做三件事:

  • 我希望有人通过提及bot来尝试使用该命令,他们会说类似“您不允许这样做”之类的内容。

  • 我想要的另一件事是用户不可能将某人踢到他上方

  • 我希望成员被踢,您必须以是或否作出反应

1 个答案:

答案 0 :(得分:2)

我将尝试一一解决您的问题:


  • 首先,我想如果有人通过提及bot尝试使用该命令,他们会说类似“您不允许这样做”之类的内容。

您可以执行if语句,以使用client.user属性(您的客户登录的用户)检测所提及的成员是否与您的机器人共享相同的ID

if (member.id === client.user.id)
 return message.channel.send('You cannot ban me');

  • 我想要的另一件事是用户不可能将某人踢到他上方

您可以通过比较两个成员的roles.highest.position属性来解决此问题。此属性将返回一个数字。数字越大,优先级越高。

if (message.member.roles.highest.position <= member.roles.highest.position)
 return message.channel.send(
  'Cannot kick that member because they have roles that are higher or equal to you.'
 );

  • 最后,我希望成员被踢,您必须以是或否作出反应

为此,您需要使用反应收集器。这就是使用Message.awaitReactions的方式。此方法将等待某人对消息做出反应,然后记录反应后的表情符号。

// first send the confirmation message, then react to it with yes/no emojis
message.channel
 .send(`Are you sure you want to kick ${member.username}?`)
 .then((msg) => {
  msg.react('?');
  msg.react('?');

  // filter function
  const filter = (reaction, user) =>
   ['?', '?'].includes(reaction.emoji.name) && user.id === message.author.id; // make sure it's the correct reaction, and make sure it's the message author who's reacting to it

  message
   .awaitReactions(filter, { time: 30000 }) // make a 30 second time limit before cancelling
   .then((collected) => {
    // do whatever you'd like with the reactions now

    if (message.reaction.name === '?') {
     // kick the user
    } else {
     // don't kick the user
    }
   })
   .catch(console.log(`${message.author.username} didn't react in time`));
 });