title: Discord py - music bot queue command
body: <p><strong>Hey guys,</strong></p>
  <p>I have two little problems I dont know how to solve.</p>
  <p><strong>What do I want to do?</strong></p>
  <p>I want to create a pretty simple music bot with a few functions.</p>
  <ol>
  <li>Play music from a URL</li>
  <li>Play music from a Title</li>
  <li>Play music from a Queue</li>
  </ol>
  <p>Now my problems:</p>
  <p>Everytime I use</p>
  <blockquote>
  <p>?play TITLE or URL</p>
  </blockquote>
  <p>it skips the currently playing song - I want the bot to wait until the song is finished</p>
  <p>and my second problem is pretty much the same - I want the bot to play a queue and wait until every song is finished - now he loops through the list of songs and only plays the first :(</p>
  <p><strong>What does my code look like:</strong></p>
  <pre class="lang-py prettyprint-override"><code>import discord
  import asyncio
  import os
  import youtube_dl
  
  import urllib.parse, urllib.request, re
  import requests
  
  from discord.ext import commands
  from discord import Embed, FFmpegPCMAudio
  from discord.utils import get
  
  '''
  
  INSTALLING YOUTUBE-DL
  
  pip install -U git+https://github.com/l1ving/youtube-dl
  
  '''
  
  queue = []
  
  youtube_dl.utils.bug_reports_message = lambda: ''
  
  ytdl_format_options = {
      'format': 'bestaudio/best',
      'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
      'restrictfilenames': True,
      'noplaylist': True,
      'nocheckcertificate': True,
      'ignoreerrors': False,
      'logtostderr': False,
      'quiet': True,
      'no_warnings': True,
      'default_search': 'auto',
      'source_address': '0.0.0.0'
  }
  
  ffmpeg_options = {
      'options': '-vn'
  }
  
  ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
  
  class YTDLSource(discord.PCMVolumeTransformer):
      def __init__(self, source, *, data, volume=0.5):
          super().__init__(source, volume)
  
          self.data = data
  
          self.title = data.get('title')
          self.url = data.get('url')
  
      @classmethod
      async def from_url(cls, url, *, loop=None, stream=False, play=False):
          loop = loop or asyncio.get_event_loop()
          data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream or play))
  
          if 'entries' in data:
              data = data['entries'][0]
  
          filename = data['url'] if stream else ytdl.prepare_filename(data)
          return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
  
  
  class Music(commands.Cog):
      def __init__(self, bot):
          self.bot = bot
  
      @commands.command()
      async def join(self, ctx):
  
          if not ctx.message.author.voice:
              await ctx.send(&quot;You are not connected to a voice channel!&quot;)
              return
          else:
              channel = ctx.message.author.voice.channel
              await ctx.send(f'Connected to ``{channel}``')
  
          await channel.connect()
  
      @commands.command()
      async def play(self, ctx, *, url):
  
          try:
  
              async with ctx.typing():
                  player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
                  ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
  
              await ctx.send(f':mag_right: **Searching for** ``' + url + '``\n&lt;:youtube:763374159567781890&gt; **Now Playing:** ``{}'.format(player.title) + &quot;``&quot;)
  
          except:
  
              await ctx.send(&quot;Somenthing went wrong - please try again later!&quot;)
  
      @commands.command()
      async def play_queue(self, ctx):
  
          for url in queue:
  
              try:
  
                  async with ctx.typing():
                      player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
                      ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
  
                  await ctx.send(f'&lt;:youtube:763374159567781890&gt; **Now Playing:** ``{url}``')
  
              except:
  
                  await ctx.send(&quot;Somenthing went wrong - please try again later!&quot;)
              
          else:
  
              await ctx.send(&quot;Queue is now done!&quot;)
  
      @commands.command()
      async def pause(self, ctx):
          voice = get(self.bot.voice_clients, guild=ctx.guild)
  
          voice.pause()
  
          user = ctx.message.author.mention
          await ctx.send(f&quot;Bot was paused by {user}&quot;)
  
      @commands.command()
      async def resume(self, ctx):
          voice = get(self.bot.voice_clients, guild=ctx.guild)
  
          voice.resume()
  
          user = ctx.message.author.mention
          await ctx.send(f&quot;Bot was resumed by {user}&quot;)
  
      @commands.command()
      async def add_queue(self, ctx, url):
  
          global queue
  
          try:
              queue.append(url)
              user = ctx.message.author.mention
              await ctx.send(f'``{url}`` was added to the queue by {user}!')
          except:
              await ctx.send(f&quot;Couldnt add {url} to the queue!&quot;)
  
      @commands.command()
      async def remove_queue(self, ctx, number):
  
          global queue
  
          try:
              del(queue[int(number)])
              if len(queue) &lt; 1:
                  await ctx.send(&quot;Your queue is empty now!&quot;)
              else:
                  await ctx.send(f'Your queue is now {queue}')
          except:
              await ctx.send(&quot;List index out of range - the queue starts at 0&quot;)
  
      @commands.command()
      async def clear_queue(self, ctx):
  
          global queue
  
          queue.clear()
          user = ctx.message.author.mention
          await ctx.send(f&quot;The queue was cleared by {user}&quot;)
  
      @commands.command()
      async def view_queue(self, ctx):
  
          if len(queue) &lt; 1:
              await ctx.send(&quot;The queue is empty - nothing to see here!&quot;)
          else:
              await ctx.send(f'Your queue is now {queue}')
  
      @commands.command()
      async def leave(self, ctx):
          voice_client = ctx.message.guild.voice_client
          user = ctx.message.author.mention
          await voice_client.disconnect()
          await ctx.send(f'Disconnected from {user}')
  
      @play_queue.before_invoke
      @play.before_invoke
      async def ensure_voice(self, ctx):
          if ctx.voice_client is None:
              if ctx.author.voice:
                  await ctx.author.voice.channel.connect()
              else:
                  await ctx.send(&quot;You are not connected to a voice channel.&quot;)
                  raise commands.CommandError(&quot;Author not connected to a voice channel.&quot;)
          elif ctx.voice_client.is_playing():
              ctx.voice_client.stop()
  
  def setup(client):
      client.add_cog(Music(client))
  </code></pre>
  <p>Thanks for the help</p>