为什么这个kick命令代码不起作用? Discord.js

时间:2020-09-10 06:38:22

标签: javascript node.js discord.js

所以,我正在执行一个kick命令,发生的事情是,当您不指定原因或您没有提到踢用户时,机器人会正确地回复您,但是机器人不会踢出所提到的用户

这是我的代码。

const { MessageEmbed } = require('discord.js')

module.exports = {
    name: "kick",
    description: 'Kicks a user.',
    usage: "<user mention> < reason>",
    execute(message, args){

        let kickedMember = message.mentions.users.first() || "No reason specified."
        let reason = args.slice(1).join(" ")

        if(!message.member.hasPermission(["KICK_MEMBERS"])) return message.reply("You don't have the 
        required permissions to run this command.")
        
        if(!kickedMember) return message.channel.send('Please mention a user to kick.')
        if(!reason) return message.reply("Please specify a reason.")

        kickedMember.send(`You have been kicked in ${message.guild.name} due to ${reason}, read the 
        rules to avoid being kicked next time`).then(() =>
        kickedMember.kick()).catch(err => console.log(err))
        
        const embed = new MessageEmbed()
        .setTitle('User Kicked')
        .addField("User Kicked", kickedMember)
        .addField("Reason", reason)
        .addField("Moderator", message.author.tag)
        .addField("Kicked Date", message.createdAt.toLocaleString())
        .setThumbnail(kickedMember.displayAvatarURL())
        .setColor()
        .setFooter(`Kicked by ${message.author.tag}`)
        .setTimestamp(Date.now())
        message.channel.send(embed);
    }
}

是的,我确实定义了MessageEmbed,但由于某种原因它不会显示在此代码中。

1 个答案:

答案 0 :(得分:1)

您不能踢用户,只能踢一个成员,还应该使用.kickable检查该成员是否可以踢。 Discord.js的GuildMember有一个名为GuildMember#kickable的属性,如果可以踢用户,它将返回true,您要做的就是将其添加到代码中:


// Define Discord and use it for the MessageEmbed(), because it may be an error at that part.
const Discord = require('discord.js');

module.exports = {
  name: "kick",
  description: 'Kicks a user.',
  usage: "<user mention> <reason>",
  execute(message, args){

    // Get the `member` property not the `user` property.
    let kickedMember = message.mentions.members.first()

    // I think you meant to put the "No Reason Specified." here?
    let reason = args.slice(1).join(" ") || "No Reason Specified"

    // `.hasPermission()` is deprecated
    if(!message.member.permissions.has(["KICK_MEMBERS"])) return message.reply("You don't have the required permissions to run this command.")
        
    if(!kickedMember) return message.channel.send('Please mention a member to kick.')

    // You don't need this, since you added "No Reason Specified above."
    // if(!reason) return message.reply("Please specify a reason.")

    // Avoid using whatever you did here, because it might break.
    // kickedMember.send(`You have been kicked in ${message.guild.name} due to ${reason}, read the rules to avoid being kicked next time`).then(() =>
    // kickedMember.kick()).catch(err => console.log(err))

    // Check if the bot has permission
    const botMember = message.guild.members.cache.get(client.user.id);
    if (!botMember.permissions.has(['KICK_MEMBERS'])) return message.reply('The bot doesn't have the required permission /`KICK_MEMBERS/`');
        
    // Check if the member is kickable
    if (!kickedMember.kickable) return message.reply('This member cannot be kicked, check if they have a higher role than the bot!')

    // Kick the member and then send the message, not the other way around.
    kickedMember.kick({
      reason: reason
    }).then(member => { // This passes `member` as the member that was kicked.
          
      // Send the message to the kicked member
      member.send(`You have been kicked in ${message.guild.name} due to ${reason}, read the rules to avoid being kicked next time`).catch(err => console.log(err));

      const embed = new MessageEmbed()
        .setTitle('User Kicked')
        .addField("User Kicked", member.tag) // Don't pass their data, just tag, because
                                             // once you kick them, they're no longer
                                             // in the cache.
        .addField("Reason", reason)
        .addField("Moderator", message.author.tag)
        .addField("Kicked Date", message.createdAt) // `.createdAt` is already a string.
        .setThumbnail(member.displayAvatarURL())
        .setColor()
        .setFooter(`Kicked by ${message.author.tag}`)
        .setTimestamp() // You don't need `Date.now()` discord automatically parses
                        // the timestamp into string.

      // Send the embed
      message.channel.send(embed);
    })
  }
}

有关更多信息,请查看以下链接: