Jacob Tucker
Jacob Tucker

Reputation: 134

Fetching Discord Channel Members

I am attempting to fetch all the members of a certain Discord voice channel using discord.js

I have a function trying to do this right now. It takes in an interaction, that is given when a user runs a slash command on Discord. The options parameter is simply the channel they pass in to the command.

This is what running the command looks like

const execute = async (interaction, options) => {
  if (interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
    await interaction.guild.members.fetch();
    await interaction.deferReply({ ephemeral: true });
    const channelInput = options.getChannel('channel');
    const channel = await interaction.member.guild.channels.fetch(channelInput.id);
    const members = channel.members.map(member => member.id);
    console.log(members); // [ '143100912687251456', '143100912687251622' ]
  }
}

The issue is that it does not update properly if the user changes channels. If I restart the bot, and run the command, it executes perfectly. But if I were to change to another voice channel, it still thinks I am in the old one and not the new one.

This is probably because it is fetching some sort of cache. Does anyone know how to fetch their API directly instead of a local cache that isn't updating?

Upvotes: 2

Views: 1214

Answers (1)

Nanotect.
Nanotect.

Reputation: 61

Add GuildVoiceStates Intent

    intents: [
        GatewayIntentBits.GuildVoiceStates,
    ],

Upvotes: 2

Related Questions