JS 中的 Discord 机器人 - 当用户加入频道时发出警报

问题描述 投票:0回答:1

问题来了: 我想创建一个不和谐的机器人,当有人加入频道时它会提醒我。 代码有效,并在控制台中显示用户数量,但不会每次都更新计数器。我必须重新启动它才能获得更新号。如何重置计数器并更新计数器?谢谢

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');

我尝试清除缓存,在每个周期结束时设置 voiceChannel.members.size = 0。 我想要每个周期更新的成员数量。

javascript discord bots counter member
1个回答
0
投票

你需要使用

voiceStateUpdate
之类的东西

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!')
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.