Iterating over all server members OR iterating over people with x role

Hello. Thank you for always being so helpful and taking care of us. I'm having trouble iterating over but users and boosters. So my first approach was fetching all guild members, and iterating over that and checking if they are boosters, but fetching members times out. So I tried this simple fetch of all the role members:
let boosterRole = await message.channel.guild.roles.fetch("977927036523929600")

console.log(boosterRole)
let boosters = await boosterRole.members.map(m => m.user.id)
message.channel.send("Getting all users with the booster role... Done! " + boosters.length)
let boosterRole = await message.channel.guild.roles.fetch("977927036523929600")

console.log(boosterRole)
let boosters = await boosterRole.members.map(m => m.user.id)
message.channel.send("Getting all users with the booster role... Done! " + boosters.length)
This somewhat works, it gets only 4 users though (there is 23 boosters). All of the 4 users it gets are boosters though. Both the console log and the message says 4 members.
Solution:
If fetching members times out it means you didn't give your bot the guild members intent
Jump to solution
12 Replies
bomi
bomi2y ago
Giving it {force: true} does not make a difference
Favna
Favna2y ago
so basically you want to know which users of a server are boosters?
bomi
bomi2y ago
Yeah I mean I dont have trouble with that otherwise where I know which user is in question and I can then check if they have that role I could iterate over all the users but that'd be very slow
Favna
Favna2y ago
Try manually sending a fetch request using the endpoint the client also calls:
https://discord.com/api/v9/guilds/<guild_id>/premium/subscriptions
https://discord.com/api/v9/guilds/<guild_id>/premium/subscriptions
i.e. for this guild it is https://discord.com/api/v9/guilds/737141877803057244/premium/subscriptions @Vladdy might be able to tell you how auth works for the endpoint (it's an authorization header, I'm just not sure if it can be requested with a bot token, you can try though)
vladdy
vladdy2y ago
That endpoint is client only
Favna
Favna2y ago
oh <_<
vladdy
vladdy2y ago
You'll want to fetch all members first, then the role Then you should get the right number
Solution
vladdy
vladdy2y ago
If fetching members times out it means you didn't give your bot the guild members intent
Favna
Favna2y ago
very lame discord, I thought I had a genius idea for once but oh no
vladdy
vladdy2y ago
That's not how you do bot auth anyways Missing prefix
bomi
bomi2y ago
Interesting Yep that's it, noob mistake as always
vladdy
vladdy2y ago
Happens to all of us
Want results from more Discord servers?
Add your server