如何使音乐机器人播放有限的歌曲播放列表?

时间:2020-08-01 11:58:01

标签: javascript node.js discord discord.js

在我的音乐bot上进一步构建...我正试图从让他播放一首歌然后离开,到让他播放有限的歌曲列表然后离开,来实现跳跃。

这不应与队列混淆-歌曲列表是预先确定且有限的。至少在目前,它不能被机器人添加或更改。该机器人确实会洗牌列表。

现在的问题是,他不是一首一首地播放列表中的歌曲,而是播放第一首歌曲,然后播放第二首歌曲……并停止播放。

我曾尝试根据SongToPlay数组的长度设置一个循环,但是所有要做的就是使该漫游器快速遍历每首歌曲(在上一首歌曲有时间播放之前),然后离开。

const connection = message.member.voice.channel.name;
            const channel = message.member.voice.channel;
            message.channel.send("Now playing Scythe OST in the "+connection+" channel.");
            var SongToPlay = shuffle(testbells);
            channel.join().then(connection => {
                console.log('Now playing '+SongToPlay[0]+'.');
                message.channel.send('Now playing '+SongToPlay[0]+'.');
                const dispatcher = connection.play('./Scythe Digital Edition - Soundtrack/'+SongToPlay[0]+'.mp3');
                dispatcher.setVolume(0.1);
                dispatcher.on("finish", () => {
                    SongToPlay.shift();
                    console.log('Now playing '+SongToPlay[0]+'.');
                    message.channel.send('Now playing '+SongToPlay[0]+'.');
                    connection.play('./Scythe Digital Edition - Soundtrack/'+SongToPlay[0]+'.mp3');
                    dispatcher.setVolume(0.1);
                });
                channel.leave();
            })
            .catch(console.error);

1 个答案:

答案 0 :(得分:2)

const connection = message.member.voice.channel.name; const channel = message.member.voice.channel; message.channel.send("Now playing Scythe OST in the "+connection+" channel.");

var SongToPlay = shuffle(testbells); channel.join().then(connection => {
    let currentSong = 0;
    const keepPlaying = () => {
        console.log(`Now playing ${SongToPlay[currentSong]}.`);
        message.channel.send(`Now playing ${SongToPlay[currentSong]}.`);
        const dispatcher =
        connection.play(`./Scythe Digital Edition - Soundtrack/${SongToPlay[currentSong]}.mp3`);
        dispatcher.setVolume(0.1);
        dispatcher.on("finish", () => {
            if (currentSong < SongToPlay.length - 1) {
                currentSong++;
                keepPlaying();
            }

        });
    }
    keepPlaying();
}).catch(console.error);