Kareem Almasri
Kareem Almasri

Reputation: 1

Having trouble with a queue

I recently started working on this music bot, but I am having an issue with the queue system. I request multiple things, and it ends up playing only two things.
Code:

case "~play":

  if (queue.length == 0) {
    queue.push(args[1]);
    dispatcher = guild.voiceConnection.playStream(ytdl(queue[0], {
      filter: 'audioonly'
    })).on('end', () => {

      console.log('finished');
      queue.shift();

      guild.voiceConnection.playStream(ytdl(queue[0], {
        filter: 'audioonly'
      }));

    });
  } else queue.push(args[1]);

  break;

The queue variable is an empty array at the beginning.

Upvotes: 0

Views: 153

Answers (1)

miradham
miradham

Reputation: 2355

Playing only 2 items is expected as per your logic.

You logic allows to playStream only 2 times: 1st time when queue is empty and 2nd time when 1st play ends. There is no any action regarding 2nd play end.

Update logic to call playStream every time when playStream ends and queue is not empty, something like:

...
case "~play":

  if (queue.length == 0) {
    queue.push(args[1]);
    playNext();
  } else queue.push(args[1]);

  break;
...
function playNext() {
    dispatcher = guild.voiceConnection.playStream(ytdl(queue[0], {
      filter: 'audioonly'
    })).on('end', () => {

      console.log('finished');
      queue.shift();
      // if more songs in the queue call playNext()
      // this will allow you to playNext every time when playStream ends 
      if (queue.length > 0) {
          playNext(); 
      }    
    });
}
...     

Upvotes: 1

Related Questions