Search code examples
node.jsdiscord.js

dicord.js bot joins a channel when a specific user joins a channel


I have been having many issues having my bot join a channel whenever a specific user joins a channel. The idea is to have the bot follow around a specific user. What I have done is use VoiceState to check if users are joining and leaving a channel and trying to get the IDs of the members coming and going. I have gotten some progress using this method, but I am stuck and can't figure out if there is a better way to do this. Any help is appreciated :)

client.on('voiceStateUpdate', (oldState, newState) => {
    if (newState.channelId === null) {
        //if user leaves leave
        console.log('user left channel', oldState.channelId);
    }
    //if specified user joins bot joins
    else if (oldState.channelId === null) {
        console.log('user joined channel', newState.channelId);
        let users = newState.channel.members;
        let ids = users.keys();
        console.log(ids);

        if (//target id is in id's)
            {
                //then bot joins
            }
    }

Solution

  • The solution I found was using .member for voicestate and from there using .id hope this helped anyone with the same issue :)

    client.on('voiceStateUpdate', (oldState, newState) => {
            if (newState.channelId === null) {
                //if user leaves leave
                console.log('user left channel', oldState.channelId);
            }
            //if F0re joins bot joins
            else if (oldState.channelId === null) {
                console.log('user joined channel', newState.channelId);
        
                let member = newState.member;
        
                let targetID = member.id;
        
                if (targetID == TARGET) {
                    join.joinChannelViaChannel(newState.channel);
                }
            }
            else {
                console.log('user moved channels', oldState.channelId, newState.channelId);
            }
        });