Get a list of all mutual severs

I'd like to get a list of every server that the bot shares with a user. I've looked around here and tried things like client.guilds.cache.filter((guild) => guild.members.cache.has(userID)) but they always end up with a collection of size 0. I'm assuming it's something to do with something not being cached, so I was wondering if there's a better way to do it.
15 Replies
d.js toolkit
d.js toolkit15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Squid
Squid15mo ago
you're pretty close You would need to try and fetch the member since, like you said, they're probably not cached If the promise resolves, then the user is a member If the promise rejects, then the user is not a member That's as simple as guild.members.fetch(userID).then(() => true).catch(() => false): The problem is that .filter() doesn't expect promises (so you can't rely on promises like .fetch() in your filter argument), so you may need to look into for...of loops instead
BradyDaLlama .*˖
BradyDaLlama .*˖OP15mo ago
I see I see so kinda like for each user to check for each server the bot is in check if user is in server, if it is add it to a list of mutual servers then with this list I can do for each item in list, do what I wanted to do originally
Squid
Squid15mo ago
Yep that looks right Just try not to spam the API since, depending on how many users you're checking and how many servers your bot is in, it might be resource-intensive
BradyDaLlama .*˖
BradyDaLlama .*˖OP15mo ago
yeah that's what I'm concerned about. It may end up being a lot of users/ servers as the bot grows, so I'm trying to think of more efficient ways to do it i might make it so the user has to specify which servers they want notifications from or something to keep a list of the servers in a database, that way I can just reference that instead of trying to procedurally find their mutual servers I wish there was just a call to get the mutual servers tho LMAO that would make it a lot simpler
Squid
Squid15mo ago
OAuth2 lets you get the guilds a user is in, and you could cross-reference that with the guilds that your bot has cached The problem with that is that you have to go through OAuth2
BradyDaLlama .*˖
BradyDaLlama .*˖OP15mo ago
Ok see I actually have used that for my dashboard but then it pops up and they have to click accept right?
Squid
Squid15mo ago
yep
BradyDaLlama .*˖
BradyDaLlama .*˖OP15mo ago
which means it would periodically have to do that to keep the servers up to date and I don't really want to do that but it's a good idea would using let userList = guild.members.fetch() for each guild be less API intensive? I feel like that could be a good solution (the difference being I get a list of all the users and use that, instead of one by one fetch the users) I would also have to fetch the guild right before that but you get the idea
Squid
Squid15mo ago
probably? that way you're only making as many API calls as guilds your bot is in, versus making that many API calls multiplied by every user
BradyDaLlama .*˖
BradyDaLlama .*˖OP15mo ago
right
Squid
Squid15mo ago
guilds are cached by default anyways which really helps
BradyDaLlama .*˖
BradyDaLlama .*˖OP15mo ago
It's actually what I use for the local leaderboard, I forgot about it what does that mean for this situation?
Squid
Squid15mo ago
You can just iterate through cilent.guilds.cache, no fetching required
BradyDaLlama .*˖
BradyDaLlama .*˖OP15mo ago
ah awesome! thanks for the help HACKERCAT seems that each server's member fetch takes about .5 seconds soooo probs not the best way to do this but if client.guilds.cache only does the servers on it's shard, it'd be fine
Want results from more Discord servers?
Add your server