How to check if a user is in a specific guild

Hey there, I'm trying to make a command, that invites a person to a football club. Because, I don't want anyone to join that isn't in the main server, I wanted to check if the user is in the discord. I've tried countless solutions, but nothing worked. I don't know why, but I hope that someone can help me with this problem. Here's the code (I highlighted where it will check)
12 Replies
d.js toolkit
d.js toolkit7mo 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!
officialdatch
officialdatchOP7mo ago
And I want this one to check if the specific guild.
No description
officialdatch
officialdatchOP7mo ago
I tried couple of solutions, but none of them works.
d.js docs
d.js docs7mo ago
To share long code snippets, use a service like gist, sourcebin, starbin, or similar instead of posting them as large code blocks or files.
TÆMBØ
TÆMBØ7mo ago
The issue (at least from the screenshot) is that you’re checking the cache. Members are not guaranteed to be cached by default. You should be fetching instead
d.js docs
d.js docs7mo ago
:method: GuildMemberManager#fetch() Fetches member(s) from a guild.
officialdatch
officialdatchOP7mo ago
I think it's in the right direction, but i get this error. How can i fix it so if he doesn't find the person it shows the message
No description
b3ssel
b3ssel7mo ago
If the given user ID is not on the server, you will get an error as return Use this instead:
const userInGuild = await guild.members.fetch(user.id).catch(() => { return null });

if (!userInGuild) {
const nothere = new EmbedBuilder()
//...
};
const userInGuild = await guild.members.fetch(user.id).catch(() => { return null });

if (!userInGuild) {
const nothere = new EmbedBuilder()
//...
};
officialdatch
officialdatchOP7mo ago
Ok, thanks I'll try.
b3ssel
b3ssel7mo ago
so what userInGuild does is trying to fetch the user id, but if its not there it returns an error we need to catch that error and then return null, meaning that userInGuild is equal to null then the if (!userInGuild) statement is checking if userInGuild is null (meaning the user is not in the server) or is a GuildMember object (meaning the user is in the server)
officialdatch
officialdatchOP7mo ago
Thanks for the explanation, I'll check if this works and I'll get back to you. @Becel It worked, tysm
Want results from more Discord servers?
Add your server