为什么这个kick命令代码不能工作?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,但由于某种原因,它不会出现在这段代码中。

您不能踢用户,只能踢成员,还应该使用.kickable检查该成员是否可踢。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);
})
}
}

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

  • Discordjs.guide-指南-踢开会员
  • Discordjs.guide-指南-权限
  • Discord.js-文档-GuildMember属性
  • Discord.js-文档-GuildMember#kickable属性

最新更新