Franky
Franky

Reputation: 45

Discord.js join/leave voice channel notification in text channel

I'm trying to create a bot that will send a message to a specific text channel (vc-text for example) when someone joins a specific voice channel (VC1 for example).

Here's the bot.js code:

const Discord = require('discord.js');
const {token} = require('./auth.json');
const bot = new Discord.Client();

bot.login(token);

bot.once('ready', () =>{
    console.log(`Bot ready, logged in as ${bot.user.tag}!`);
})

bot.on('voiceStateUpdate', (oldMember, newMember) => {
    let newUserChannel = newMember.voiceChannelID
    let oldUserChannel = oldMember.voiceChannelID

    if(newUserChannel === 712677767333937284) {
        // User Joins a voice channel
        console.log("Joined VC1")

    } else if(newUserChannel !== 712677767333937284){
        // User leaves a voice channel
        console.log("Left VC1")

    }
})

Some ID's:

When I join VC1, I get the console message "Left VC1", and when I leave/join another, I also get the same console message.

I got the bot example from https://www.reddit.com/r/discordapp/comments/6p85uf/discordjs_any_way_to_detect_when_someone_enter_a/

Upvotes: 1

Views: 24131

Answers (1)

abc
abc

Reputation: 27

This should work

    bot.on('voiceStateUpdate', (oldMember, newMember) => {
       let newUserChannel = newMember.channelID;
       let oldUserChannel = oldMember.channelID;
    
       if(newUserChannel === "Channel id here") //don't remove ""
       { 
           // User Joins a voice channel
           console.log("Joined vc with id "+newUserChannel);
       }
       else{
           // User leaves a voice channel
           console.log("Left vc");
       }
    });

Upvotes: 1

Related Questions