javascriptdiscorddiscord.jsbotsmute

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


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 VoiceStateUpdate and the OldState and NewState. I've searched widely, but I can only find one for joining a vc, not leaving. Here is what I got so far:

Mute command:

    else if (command === 'mute') {
        message.delete()
        if (!message.member.roles.cache.has('') && !message.member.roles.cache.has('')) {
            message.reply('You don\'t have permission to use this commmand!')
            .then(message => {
                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 => {
                message.delete({ timeout: 5000 })
            }).catch();
        }
    }

Unmute event:

client.on('voiceStateUpdate', (oldState, newState) => {
    if (oldState.member.user.bot) return;

    if (oldState.member.user !== newState.member.user) member.voice.setMute(false);
});

Thanks for taking your time to help me! :)


Solution

  • You can't manipulate the voice state of a person that isn't on a voice channel, it would cause a DiscordAPIError: Target user is not connected to voice., and even if you could, you probably would still not want to, because even if you don't get another unhandled promise error of some sort, you would probably still get an infinite loop by checking if the New State's channel is null, because it would fire another event with null channel as soon as you unmute the person and so on.

    So, the way I see it right now, a possible solution to your problem, is to unmute whenever the user joins a channel. It might not be necessary to be this way if someone can figure out a better way to check if it is a channel leaving that is happening other than just using newState.channel === null. Whatever, if it's okay for you to unmute on join, you could do it this way:

    client.on('voiceStateUpdate', (oldState, newState) => {
      if (oldState.channel === null && newState.channel !== null) {
        newState.setMute(false);
      }
    });
    

    Notice though that this could bug if someone joins the channel exactly after leaving, otherwise, you should have no problems.