Better way to see if user is in channel
I want to check if a user is in the same channel as the executed command. My current solution is quite big and complex. Is there a simpler/smaller way of achieving this?
31 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!I just discovered
channel.permissionsFor, seems to do the trickwhat does "a user is in the channel" mean?
are you talking about a voice channel?
a lot of that code seems unnecessary
Wether a user has access to a text channel
ah, then yeah permissionsFor
grab the member though
not the user
meaning getMember instead of getUser
then you can pass it to permissionsFor directly and you're done
if this returns null then the user isn't in the guild
oooh thats useful. Whats the difference between the two?
Despite sounding similar there is a distinct difference between users and members in Discord:
- User: global Discord user data (global avatar, username, tag, id)
- GuildMember: user data associated to a guild (guild, nickname, roles, voice, guild avatar, etc.)
- Conversion: User ➞ GuildMember | GuildMember ➞ User
* Note: Events received in cached guilds will often have both the member and user available, eg.
interaction.user and interaction.memberthat saves a lot of lines nice
also Collection are keyed by ids, for the case of members, the member ids
meaning you don't need to .some, just .has
but that's for a future case
for this just follow the getMember approach
This seems to be incompatible

I dont think the member stuff is relevant here honestly
you need to typeguard whether the interaction is in a cached guild
:method: BaseInteraction#inCachedGuild()
discord.js@14.19.3
Indicates whether this interaction is received from a cached guild.why not? from what you described you need it
damn this explains a lot
it'll always be true if you have the guilds intent and the bot isn't user-installable
and the bot is in the guild as a member
but, typescript has no way to know all of that
A lot better than comparing constructor names to strings, not sure how to use it though
how to use which?
inCachedGuild(), what classes give access to it?
figured, thanks for all the help
Is there a place where I can figure out more about cache stuff? The docs dont go deep enough and the API spec is too verbose
Because I have no clue how this stuff works rnthere isn't, but basically if your bot received the full data while it was logged in, it gets cached
That makes sense. If the cache wasn't there, you'd refetch it?
guilds, guild channels except threads and roles are the exception since they get received on start and updated if you have the Guilds intent which is required for most caching to work
i see, thanks
if you call fetch() and it isn't in cache then yes
there isn't a cache eviction system by default though
so if, say you receive a member, and you don't have the GuildMembers intent, it'll get cached, but it's not guaranteed to be up to date later on since you won't receive the events related to guild members (since you don't have the intent)
so if you don't have that intent and you want a member's data to be up to date then you'll need to force fetch them, which is an option on every fetch method, to skip the cache
Sweet, ic
members is probably the most problematic since it's a privileged intent
other than that you should be able to just keep adding the intents you want for their equivalent cache to be up to date
ah, also guild emojis
So in the case of the topic, i tell ts im in a cached guild and it can get the permissions no? Its still colliding types
you should first check and then get the member
Ahhhh fair
because the member by that time is already a member in a non cached | cached guild
Yup, that worked. Thanks so much again