我正在构建一个 Discord 机器人,并且我想要一个 if
语句,该语句仅在消息作者在公会中具有管理员角色时才会继续.
我尝试过拥有特定于角色的权限,但这意味着机器人所在的所有服务器上都必须有完全相同名称的角色.
如何检查邮件作者是否具有管理员角色?(该角色具有管理员权限.)
由于管理器的实施.
这里确实需要解决三个不同的问题.它们都是相关的,但每个都有不同的直接答案.
<小时><块引用>如何检查邮件作者是否具有管理员角色?
发送消息的 GuildMember 是通过 Message#member
访问 属性,而不是 Message#author
返回一个 User.请记住,成员具有角色和权限,而不是用户.
成员角色的Collection可以使用 GuildMember#roles
检索.
您可以通过两种主要方式搜索角色:
Map#has()
Collection#find()
所以,把这一切联系在一起:
if (message.member.roles.has'roleIDHere')) console.log('User is an admin.');
或
if (message.member.roles.find(role => role.name === 'Admin')) console.log('用户是管理员.');
<小时><块引用>
如何查看邮件作者的角色是否有管理员权限?
Message#member
中的 GuildMember.GuildMember#roles
集合.Collection#find()
搜索集合.Role#hasPermission()
在谓词函数中.例如:
if (message.member.roles.find(role => role.hasPermission('Administrator'))) console.log('用户是管理员.');
您也可以将此概念应用于任何特定角色.
<小时>这种情况的最佳方法...
<块引用>如何查看邮件作者是否有管理员权限?
Message#member
来访问 GuildMember.GuildMember#hasPermission()
方法.考虑这个简短的例子:
if (message.member.hasPermission('ADMINISTRATOR')) console.log('User is an admin.');
快,对吧?
<小时>在尝试检查用户是否为管理员之前,请确保检查您的客户收到的消息不是 DM.Message#member
未在公会中发送消息时未定义,尝试使用它的属性会引发错误.
使用此条件,如果消息是 DM,它将停止:
if (!message.guild) return;
I'm building a Discord bot and I want to have an if
statement that will only proceed if the message author has an administrator role in the guild.
I've tried having role-specific permissions, but this means that there will have to be the exact same name role on all servers that the bot is on.
How can I check if the message author has an admin role? (The role has the administrator permission.)
Some of the following code must be modified to use in the newest major Discord.js version (v12 at the time of this edit) due to the implementation of Managers.
There's really three different questions needed to be addressed here. They're all related, but each have different direct answers.
How do I check if the message author has an Admin role?
The GuildMember that sent a message is accessed via the Message#member
property, as opposed to Message#author
which returns a User. Remember, a member has roles and permissions, not a user.
A Collection of a member's roles can be retrieved with GuildMember#roles
.
You can search for a role two main ways:
Map#has()
Collection#find()
So, tying this all together:
if (message.member.roles.has'roleIDHere')) console.log('User is an admin.');
or
if (message.member.roles.find(role => role.name === 'Admin')) console.log('User is an admin.');
How do I check if the message author's role has the Administrator permission?
Message#member
.GuildMember#roles
collection.Collection#find()
.Role#hasPermission()
in the predicate function.For example:
if (message.member.roles.find(role => role.hasPermission('Administrator'))) console.log('User is an admin.');
You can apply this concept to any specific role, too.
Best method for this situation...
How do I check if the message author has the Administrator permission?
Message#member
to access the GuildMember.GuildMember#hasPermission()
method.Consider this short example:
if (message.member.hasPermission('ADMINISTRATOR')) console.log('User is an admin.');
Quick, right?
Make sure you check that the message your client receives is not a DM before attempting to check if the user is an Admin. Message#member
is undefined when the message isn't sent in a guild, and trying to use properties of it will throw an error.
Use this condition, which will stop if the message is a DM:
if (!message.guild) return;
这篇关于如何使用 Discord.js 检查消息作者是否具有管理员角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!