【问题标题】:How do I disconnect the bot when the user that summoned it leaves?当召唤它的用户离开时,我如何断开机器人的连接?
【发布时间】:2021-07-03 01:04:41
【问题描述】:

我创建了一个 Discord 机器人,它跟踪 vc 周围的用户并加入他们,当他们离开时如何断开音频流?代码如下:

client.on('voiceStateUpdate', (oldMember, newMember) => {
  console.log(newMember.channelID)
 
  let newUserChannel = newMember.channelID;
  const channel = client.channels.cache.get(newUserChannel);
  
  channel.join().then(connection => {
      const dispatcher = connection.play('audio.mp3',{ volume: 1 });

      dispatcher.on('start', () => {
        console.log('audio.mp3 is now playing!');
      });
  
      dispatcher.on('finish', () => {
        console.log('audio.mp3 has finished playing!');
        connection.disconnect();
      });
      // Always remember to handle errors appropriately!
    }).catch(e => {
        console.error(e);
    });
  }
);



【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    只需在您的代码中添加一条 if 语句,它会检查频道中是否还有一个 Discord 用户(机器人除外)。如果没有,请断开连接:

    if (oldMember.channel.members.size === 1) {
          connection.disconnect();
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 2021-04-13
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      相关资源
      最近更新 更多