且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Discord.js 检查消息作者是否具有管理员角色?

更新时间:2023-11-30 12:11:28

由于管理器的实施.

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

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

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

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;