简体   繁体   English

获取具有角色的成员列表 | Discord.JS

[英]Get a list of members with a Role | Discord.JS

So, I wanted to create a command like .getuser @role and the bot then tags every user with that role and after it says x people have this role.所以,我想创建一个像.getuser @role这样的命令,然后机器人用这个角色标记每个用户,然后说x people have this role. . .

The problem is that I couldn't find anywhere something that resembles this, I hope someone will help me cause I'm out of luck on this one!问题是我找不到任何类似的东西,我希望有人能帮助我,因为我在这个上不走运!

let membersWithRole = guild.roles.resolve('role_ID').members.size

Basically we get a specific role from a guild trough its ID and then we see the size of the members collection of the role.基本上,我们通过其 ID 从公会中获取特定角色,然后我们会看到该角色的成员集合的大小。

You can use roles.get()您可以使用roles.get()

members = message.guild.roles.cache.find(role => role.name === 'role name').members.map(m=>m.user.tag);

Edit: I've been received lots of messages claiming that the bot is inaccurately showing what members have what roles.编辑:我收到了很多消息,声称该机器人不准确地显示了哪些成员具有哪些角色。 This is because discord uses the cache to grab info about roles.这是因为 discord 使用缓存来获取有关角色的信息。 To fix this do the following...要解决此问题,请执行以下操作...

  1. Navigate to your application at https://discord.com/developers/applicationshttps://discord.com/developers/applications导航到您的应用程序
  2. Go to the bot section Go 到机器人部分
  3. Turn ON "Server Members Intent"打开“服务器成员意图”

This will make it so that your discord bot doesn't grab from the cache for member data from the api.这将使您的 discord 机器人不会从缓存中获取来自 api 的成员数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM