我最近开始研究这个音乐机器人,但我遇到了队列系统的问题。我请求多个内容,但最终只播放两个内容。
代码:
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;
队列变量一开始是一个空数组。
最佳答案
按照您的逻辑,预计只会播放 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();
}
});
}
...
关于javascript - 队列有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51660697/