简体   繁体   English

如何让所有成员都拥有一个角色?

[英]How do I get all members with a role?

I'm trying to send a DM to all members with a specific role.我正在尝试向具有特定角色的所有成员发送 DM。 I've tried that to get members with a role:我已经尝试过让成员担任角色:

guild.roles.cache.get(ROLE_ID).members

and

guild.roles.fetch(ROLE_ID)

My problem is that the bot only returns my user.我的问题是机器人只返回我的用户。

I know someone has asked a question like this before where it was solved using the correct intents , but it's not the case for me.我知道有人在使用正确的intents解决之前问过这样的问题,但对我来说并非如此。

I think the problem is that Role#members returns a Collection of the cached guild members only that have this role.我认为问题在于Role#members仅返回具有此角色的缓存公会成员的集合。 Try to fetch all members first and then try again:尝试先获取所有成员,然后重试:

let members = await guild.members.fetch()
let membersWithRole = guild.roles.cache.get(ROLE_ID).members

Or you can just filter the members by their role:或者您可以按角色filter members

let members = await guild.members.fetch()
let membersWithRole = members.filter(member => member.roles.cache.has(ROLE_ID));

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

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