Gianmarco
Gianmarco

Reputation: 832

Discord js check if a user joined or left the voice channel

How can i check if a user joined or left a vocal channel with the new version of discordj (v12.x)?

 client.on('voiceStateUpdate', (oldState, newState) => {
        if(userJOined){
         //do somethings
        }else{
         //do something else if the user left
       }

    })

Upvotes: 0

Views: 1329

Answers (1)

Lioness100
Lioness100

Reputation: 8402

All VoiceStates have a channelID property: the ID of the VoiceChannel they're already in or null. If oldState.channelID is null and newState.channelID isn't, you'll know that the member joined a voice channel. If it's the other way around, you'll know the member left a voice channel.

client.on('voiceStateUpdate', (newState, oldState) => {
 if (newState.channelID && !oldState.channelID) {
  console.log('Someone joined');
  // ...
 } else if (oldState.channelID && !newState.channelID) {
  console.log('Someone left');
  // ...
 } else {
  console.log('Neither of the two actions occured');
  // ...
 }
});

Upvotes: 1

Related Questions