javascript - 为什么这个踢命令代码不起作用?不和谐.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,但由于某种原因它不会出现在这段代码中。
解决方案
你不能踢一个用户,你只能踢一个成员,你还应该检查该成员是否可以使用.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);
})
}
}
有关更多信息,请查看以下链接:
推荐阅读
- python - 如何将字典键与模型 ID 匹配
- java - 打印给定数字中的最大数字
- django - Django AUTHENTICATION AND AUTHORIZATION 组和权限想要创建经过验证的用户
- java - 为什么 tomcat 比提供静态资源的 nodejs 快
- apache-storm - Storm 0.9.7 版是否仍受支持?
- python - Python Pandas - Lambda 应用保持初始格式
- ietf-netmod-yang - yang模型中同一列表中叶子的基于比较的默认值
- if-statement - 量角器:Else 语句未按预期工作
- postgresql - 基于结果 Postgresql 的 case 语句
- mysql - 处理sql表中的数据类型