問題描述
由于像這樣 https://media.discordapp.net/attachments/223867697312694272/723382952330919976/image0.png
相反,我的機器人允許用戶將上面的角色添加給他們自己或其他用戶,只要他們具有所需的權(quán)限 https://cdn.discordapp.com/attachments/223867697312694272/723382993384767550/image0.png
Instead my bot allows the user to add the role above them to themselves or other users as long as they have the required permissions https://cdn.discordapp.com/attachments/223867697312694272/723382993384767550/image0.png
我的代碼:
const Discord = require("discord.js");
module.exports.run = async (bot, message, args) => {
if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("You don't have permissions to use this!");
let xdemb = new Discord.RichEmbed()
.setColor("RANDOM")
.setTitle("Role Command")
.addField("Description:", `Adds/removes a role to/from a member`, true)
.addField("Usage:", "`?role` [user] roleName", true)
.addField("Example:" ,"`?role` @user goodrole", true)
let member = message.mentions.members.first();
if(!member) return message.channel.send(xdemb)
let role = args.slice(2).join(" ")
if(!role) return message.channel.send("Provide a role to assign")
let gRole = message.guild.roles.find(r => r.name.toLowerCase() === role.toLowerCase())
if(!gRole) return message.channel.send(`There's no role with the name `${role}``)
if(member.roles.has(gRole.id)) {
member.removeRole(gRole.id)
message.channel.send(`Removed role `${role}` from **${member.user.username}**`)
} else {
member.addRole(gRole.id)
message.channel.send(`Added role `${role}` to **${member.user.username}**`)
}
}
module.exports.help = {
name: "role"
}
推薦答案
Discord.js 中的 Role
類有一個 position
屬性,表示其在角色管理器中的位置.
The Role
class in Discord.js has a position
property which represents its position in the role manager.
GuildMember
類有一個名為 roles
,它的類型是 GuildMemberRoleManager
.GuildMemberRoleManager
類有一個名為 highest
,指向成員擁有的排名最高的角色.
The GuildMember
class has a property named roles
, which is of type GuildMemberRoleManager
. The GuildMemberRoleManager
class has a property named highest
, which points to the highest ranked role that the member has.
所以.為了確保機器人不會在層次結(jié)構(gòu)中為成員提供比他們更高的角色,您可以將他們想要的角色的 position
與 position
進行比較他們目前的最高職位.
So. To make sure that the bot doesn't give a member a role that is higher than them in the hierarchy, you can compare the position
of the role they want with the position
of their current highest role.
這篇關(guān)于用戶角色層次結(jié)構(gòu)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!