【发布时间】:2021-04-20 00:35:47
【问题描述】:
我正在尝试在 discord.js 中为我的机器人创建一个队列系统。当我使用命令添加 2 首歌曲进行测试时,它会将它们放入队列中,如下所示:
[
'https://www.youtube.com/watch?v=Jgv7qhDLPhg&ab_channel=BruhSoundEffects',
'https://www.youtube.com/watch?v=Jgv7qhDLPhg&ab_channel=BruhSoundEffects'
]
第一首歌曲播放效果很好并且可以正常工作,但是当涉及到过渡时,声音就停止了。控制台没有返回错误,可能是逻辑错误,但我不知道哪里出错了。
module.exports = class Youtube {
constructor(){
this.playQueue = [];
this.dispatcher = null;
}
playYoutubeMusic(channel, message, args, volume = 0.2) {
if(!args[0]) return message.channel.send('no link provided');
if(!this.validYtLink(args[0])) return message.channel.send('Invalid yt link');
console.log(this.playQueue.length);
const _this = this;
if(this.playQueue.length == 0){
this.playQueue.push(args[0]);
console.log(this.playQueue.length);
channel.join().then(connection => {
this.play(false, connection);
this.dispatcher.setVolume(volume);
this.dispatcher.on("finish", function () {
console.log("test");
console.log(_this.playQueue);
if (_this.playQueue.length != 0) {
_this.play(true, connection);
}
else connection.disconnect();
});
});
} else {
this.playQueue.push(args[0]);
console.log(this.playQueue);
}
}
play(shift = false, connection){
console.log("test");
if(shift) this.playQueue = this.playQueue.shift();
this.dispatcher = connection.play(ytdl(this.playQueue[0], { quality: 'highestaudio' }));
}
-
channel来自const channel = client.channels.cache.get("626133529130172432"); -
message来自client.on("message", function(message) -
args在这种情况下是链接
我只是不明白第一首歌曲如何正常播放而没有任何问题,但是当谈到第二首歌曲时,机器人只是停止了,但控制台中没有错误。
【问题讨论】:
标签: javascript node.js discord.js