I would solve the problem by building a queue that goes from the first to the tenth song
// I chose a for loop just because I can break out of it
// This goes on until it reaches the end of the queue or the tenth song, whichever comes first
for (let i = 0; i < g.playQueue.length && i < 9; i++) {
let song = g.playQueue[i];
// This is your part
let ytLink = ytBaseUrl + song.id;
let title = song.title;
if (title.length > 30) title = title.substring(0, 19) + "... ";
// Instead of directly adding the next line to q, I store it in another variable
let newline = "";
newline += "`" + (i+1) + "`. ";
newline += `[${title}](${ytLink}) | `;
newline += "`" + song.length + "`\n";
// If the sum of the two lengths doesn't exceed the 1024 chars limit, I add them together
// If it is too long, I don't add it and exit the loop
if (q.length + newline.length > 1024) break;
else q += newline;
}
This should fix it, since q
should not exceed 1024 characters. This is a simple but effective solution in my opinion.
Feel free to let me know if something doesn’t work properly 😉
6
solved Setting a maximum size for a music queue