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
- 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!And I want this one to check if the specific guild.
I tried couple of solutions, but none of them works.
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
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
If the given user ID is not on the server, you will get an error as return
Use this instead:
Ok, thanks I'll try.
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)Thanks for the explanation, I'll check if this works and I'll get back to you.
@Becel It worked, tysm