I have a music library in .mp3 format stored in a Google Drive folder, with a bunch of music files I want to be able to play one after the other. I am able to read and stream each file individually, but when I try to "queue" all the files from the folder and play them one after the other, it won't wait till one stream (song) is done playing to play the next, and instead starts the next one immediately, which results in only the last song being played out of the entire folder. I'd assume I have to mess around with async/await which I have done earlier in discord.js development, or with Promises and Promise.all(), which I am not familiar with. Here's the relevant part of the code.
var folderId = "'the-folder-id'";
drive.files.list({
q: folderId + " in parents", // to get all the files in the folder
fields: 'files(id)'
}, (err, res) => {
if (err) throw err;
const files = res.data.files;
files.map(file => {
drive.files.get({
fileId: file.id,
alt: 'media'
},
{ responseType: "stream" },
(err, { data }) => {
message.member.voiceChannel.join().then(connection => {
const dispatcher = connection.playStream(data); // doesn't wait for this to finish to play the next stream (song)
}).catch(err => console.log(err));
});
});
});
Note that I have a command to make the bot leave the channel, so it's normal that there isn't any voiceChannel.leave()
in my code, as I don't want it to leave right after the songs have finished playing.
Any advice is welcome, thanks in advance!