久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 Discord.js 檢查消息作者是否具有管理員

How can I check if the message author has an admin role using Discord.js?(如何使用 Discord.js 檢查消息作者是否具有管理員角色?)
本文介紹了如何使用 Discord.js 檢查消息作者是否具有管理員角色?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在構建一個 Discord 機器人,并且我想要一個 if 語句,該語句僅在消息作者在公會中具有管理員角色時才會繼續.

我嘗試過擁有特定于角色的權限,但這意味著機器人所在的所有服務器上都必須有完全相同名稱的角色.

如何檢查郵件作者是否具有管理員角色?(該角色具有管理員權限.)

解決方案

由于管理器的實施.

這里確實需要解決三個不同的問題.它們都是相關的,但每個都有不同的直接答案.

<小時><塊引用>

如何檢查郵件作者是否具有管理員角色?

  • 發送消息的 GuildMember 是通過 Message#member 訪問 屬性,而不是 Message#author 返回一個 User.請記住,成員具有角色和權限,而不是用戶.

  • 成員角色的Collection可以使用 GuildMember#roles 檢索.

  • 您可以通過兩種主要方式搜索角色:

    1. ID:Map#has()
    2. 屬性: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.
  • 但是,您可以通過 所有成員的權限?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 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:

    1. ID: Map#has()
    2. Property: 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?

  • 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應)
Check if user ID exists in Discord server(檢查 Discord 服務器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創建我的第一個機器人,但總是錯誤 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機器人編寫事件/命令處理程序?)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
主站蜘蛛池模板: 久久一二| 亚洲国产精品人人爽夜夜爽 | 欧美精品一区二区在线观看 | 新91| 久久无毛 | 亚洲精品一区二三区不卡 | 一区二区视屏 | 91国产精品 | 国内精品久久精品 | gogo肉体亚洲高清在线视 | 日韩精品在线视频 | 成人午夜网站 | av电影一区 | 国产亚洲成av人片在线观看桃 | 日本成人毛片 | 中国一级特黄真人毛片免费观看 | 粉嫩一区二区三区四区公司1 | 久草成人| av色站| 91精品久久久久久久久久入口 | 日韩精品在线网站 | 亚洲欧美日韩精品久久亚洲区 | 亚洲一区二区免费视频 | 日韩激情在线 | 国产乱码精品一品二品 | 成人午夜免费视频 | 黄色播放| 亚洲欧洲视频 | 日韩三级免费观看 | 欧美日韩国产一区二区三区不卡 | 成年无码av片在线 | 天天干天天操天天射 | 国产有码| 精品一区二区在线观看 | 日韩在线资源 | 久久中文字幕一区 | 黄色亚洲| 精品久久久精品 | 91av视频在线免费观看 | 成年视频在线观看 | 久久人爽爽人爽爽 |