【问题标题】:Some problems with the !kick command code!kick 命令代码的一些问题
【发布时间】:2020-09-22 23:38:54
【问题描述】:

我的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!`);
  }
 },
};

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

我正在尝试做 3 件事:

  • 如果有人试图通过提及机器人来使用该命令,他们会说“你不能这样做”

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

  • 我希望会员被踢,你必须做出是或否的反应

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    我会尝试一一解决你的问题:


    • 首先,我想如果有人试图通过提及机器人来使用该命令,他们会说“你不能这样做”

    您可以使用client.user 属性(您的客户端登录的用户)执行if 语句来检测提及的成员是否与您的机器人共享相同的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`));
     });
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2021-03-01
      • 2019-08-22
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多