user3643309
user3643309

Reputation: 51

How can I check if someone is in a voice channel in DiscordJS V13?

I'm trying to check if the user who initiated a command is currently in a voice channel, and while after I run the bot my code works and I get the voice channel information back if I then LEAVE the channel and re-initiate the command I'm getting back the same voice channel information like it was cached? The only way to receive up-to-date information is to restart my bot entirely.

export const interactionListener = async (interaction, client) => {
  if (!interaction.isCommand()) return; 
  const { commandName } = interaction; 
  switch (commandName) {
    case 'play':
      let user = await interaction.member.fetch();
      let channel = await user.voice.channel;
      if (!channel) {
        interaction.reply('you are not in a voice channel');
      } else {
        interaction.channel.send('do something else');
      }
 }
};

and that code is running on an interactionCreate here

client.on('interactionCreate', async (interaction) => {
  interactionListener(interaction, client);
});

Upvotes: 2

Views: 3680

Answers (1)

user3643309
user3643309

Reputation: 51

FIXED: I needed another intent

Previous intents were:

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

NEW Intents are:

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

I was missing

Intents.FLAGS.GUILD_VOICE_STATES

Upvotes: 3

Related Questions