How to query members of a voice channel
Hello,
i would like to query all the members of a certain voice channel.
But unfortunately i only get an empty list of members.
I had the same thing with channels, before i added the "View Channel/Message" Permission on the Discord Developer Site".
I query the channels by:
i filter the channel list for voiceChannels beforehand:
const voiceChannels = channels.filter(channel => channel.type === ChannelType.GuildVoice)
And i check for members by:
voiceChannels.first()?.members.size
or
voiceChannels.first()?.members.has("someID")
But i always get an empty collection.
I have searched quite some time on google but were only able to find results like "You need Members Intent"
Do i really? Its a complete overkill, cause i just wanna read the members and their id and nothing more.
I hope you can help me with your expierince.
Because this question is super explicit, i hope you can help me even though this might be discord api related :peepoLoveMonbrey:
Thank you guys! Have a good day š
Details:
Node version: v20.6.1
discordjs: 14.14.1 (i cant change the label afterwards no?)35 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!
- ā
Marked as resolved by OPOn login of the bot? Or the player? So if the bot joins the members of voices will be retrieved just once?
So VoiceChannel#members only retrieves cached members?
can you give me a short code examples?
Thank you so much! ā¤ļø
By using "GuildVoiceStates" intents, the bot will cache every player/user/member present in a voice chat.
In the past I was doing something like this :
ā can not be the best way to do it anymore
ah i see, thanks for explaining
Do i need to call fetch on channel cache or can i just do something like:
const channels = interaction.guild?.channels.fetch()
will this automatically use the cache?Fetch is not using cache, as fetch is used to require real-time data
But this only works if the bot already ran before the user joint the voice channel no? If it happened beforehand, the bot wouldnt know
How often will this cache be updated?
The bot will cache every users present in a voice channel on starting.
okay.
So for my understanding:
Variant 1:
Bot starts. Requests all Members in Voice Channels. And continues updating the list if someone left a voice channel or joint, correcT?
or
Variant 2:
Bot starts. Starts to listening for all new changes in Voice channels (Joins/Leaves))
Yes for the first part and I think so for the second one
i see
I only know that fetch is for requiring data in real-time (if not cached).
But yeah, I think so, cache is getting updated every second the bot is online
Okay after reading a bit, cache will slowly populate as events begin to emit.
I see. So there is quite a difference.
I tried using the cache for channels too. But there were empty aswell, until i realised that a permission, could let me at least fetch them.
So i could use intents, that would track every channel update etc.
Same for members in voice channels etc. right?
So I don't think players connected in voice chat before the bot was online can be (or are) cached.
The best way to do is to
cache.get
and if it's empty or undefined use fetchi see
Thank you so much buddy
just got a " I SEE MOMENT"
@lwz will .fetch automatically cache?
Tbh, I think so, not sure :]
would make sense tbh
Yes, i would say yes
But don't want to say something wrong
It makes me read all the DJS guide :]
SOLUTION
Query Members in Voice Channel
NOTICE
Use the "GuildVoiceStates" Intent, so the Bot automatically receives joins/leaves Events.
Keep in mind:
If you have the intent
GuildVoiceStates
active.
You will only cache members that joint the channel after the bot were started
i couldnt find answers to my question in the djs docs. Well tbh i obv didnt search through all of them šyes
The guide was very helpful
For cache and fetch I already found something on reddit, but also in this Discord
Thank you sir
yea agree on that one, but not for my initial question.
Yeah I see š Good luck for what you're coding!
Thank you for your patience š Have a good day
what exactly do u want to achieve
In short:
My bot searches for teammates for several games. After matching i want to search for the Voice Channel of the teamleader so others can just click(link it in the matching response) on the channel and join more comfortable
You're welcome! You too
yeah so iterate through every vc check if the member is present
thats exactly what i do. This wasnt my problem. My Problem was that i whenever i checked for members in a voice channel it always returned empty collection even though it hat members in it
As proposed here:
https://discord.com/channels/222078108977594368/1232956485819170846/1232956485819170846
GuildVoiceStates intent must be there
i suppose its already fixed then
Yea someone already told me that.
Just wanted to close it just a second before you wrote something. You still answered another question i had! Thanks for that! I really appreciate that ā¤ļø
Im currently formatting the solution so people after me can find this easier
dont mention it... good luck with your bot š
What should i not mention? sry š
doesnt matter lol good luck
thanks ā¤ļø