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?
const opponent = interaction.options.getUser("opponent");
const guild = interaction.guild;

if (!guild) {
return;
}

const channel = guild.channels.cache.get(interaction.channelId);

if (!channel) {
return;
}

if (!channel.members) {
return;
}

if (channel.members.constructor.name === "Collection") {
const userInChannel = (channel.members as Collection<string, GuildMember>).some(user => user.id == opponent?.id);

if (!userInChannel) {
return;
}
}

//success
const opponent = interaction.options.getUser("opponent");
const guild = interaction.guild;

if (!guild) {
return;
}

const channel = guild.channels.cache.get(interaction.channelId);

if (!channel) {
return;
}

if (!channel.members) {
return;
}

if (channel.members.constructor.name === "Collection") {
const userInChannel = (channel.members as Collection<string, GuildMember>).some(user => user.id == opponent?.id);

if (!userInChannel) {
return;
}
}

//success
31 Replies
d.js toolkit
d.js toolkit6mo 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!
[DQ] Swat
[DQ] SwatOP6mo ago
I just discovered channel.permissionsFor, seems to do the trick
Amgelo
Amgelo6mo ago
what does "a user is in the channel" mean? are you talking about a voice channel? a lot of that code seems unnecessary
[DQ] Swat
[DQ] SwatOP6mo ago
Wether a user has access to a text channel
Amgelo
Amgelo6mo ago
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
[DQ] Swat
[DQ] SwatOP6mo ago
oooh thats useful. Whats the difference between the two?
d.js docs
d.js docs6mo ago
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.member
[DQ] Swat
[DQ] SwatOP6mo ago
that saves a lot of lines nice
Amgelo
Amgelo6mo ago
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
[DQ] Swat
[DQ] SwatOP6mo ago
This seems to be incompatible
const opponent = interaction.options.getMember("opponent");

if (opponent == null) {
return;
}

const opponentPermissions = channel.permissionsFor(opponent);
const opponent = interaction.options.getMember("opponent");

if (opponent == null) {
return;
}

const opponentPermissions = channel.permissionsFor(opponent);
No description
[DQ] Swat
[DQ] SwatOP6mo ago
I dont think the member stuff is relevant here honestly
Amgelo
Amgelo6mo ago
you need to typeguard whether the interaction is in a cached guild
d.js docs
d.js docs6mo ago
:method: BaseInteraction#inCachedGuild() discord.js@14.19.3 Indicates whether this interaction is received from a cached guild.
Amgelo
Amgelo6mo ago
why not? from what you described you need it
[DQ] Swat
[DQ] SwatOP6mo ago
damn this explains a lot
Amgelo
Amgelo6mo ago
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
[DQ] Swat
[DQ] SwatOP6mo ago
A lot better than comparing constructor names to strings, not sure how to use it though
Amgelo
Amgelo6mo ago
how to use which?
[DQ] Swat
[DQ] SwatOP6mo ago
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 rn
Amgelo
Amgelo6mo ago
there isn't, but basically if your bot received the full data while it was logged in, it gets cached
[DQ] Swat
[DQ] SwatOP6mo ago
That makes sense. If the cache wasn't there, you'd refetch it?
Amgelo
Amgelo6mo ago
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
[DQ] Swat
[DQ] SwatOP6mo ago
i see, thanks
Amgelo
Amgelo6mo ago
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
[DQ] Swat
[DQ] SwatOP6mo ago
Sweet, ic
Amgelo
Amgelo6mo ago
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
[DQ] Swat
[DQ] SwatOP6mo ago
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
if (interaction.inCachedGuild()) {
const opponentPermissions = channel.permissionsFor(opponent);
}
if (interaction.inCachedGuild()) {
const opponentPermissions = channel.permissionsFor(opponent);
}
Amgelo
Amgelo6mo ago
you should first check and then get the member
[DQ] Swat
[DQ] SwatOP6mo ago
Ahhhh fair
Amgelo
Amgelo6mo ago
because the member by that time is already a member in a non cached | cached guild
[DQ] Swat
[DQ] SwatOP6mo ago
Yup, that worked. Thanks so much again

Did you find this page helpful?