I’m trying to get all users connected to a voice channel on my server. When someone talks to a bot in #general
, I want to get the users inside Voice Channel 1
.
I’m using Node 17 and DiscordJS 13.
This is my code:
message.guild.channels .fetch(channelID, { cache: false, force: true }) .then((channels) => { console.log(channels.members); });
Also, I tried this one:
let voiceChannel = client.guilds.cache .get(process.env.DISCORDJS_GUILD_ID) .channels.cache.get(process.env.DISCORDJS_CHANNEL_ID); let membersInChannel = voiceChannel.members; console.log(membersInChannel);
But, it always returns the voice channel users that joined when I start the node app. If someone leaves the voice channel, it keeps showing him in the console.log
when I say something to the bot in #general
. How can I achieve this?
Advertisement
Answer
I’ve had the same problem. Although I added { force: true }
as the option in fetch
, it always showed the same number of members when I started the app, ignoring the ones who joined/left the channel.
Enabling the GUILD_VOICE_STATES
intent solved this. So don’t forget to add it to your client
:
const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, ], });