title: How to detect when a user leaves a voice channel in Discord with discord.js V12
body: <p>I am making a Discord.js Bot on v12 that includes a mute command, that mutes the whole voice channel you are in. The problem is when somebody leaves the channel they stay muted. I am trying to fix that with a simple event to unmute the person, but I don't understand the <code>VoiceStateUpdate</code> and the <code>OldState</code> and <code>NewState</code>. I've searched widely, but I can only find one for joining a vc, not leaving. Here is what I got so far:</p>
  <p>Mute command:</p>
  <pre><code>    else if (command === 'mute') {
          message.delete()
          if (!message.member.roles.cache.has('') &amp;&amp; !message.member.roles.cache.has('')) {
              message.reply('You don\'t have permission to use this commmand!')
              .then(message =&gt; {
                  message.delete({ timeout: 5000 })
              }).catch();
              return;
          }
          if (message.member.voice.channel) {
              let channel = message.guild.channels.cache.get(message.member.voice.channel.id);
              for (const [memberID, member] of channel.members) {
              member.voice.setMute(true);
              }
          } else {
              message.reply('You need to join a voice channel first!')
              .then(message =&gt; {
                  message.delete({ timeout: 5000 })
              }).catch();
          }
      }
  </code></pre>
  <p>Unmute event:</p>
  <pre><code>client.on('voiceStateUpdate', (oldState, newState) =&gt; {
      if (oldState.member.user.bot) return;
  
      if (oldState.member.user !== newState.member.user) member.voice.setMute(false);
  });
  </code></pre>
  <p>Thanks for taking your time to help me! :)</p>