Discord.js | How to get members currently connected to a Voice Channel?

KOseare

I'm trying to get the members who are currently connected to a specific voice channel. With some properties I could get them, but the problem I found was that the members who are listed aren't the ones who are currently connected but the ones who had been connected to the voice channel when the bot started running.

This is my code (it's shortened but is copied):

const {Client, Intents} = require('discord.js')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILD_MEMBERS] })
client.on('messageCreate', messagesHandler)

function messagesHandler (msg) {
  if (msg.author.bot) return false;
  console.log(`Msg received: ${msg.content}`)

  if (msg.content.startsWith('/')) {
    const msgArr = msg.content.split(' ')

    switch (msgArr[0]) {
      case '/list':
        listCommand(msg, msgArr)
        break
    }
  }
}

async function listCommand (msg, msgArr) {
  if(!msgArr[1]) {
    const voiceChannel = await getChannel(msg, 'Voice1', true).fetch()
    if (!voiceChannel) {
      msg.channel.send('ERROR: Voice1 channel not found')
    } else {
      await getCurrentChannelMembers(msg, voiceChannel)
    }
  }
}

function getChannel(msg, channel, voiceChannel = false) {
  return msg.guild.channels.cache.find(c => c.name === channel && (!voiceChannel || c.isVoice()))
}

async function getCurrentChannelMembers (msg, channel) {
  const fetchedChannel = await channel.fetch(true)
  const members = fetchedChannel.members
  console.log('Members: ', members)
}

Any help would be appreciated!

MrMythical

Your bot needs GUILD_VOICE_STATES intent

const client = new Client({ 
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES, 
    Intents.FLAGS.GUILD_PRESENCES,
    Intents.FLAGS.GUILD_MEMBERS,
    Intents.FLAGS.GUILD_VOICE_STATES
  ] 
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rust serenity discord: How to get members from a voice channel?

Discord.js Move members to different voice channel

Voice channel userlimit discord js

Discord.js How to make a command to get every member in my voice channel?

How do I get every minute every voice user in the channel? Discord.js v13

How do I get user's current voice channel using discord.js?

Discord.js - Get last user that left a voice channel

How would I convert my discord.py discord bots code that I use to get a list of members in a voice channel to the new discord.py version?

Discord.py - How would I get the ID of a Voice Channel?

How to get the ID of a given voice channel? - Discord.py

How to set a variable to a voice channel specified by a user - Discord.js

Is it possible to show by command how many voice channels my Discord bot is currently connected to?

Get id of voice channel where bot is connected to

How can you check voice channel id that bot is connected to? (discord.py)

Discord bot can't connected to voice channel (python)

discord.errors.ClientException: Already connected to a voice channel music bot

discord music bot error, ClientException: Already connected to a voice channel in this server

Joining a voice channel on ready (discord.js)

Muting an Entire Discord Voice Channel (JS)

discord.js voice channel member count

Discord js / Check if user is in an specific voice Channel

Discord.js Voice Channel with Slash Commands

User connected to voice channel?

How to detect when a user leaves a voice channel in Discord with discord.js V12

Get the permissions of a category Channel and set it to voice channel discord.py

Voice channel doesn't get deleted after timeout. Discord.JS 12

Mute everyone in a voice channel with a Discord Bot with Discord.js

How to get count of all members in all voice channels discord.py

How do I disconnect a user from a voice channel in discord.js?