guild.voiceStates.cache force update?

When trying to get all users in a voicechannel, they are still in the cache after leaving. Is it possible to get an accurate count of users in voice channels?
function getActiveUsers(server) {
return server?.voiceStates?.cache.map(channel => channel?.member?.user?.username);
}
function getActiveUsers(server) {
return server?.voiceStates?.cache.map(channel => channel?.member?.user?.username);
}
12 Replies
d.js toolkit
d.js toolkit•7mo 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
CheeseGroveCam
CheeseGroveCam•7mo ago
I have both these intents. (and more but these are the relevant ones)
GatewayIntentBits.GuildVoiceStates,
641
GatewayIntentBits.GuildVoiceStates,
641
Could having more intents cause issues? I've just added a bunch without thinking about it
Syjalo
Syjalo•7mo ago
Voice states have channelId property, which is null if the user isn't in a channel
CheeseGroveCam
CheeseGroveCam•7mo ago
Yeah sorry for the confusion. Maybe I should named it voiceChannel instead. But I will try cleaning up some of the intents and see if that helps. Unfortunetely I need the user for what I'm doing, not only the count. Sorry for the confusion.
Syjalo
Syjalo•7mo ago
What you called channel is a voice state and it should be called voiceState or just state. It's not a voice channel, not even a channel. Voice states have the user info too
CheeseGroveCam
CheeseGroveCam•7mo ago
From what I can tell it is a channel. (Channel is maybe not the correct word from API standpoint, but from discord POV it's a voice channel) The channel contains a voiceState object. No sorry, I missread. It's a guild Yeah sorry for the confusion. But eitherway the issue is still the same. Getting the user info from here is not the issue, it's that it's cached and doesn't update correctly
Syjalo
Syjalo•7mo ago
It updates correctly Did you read my reply?
CheeseGroveCam
CheeseGroveCam•7mo ago
Maybe it's that I'm not understanding what a VoiceState is. I have assumed that a voice channel == VoiceState. That is why I called it a channel I don't understand the use of this channelId. If multiple users are connected to the VS, then the same channelId is just outputted once for each user
d.js docs
d.js docs•7mo ago
:property: VoiceChannel#members The members in this voice-based channel
CheeseGroveCam
CheeseGroveCam•7mo ago
Exactly. Which I am able to, just that when someone leaves, they are still in the cache
Syjalo
Syjalo•7mo ago
Yes, and if the user isn't in a channel, then the channelId will be null. So after leaving the channel the user's state updates and you can filter by the channelId to get all members in the guild that are in voice channels Then why did you ask this in your initial message?
Is it possible to get an accurate count of users in voice channels?
CheeseGroveCam
CheeseGroveCam•7mo ago
I see! Yeah that makes sense. I will look through this voiceChannel instead. But I don't see it in the Guild. Where is this found? Alright, I found it in the documentation under "channels". Sorry for the confusion guys but thanks for the help. This should do it 🙂
Want results from more Discord servers?
Add your server