How can I queue songs in my discord music bot?

  discord, discord.py, python

I am working on a discord music bot and I need help queuing songs. It streams audio from YouTube directly into the VC it is connected to. What I need here is to wait for the current song to stop and then execute the user command for the second song and so on.

    @commands.command()
    if not ctx.author.voice:
        return await ctx.send('You are not in a voice channel.')
    async def play(self, ctx, url):
        print("command active")
        channel = ctx.message.author.voice.channel
        voice = get(self.client.voice_clients, guild=ctx.guild)
        if voice and voice.is_connected():
            await voice.move_to(channel)
        else:
            voice = await channel.connect()
            FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
            ydl_opts = {'format': 'bestaudio'}
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(url, download=False)
                URL = info['formats'][0]['url']
            voice.play(discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))

Source: Python Questions

LEAVE A COMMENT