首页 > 解决方案 > 检查是否在机器人上播放音乐(从 youtube 流式传输)

问题描述

我刚刚创建了一个工作队列命令,但现在我坚持编辑播放命令

我试图让它如果正在播放音乐,播放命令只会将查询推送到队列并等待音乐完成播放然后转移队列以播放下一个。

我坚持如何检查机器人是否仍在从互联网上播放音乐。

//push function, stream and streamOptions is above from here
if (/*check whether a music is playing = false*/) {
   function dispatcher() {
       connection.playStream(stream, streamOptions)
       dispatcher.on('end', function () {
       lst.shiftlist(); //shift the queue
       if (lst.urllist[0]/*check if queue[0] contains*/) {
          dispatcher();
       }
       else {
          message.channel.send('Queue empty!');
          return;
       }
       })
   }
}
else if (/*check whether a music is playing = true*/) {
     message.channel.send('Added to queue!')
     console.log(url)
}

是否可以检查机器人是否仍在播放流?还是我可以使用其他任何方法来解决这个问题?

标签: node.jsdiscord.js

解决方案


在我的脑海中,最简单的方法是在音乐开始播放时切换某种状态,并在调度程序结束事件触发时将其关闭。就像是:

var playing = false
if (/*check whether a music is playing = false*/) {
   function dispatcher() {
       connection.playStream(stream, streamOptions)
       playing = true
       dispatcher.on('end', function () {
       lst.shiftlist(); //shift the queue
       if (lst.urllist[0]/*check if queue[0] contains*/) {
          dispatcher();
       }
       else {
          playing = false
          message.channel.send('Queue empty!');
          return;
       }
       })
   }
}
else if (/*check whether a music is playing = true*/) {
     message.channel.send('Added to queue!')
     console.log(url)
}

推荐阅读