User role-hierarchy(用户角色层次结构)
问题描述
由于像这样 https://media.discordapp.net/attachments/223867697312694272/723382952330919976/image0.png
相反,我的机器人允许用户将上面的角色添加给他们自己或其他用户,只要他们具有所需的权限 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.
所以.为了确保机器人不会在层次结构中为成员提供比他们更高的角色,您可以将他们想要的角色的 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.
这篇关于用户角色层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用户角色层次结构
基础教程推荐
- 直接将值设置为滑块 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01