久久久久久久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?)
主站蜘蛛池模板: 欧美一区二区大片 | 狠狠视频 | 天天干天天玩天天操 | 亚洲网站在线观看 | 嫩草伊人| 日韩一区二区久久 | 久久不卡 | 国产一区二区久久久 | 国产综合视频 | 久久久久精 | 久久毛片 | 国产一级免费视频 | 五月天婷婷久久 | 日本精品视频在线 | 欧美在线国产精品 | 精品视频www| 精品一二三| 久久精品a级毛片 | 国产综合久久 | 日韩在线播放视频 | 国产色在线 | 免费看国产精品视频 | 国产一区二区三区色淫影院 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 影音先锋久久 | 欧美成人a∨高清免费观看 欧美日韩中 | 国产一区| 欧美一卡二卡在线观看 | 午夜精品久久久久久久久久久久久 | 中文字幕av网站 | 99久久精品免费看国产四区 | 久久岛国| 国产成人精品亚洲日本在线观看 | 国产精品一区二区三区在线 | 99pao成人国产永久免费视频 | 日韩av一区二区在线观看 | 国产伦精品一区二区三毛 | 国产成人亚洲精品 | 欧美激情在线精品一区二区三区 | 国产精品久久久久久久久久 | 91九色婷婷|