【问题标题】:How to make my bot leave a voice channel if there is no one in the channel如果频道中没有人,如何让我的机器人离开语音频道
【发布时间】:2021-07-06 03:49:47
【问题描述】:

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


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

我对此进行了测试,但它不起作用,有人可以帮助我吗?谢谢!

【问题讨论】:

    标签: discord discord.js bots radio


    【解决方案1】:

    试试这个:

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

    cmets 应该是不言自明的

    【讨论】:

      猜你喜欢
      • 2018-06-16
      • 2016-11-07
      • 2021-04-24
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2020-10-24
      • 2020-08-30
      相关资源
      最近更新 更多