blanning
blanning

Reputation: 61

How to detect when a user joins a voice channel?

I am currently trying to make a bot for my server and one of the things I want to do is have it detect whenever a user joins any voice channel and simply make it send a message. I can't personally figure it out or find any answers on the internet as most of the time people are detecting it based on a command while I want it to be passive. I know that voiceStateUpdate has been changed and some things are different from how I've seen others use it in the past.

If anyone has any solutions please let me know, thanks.

Upvotes: 5

Views: 20026

Answers (2)

stackers
stackers

Reputation: 3289


    client.on('voiceStateUpdate', (oldState, newState) => {
        if (newState.channelID === null) console.log('user left channel', oldState.channelID);
        else if (oldState.channelID === null) console.log('user joined channel', newState.channelID);
        else console.log('user moved channels', oldState.channelID, newState.channelID);
    });

Upvotes: 7

Worthy Alpaca
Worthy Alpaca

Reputation: 1245

In discord.js v.12 the listener you need to use is indeed voiceStateUpdate. It has the parameters oldState and newState. With those you can detect a number of things including the member object.

Using that you might use something like this to detect if a user or bot is connecting or disconnecting a voice channel.

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

    // the rest of your code
})

Upvotes: 2

Related Questions