如果频道中没有人超过5分钟,如何使我的漫游器离开语音频道

时间:2020-10-07 10:01:03

标签: discord discord.js

所以最近我一直在制作一个音乐机器人来解决不和谐问题,但是如果没有人在语音通道中停留时间超过5分钟,我想让我的机器人离开某个频道。现在我的机器人仅通过stop命令断开连接

    
    if (!queue) return message.reply("There is nothing playing.").catch(console.error);
    if (!canModifyQueue(message.member)) return;

    queue.songs = [];
    queue.connection.dispatcher.end();
    queue.textChannel.send(`${message.author} ⏹ stopped the music!`).catch(console.error);
  }
};

有人可以帮我吗?我将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以使用voiceStateUpdate事件,该事件在有人加入或离开语音通道时发出(除其他外)。

client.on('voiceStateUpdate', (oldState, newState) => {

  // if nobody left the channel in question, return.
  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
     }, 300000); // (5 min in ms)
});

答案 1 :(得分:0)

您可以使用 voiceStateUpdate 事件,该事件会在有人加入或离开语音频道(以及其他内容)时发出。

client.on('voiceStateUpdate', (oldState, newState) => {

  // if nobody left the channel in question, return.
  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
     }, 300000); // (5 min in ms)
});