首页 > 解决方案 > 为什么这个踢命令代码不起作用?不和谐.js

问题描述

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

这是我的代码。

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,但由于某种原因它不会出现在这段代码中。

标签: javascriptnode.jsdiscord.js

解决方案


你不能踢一个用户,你只能踢一个成员,你还应该检查该成员是否可以使用.kickable. Discord.jsGuildMember有一个名为的属性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);
    })
  }
}

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


推荐阅读