如何使用 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 检查消息作者是否具有管理员角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

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?)