如何使用 Discord.js 检查消息作者是否具有管理员角色? [英] How can I check if the message author has an admin role using Discord.js?

查看:15
本文介绍了如何使用 Discord.js 检查消息作者是否具有管理员角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Discord 机器人,并且我想要一个 if 语句,该语句仅在消息作者在公会中具有管理员角色时才会继续.

我尝试过拥有特定于角色的权限,但这意味着机器人所在的所有服务器上都必须有完全相同名称的角色.

如何检查邮件作者是否具有管理员角色?(该角色具有管理员权限.)

解决方案

由于管理器的实施.

这里确实需要解决三个不同的问题.它们都是相关的,但每个都有不同的直接答案.

<小时><块引用>

如何检查邮件作者是否具有管理员角色?

所以,把这一切联系在一起:

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('用户是管理员.');

您也可以将此概念应用于任何特定角色.

<小时>

这种情况的最佳方法...

<块引用>

如何查看邮件作者是否有管理员权限?

考虑这个简短的例子:

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?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆