如何使用Discord.js检查消息作者是否具有管理员角色? [英] How can I check if the message author has an admin role using Discord.js?
问题描述
我正在构建Discord机器人,我想有一个 if
语句,该语句仅在消息作者在行会中具有管理员角色时才会继续。
我尝试拥有特定于角色的权限,但这意味着该漫游器所在的所有服务器上都必须具有完全相同的名称角色。
如何检查消息作者是否具有管理员角色? (该角色具有管理员权限。)
必须修改以下某些代码才能在最新版本中使用主要的Discord.js版本(在本次编辑时为v12)由于实施了Managers。
这里确实需要解决三个不同的问题。它们都是相关的,但是每个都有不同的直接答案。
如何检查如果消息作者具有管理员角色?
-
通过
GuildMember = https://discord.js.org/#/docs/main/v11/class/Message?scrollTo=member rel = nofollow noreferrer> Message#member
属性,而不是Message#author
返回用户。请记住,成员具有角色和权限,不是用户。 -
A 成员角色的集合可以通过
GuildMember#roles
。 -
您可以通过两种主要方式搜索角色:
- ID:
Map#has()
- 属性:
Collection#find()
- ID:
所以,将它们绑在一起:
if(message.member.roles.has'roleIDHere'))console.log('用户是n管理员。);
或
< pre class = lang-js prettyprint-override>
if(message.member.roles.find(role => role.name ==='Admin'))console.log('用户为管理员);
我该如何检查消息作者的角色是否具有管理员权限?
- 再次,我们需要使用
Message#member
中的GuildMember。 - 同样,我们需要使用
GuildMember #roles
集合。 - 然后...déjàvu ...您可以使用
Collection#find()
。 - 这次,您应该专门检查
Role#hasPermission()
。
例如:
if(message .member.roles.find(角色=> role.hasPermission('Administrator')))console.log('用户是管理员。');
您也可以将此概念应用于任何特定角色。
这种情况的最佳方法...
如何检查消息作者是否具有管理员权限?
- 我们继续使用
Message#member
来访问GuildMember。 - 但是,您可以检查 all 通过
GuildMember#hasPermission()
方法。
请考虑以下简短示例:
if(message.member.hasPermission('ADMINISTRATOR'))console.log('用户是管理员。');
快速,对吧?
在尝试检查用户是否为管理员之前,请确保您检查客户端收到的消息不是DM。当邮件不是在公会中发送时, Message#member
是未定义的,尝试使用其属性将引发错误。
使用此条件,如果消息为DM,则将停止:
如果(!message.guild)返回;
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 toMessage#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:
- ID:
Map#has()
- Property:
Collection#find()
- ID:
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?
- Again, we need to use the GuildMember from
Message#member
. - And again, we need to use the
GuildMember#roles
collection. - And... déjà vu... you can search through a Collection with
Collection#find()
. - This time, you should specifically check
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?
- We continue to use
Message#member
to access the GuildMember. - However, you can check all of a member's permissions at once via the
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检查消息作者是否具有管理员角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!