如果频道中没有人,如何让我的机器人离开语音频道

时间:2021-04-10 13:47:17

标签: discord discord.js bots radio

我最近创建了一个播放广播的不和谐机器人,我想知道如何确保在没有人的情况下机器人引导它。


  if (oldState.channelID !==  oldState.guild.me.voice.channelID || newState.channel)
    return;

  // otherwise, check how many people are in the channel now
  if (!oldState.channel.members.size === 1) 
    setTimeout(() => { // if 1 (you), wait five minutes
      if (!oldState.channel.members.size === 1) // if there's still 1 member, 
         oldState.channel.leave(); // leave
     }, 10);
});

我测试了这个,但它不起作用,有人可以帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

client.on('voiceStateUpdate', (oldState, newState) => {
    if (oldState.channelID !==  oldState.guild.me.voice.channelID || newState.channel){
        return(0); //If it's not the channel the bot's playing in
    }
    if((oldState.channel.members.size -1) <= 1){
        //The bot's still in channel so it's never be zero
        //So we'll use <=1
        oldState.channel.leave();  //Leave the channel
    }
});

如果您想在一段时间后触发它,请输入 setTimeout
或者:

client.on('voiceStateUpdate', (oldState, newState) => {
    if (oldState.channelID !==  oldState.guild.me.voice.channelID || newState.channel){
        return(0); //If it's not the channel the bot's playing in
    }
    if(!oldState.channel.members.size -2){
        //The bot's still in channel so it's never be zero
        //We'll use -2 this time
        oldState.channel.leave();  //Leave the channel
    }
});

评论应该是不言自明的