Discord bot in JS - Alert when a user join a channel

129 Views Asked by At

Here the problem: I want to create a discord bot that alert me when someone join a channel. Code works, and display in console the number of users, but not update the counter each time. I have to restart it to have the update number. How to reset the counter and have the updated one? Thanks

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });

client.once('ready', () => {
    console.log(`Bot connected ${client.user.tag}!`);
});
    setInterval(() => {
        const voiceChannel = client.channels.cache.get('MY VOCAL CHANNEL ID');
        console.log(`Number of people: ${voiceChannel.members.size}`);
            if (voiceChannel.members.size > 0) {
                console.log('Someone is here');
            } else {
                console.log('No one is here');
            }
    }, 1000);  
;
client.login('MY BOT TOKEN');

I tried to clear cache, to set voiceChannel.members.size = 0 every end of cycle. I want the updated number of members every cycle.

2

There are 2 best solutions below

1
sivan On

you need use a voiceStateUpdate something like blow

client.on('voiceStateUpdate', (oldState, newState) => {
const oldVoice = oldState.channelId;
const newVoice = newState.channelId;
    if(oldVoice === "CHANNEL_ID") {
        console.log('a user joined!')
    }
    if (newVoice === "CHANNEL_ID") {
        console.log('a user left!')
    }
});
0
user3082438 On

I used and adapted sivan answer:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildVoiceStates] });

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

client.on('voiceStateUpdate', (oldState, newState) => {
    const channelIdToMonitor = 'CHANNELIDTOMONITOR'; // Channel ID to monitor
    const textChannelIdToSend = 'CHANNELIDTOSENDMESSAGE'; // Channel ID to send message
    const guild = newState.guild; // server state (guild) 
    const roleToMention= guild.roles.cache.find(role => role.name === 'Admin');

    if (newState.channelId === channelIdToMonitor) {
        const memberTag = newState.member.user.tag;
        const timestamp = new Date().toLocaleTimeString();
        const message = `${roleToMention},    ${memberTag}    is on channel ${timestamp}`;

        // use the text channel to send the message
        const textChannel = guild.channels.cache.get(textChannelIdToSend);

        if (textChannel) {
            textChannel.send(message);
        }
    }

    if (oldState.channelId === channelIdToMonitor && newState.channelId !== channelIdToMonitor) {
        const memberTag = oldState.member.user.tag;
        const timestamp = new Date().toLocaleTimeString();
        const message = `    ${memberTag}    exit ${timestamp}`;

        //  use the text channel to send the message
        const textChannel = guild.channels.cache.get(textChannelIdToSend);

        if (textChannel) {
            textChannel.send(message);
        }
    }
});

client.login('CLIENTBOTID');