让我的踢命令只允许具有权限节点 KICK_MEMBERS
的人踢人时遇到了一些困难。目前我的机器人处于任何人和每个人都可以踢的状态,以下是我的代码。
const Discord = require('discord.js')
module.exports.run = async (bot, message, args) => {
const user = message.mentions.users.first();
if (user) {
const member = message.guild.member(user);
if (member) {
member
.kick('Optional reason that will display in the audit logs')
.then(() => {
message.reply(`Successfully kicked ${user.tag}`);
})
.catch(err => {
message.reply('I was unable to kick the member');
console.error(err);
});
} else {
message.reply("That user isn't in this guild!");
}
} else {
message.reply("You didn't mention the user to kick!");
}
};
module.exports.help = {
name: "kick"
}
答案 0 :(得分:1)
GuildMember#hasPermission
返回用户是否具有指定权限的布尔值。我们可以使用它来声明用户是否具有 KICK_MEMBERS
权限。
const Discord = require('discord.js')
module.exports.run = async (bot, message, args) => {
if (!message.member.hasPermission('KICK_MEMBERS', { checkAdmin: true, checkOwner: true })) return message.reply('You cannot use this command!')
// I like to have it so people with the administrator permission can use it regardless.
const user = message.mentions.users.first();
if (user) {
const member = message.guild.member(user);
if (member) {
member
.kick('Optional reason that will display in the audit logs')
.then(() => {
message.reply(`Successfully kicked ${user.tag}`);
})
.catch(err => {
message.reply('I was unable to kick the member');
console.error(err);
});
} else {
message.reply("That user isn't in this guild!");
}
} else {
message.reply("You didn't mention the user to kick!");
}
};
module.exports.help = {
name: "kick"
}