-1

I an trying to add a song to a list if there is a song currently playing and after the song is over, play the next song in the list, after that song is over, play the next...

I have not been able to find the syntax or keyword for "song playing" or "once song is done"

The song playing works fine also without the queue

What I have tried

@commands.command()
  async def play(self, ctx, song):
    queue =[]
    await self.join(ctx)
    FFMPEG_OPTIONS = {'before_options':'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options':'-vn'}
    YDL_OPTIONS = {'format':'bestaudio'}
    vc = ctx.voice_client
    if "https://" in song:
      songtype = "link"
    if songtype == "link":
      if queue == []:
        queue.append(song)
        playsong = queue.pop(0)
        with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
          info = ydl.extract_info(playsong, download=False)
          url2 = info['formats'][0]['url']
          source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
          vc.play(source)
      else:
#Dont know where to go from here
  • OK, so `if queue == []:` is just so you only have 1 song in the queue at any given time, right? Would be the same as `if len(queue) == 0:`... Can't you just recursively call your bot's play function? `vc.play(source) ; bot.play( ctx, queue[0] )` – Doyousketch2 Jun 22 '21 at 19:44
  • I want it to the play the song instantly if there is no queue. i have since changed it to ``if len(queue) == 0:`` and removed the ``queue.append`` and ``queue.pop``. However, how can I detect a song is playing so instead of playing the song it adds it to a queue. Once the song is over, it plays the first song in the queue – Pricysquirrl. Jun 22 '21 at 19:57
  • Also for the recursion I need to know when the song is over to remove the song from the queue do I not? – Pricysquirrl. Jun 22 '21 at 20:02
  • Oh OK. Keep your queue. I'm just wondering how you call your bot's play command. `async def play(self, ctx, song):` Can't you just call that once it completes `vc.play(source)`? Does `vc.play(source)` return a value? Set that to a variable, and test for it? – Doyousketch2 Jun 22 '21 at 20:04
  • yes, but I still dont know how to tell whether the song is over. Also, would calling the command again cause some issues if I want to add a "show queue" command later or smthn – Pricysquirrl. Jun 22 '21 at 20:08
  • Hmm, guess you could poll for something like `vc.status()` to see if it's stopped every couple seconds. Does `info = ydl.extract_info( ... )` return songlength in there? – Doyousketch2 Jun 22 '21 at 20:12
  • Might check the docs again tbh because this seems too complicated. There must be something more simple – Pricysquirrl. Jun 22 '21 at 20:14

1 Answers1

0

I'm not seeing a way to poll the currently playing stream. They're seperate threads that don't appear to do any reporting when complete. Maybe this'll do,

await vc .play( source )
bot .commands .play( ctx, queue[0] ):

But if not, you'd have to parse that info from ydl. On commandline, the option is --get-duration.

According to youtube-dl python library documentation when calling from Python, you pass it through ydl_opts as 'forceduration': opts .getduration,. 'Believe the syntax would be like this:

ydl_opts = { 'forceduration': True }
duration = youtube_dl .YoutubeDL( ydl_opts )

You'd have to nest that underneath your other ydl call, then await a timer that pauses that length of time. Could peruse discord bots on github or programcreek, see if they do it a different way.



Edit: Try this -- Playing a queue of audio tracks in discord.py

Doyousketch2
  • 2,060
  • 1
  • 11
  • 11