0

So I'm making progress on my music bot. Currently, it shuffles the playlist, and plays every song in the playlist once. The next step is for me to make him re-shuffle the playlist after it has played every song, and go again (I'll make the catch to 'stop' him using a command later). In theory - all I have to do is add in a check that 'if the array contains an entry in the 0 position, play that next song', otherwise 'if the array was empty, shuffle the playlist again, and THEN play the next song'

Unfortunately, even if the next song is undefined - the check is failing, and after the first run-through of the playlist, he starts spamming songs that are undefined. I have tried multiple ways of checking this. Checking if the entry in the array is undefined, checking if the length of the array is undefined, checking whether the entry was or was not contained in the original list of songs - same problem for all of them.

function PlaySong(connection, channel, SongToPlay) {
        console.log('Now playing '+SongToPlay[0]+'.'); //announce the song
        message.channel.send('Now playing '+SongToPlay[0]+'.');
        const dispatcher = connection.play('./Scythe Digital Edition - Soundtrack/'+SongToPlay[0]+'.mp3'); //play the song
        dispatcher.setVolume(0.1); //at this volume
        dispatcher.on("finish", () => { //when the song concludes
        SongToPlay.shift(); //remove that song from the list of songs to play
            if (!isNullOrUndefined(SongToPlay[0]) || SongToPlay[0] !== '') { //if the list of songs is not empty
                PlaySong(connection, channel, SongToPlay); //repeat the above for the next song
            }
            else { //otherwise if it was empty
                SongToPlay = shuffle(testbells); //shuffle the playlist and repopulate the list of songs to play
                PlaySong(connection, channel, SongToPlay); //then repeat the above for the next song
                //channel.leave();
                //return;
            }
        });
    }

1 Answers1

1

It's a bit hard without seeing all your code so I've made a simple version which should suit - it just prints rather than playing anything or messaging but this should be a reasonable structure for you.

//define your starting song array
let originalSongArray = ["Abba","Beatles","ACDC","Britney","Elvis"];

//create a shuffled version of that array 
let shuffledArray = originalSongArray.slice();
shuffle(shuffledArray);

//set the number of songs you want to play 
//note how the number is higher than the array of available songs causing shuffles
let songsToPlay = 12;
while (songsToPlay > 0)
   {
       //"play" the song (in this case write to console)
       console.log("Playing song from, " + shuffledArray[0]);
       //"remove the song from the shuffled list
       shuffledArray.shift();

       //test to ensure there are still songs remaining
       if(shuffledArray.length == 0){
           //regenerate a new shuffled list from the original
           //note, we never altered the original list order or content
           shuffledArray = originalSongArray.slice();
           shuffle(shuffledArray);
       }
   //reduce number of songs to play
   songsToPlay--;
 }

A simple shuffle function referenced this question: How to randomize (shuffle) a JavaScript array?

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}
Mark Taylor
  • 1,128
  • 8
  • 15