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:
const channels = interaction.guild?.channels.fetch() // interaction: ButtonInteraction | CommandInteraction
const channels = interaction.guild?.channels.fetch() // interaction: ButtonInteraction | CommandInteraction
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
d.js toolkit
d.js toolkitā€¢4mo 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! - āœ… Marked as resolved by OP
Sim3Xx
Sim3Xxā€¢4mo ago
On 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! ā¤ļø
lwz
lwzā€¢4mo ago
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 :
const guildChannels = await client.guilds.cache.get('my_guild_id').channels.fetch();

await guildChannels.forEach( (channel) => {
if (channel.type !== ChannelType.GuildVoice) return;
if (channel.members.size === 0) return;
console.log(channel.members);
// channel.members
});
const guildChannels = await client.guilds.cache.get('my_guild_id').channels.fetch();

await guildChannels.forEach( (channel) => {
if (channel.type !== ChannelType.GuildVoice) return;
if (channel.members.size === 0) return;
console.log(channel.members);
// channel.members
});
āš  can not be the best way to do it anymore
Sim3Xx
Sim3Xxā€¢4mo ago
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?
lwz
lwzā€¢4mo ago
Fetch is not using cache, as fetch is used to require real-time data
Sim3Xx
Sim3Xxā€¢4mo ago
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?
lwz
lwzā€¢4mo ago
The bot will cache every users present in a voice channel on starting.
Sim3Xx
Sim3Xxā€¢4mo ago
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))
lwz
lwzā€¢4mo ago
Yes for the first part and I think so for the second one
Sim3Xx
Sim3Xxā€¢4mo ago
i see
lwz
lwzā€¢4mo ago
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.
Sim3Xx
Sim3Xxā€¢4mo ago
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?
lwz
lwzā€¢4mo ago
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 fetch
Sim3Xx
Sim3Xxā€¢4mo ago
i see Thank you so much buddy just got a " I SEE MOMENT" @lwz will .fetch automatically cache?
lwz
lwzā€¢4mo ago
Tbh, I think so, not sure :]
Sim3Xx
Sim3Xxā€¢4mo ago
would make sense tbh
lwz
lwzā€¢4mo ago
Yes, i would say yes But don't want to say something wrong It makes me read all the DJS guide :]
Sim3Xx
Sim3Xxā€¢4mo ago
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 šŸ˜„
no diddy
no diddyā€¢4mo ago
yes
Sim3Xx
Sim3Xxā€¢4mo ago
The guide was very helpful
lwz
lwzā€¢4mo ago
For cache and fetch I already found something on reddit, but also in this Discord
Sim3Xx
Sim3Xxā€¢4mo ago
Thank you sir yea agree on that one, but not for my initial question.
lwz
lwzā€¢4mo ago
Yeah I see šŸ™‚ Good luck for what you're coding!
Sim3Xx
Sim3Xxā€¢4mo ago
Thank you for your patience šŸ™‚ Have a good day
no diddy
no diddyā€¢4mo ago
what exactly do u want to achieve
Sim3Xx
Sim3Xxā€¢4mo ago
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
lwz
lwzā€¢4mo ago
You're welcome! You too
no diddy
no diddyā€¢4mo ago
yeah so iterate through every vc check if the member is present
Sim3Xx
Sim3Xxā€¢4mo ago
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
no diddy
no diddyā€¢4mo ago
GuildVoiceStates intent must be there i suppose its already fixed then
Sim3Xx
Sim3Xxā€¢4mo ago
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
no diddy
no diddyā€¢4mo ago
dont mention it... good luck with your bot šŸ‘‹
Sim3Xx
Sim3Xxā€¢4mo ago
What should i not mention? sry šŸ˜„
no diddy
no diddyā€¢4mo ago
doesnt matter lol good luck
Sim3Xx
Sim3Xxā€¢4mo ago
thanks ā¤ļø