【问题标题】:Having trouble with a queue排队遇到问题
【发布时间】:2019-01-10 15:26:03
【问题描述】:

我最近开始研究这个音乐机器人,但我遇到了队列系统的问题。我请求了多个东西,但它最终只播放了两个东西。
代码:

case "~play":

  if (queue.length == 0) {
    queue.push(args[1]);
    dispatcher = guild.voiceConnection.playStream(ytdl(queue[0], {
      filter: 'audioonly'
    })).on('end', () => {

      console.log('finished');
      queue.shift();

      guild.voiceConnection.playStream(ytdl(queue[0], {
        filter: 'audioonly'
      }));

    });
  } else queue.push(args[1]);

  break;

队列变量开头是一个空数组。

【问题讨论】:

  • 这里没有足够的信息来提供很大帮助,但我怀疑将 ref 发送到队列 [0] 之类的队列项到函数而不是变量的 ref,以防它存储队列引用而不是变量引用。就像,复制变量 ref 并发送它,所以 var x = queue[0] 然后用 x 调用函数

标签: javascript arrays discord.js


【解决方案1】:

根据您的逻辑,预计只播放 2 个项目。

您的逻辑只允许播放 2 次流:第 1 次队列为空,第 2 次第 1 次播放结束。没有关于第 2 次播放结束的任何动作。

更新逻辑以在每次 playStream 结束且队列不为空时调用 playStream,例如:

...
case "~play":

  if (queue.length == 0) {
    queue.push(args[1]);
    playNext();
  } else queue.push(args[1]);

  break;
...
function playNext() {
    dispatcher = guild.voiceConnection.playStream(ytdl(queue[0], {
      filter: 'audioonly'
    })).on('end', () => {

      console.log('finished');
      queue.shift();
      // if more songs in the queue call playNext()
      // this will allow you to playNext every time when playStream ends 
      if (queue.length > 0) {
          playNext(); 
      }    
    });
}
...     

【讨论】:

    猜你喜欢
    • 2020-06-29
    • 2021-07-10
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多