問題描述
我正在構建一個 Discord 機器人,并且我想要一個 if
語句,該語句僅在消息作者在公會中具有管理員角色時才會繼續.
我嘗試過擁有特定于角色的權限,但這意味著機器人所在的所有服務器上都必須有完全相同名稱的角色.
如何檢查郵件作者是否具有管理員角色?(該角色具有管理員權限.)
由于管理器的實施.
這里確實需要解決三個不同的問題.它們都是相關的,但每個都有不同的直接答案.
<小時><塊引用>如何檢查郵件作者是否具有管理員角色?
發送消息的 GuildMember 是通過
Message#member
訪問 屬性,而不是Message#author
返回一個 User.請記住,成員具有角色和權限,而不是用戶.成員角色的Collection可以使用
GuildMember#roles
檢索.您可以通過兩種主要方式搜索角色:
- ID:
Map#has()
- 屬性:
Collection#find()
- ID:
所以,把這一切聯系在一起:
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. - 但是,您可以通過 所有成員的權限?scrollTo=hasPermission" rel="noreferrer">
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 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 檢查消息作者是否具有管理員角色?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!