Very slow Discord bot to play music I have been writing a discord.py bot, running on replit.com. I gotten to the music bot part after finishing the queue part. My code is logically correct; however, the code is way too inefficient causing the bot to crash when someone adds another song to the queue. How can I program my bot to be more efficient? If my code must be completely different, then a short explanation would be appreciated.
The code for play:
@bot.command(name='play', aliases=['sing', 'p'])
async def _play(ctx, url : str):
    queueurls = [] #list with the urls
    vc = ctx.voice_client
    if not vc:
      channel = ctx.author.voice.channel
      await channel.connect()
    url = ctx.message.content
    url = ttourl(url) #the message is processed to an url
    queueurls.append(url) #add the url to the list
    voicec = ctx.voice_client
    if not voicec.is_playing(): #when nothing is going on
      while queueurls != []: #as long as the url list isn't empty I want the process to repeat
        voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
        url = queueurls[0] #url is the first item in the list
        queueurls.pop(0) #now erase the now used first item (url)
        with youtube_dl.YoutubeDL(ytdlopts) as ydl:
          ydl.download([url]) #download the song
        voice.play(discord.FFmpegPCMAudio(&quot;songstemp/song.mp3&quot;)) #play the song
        vcs = ctx.voice_client
        while vcs.is_playing: #wait for the song to be over
          continue
        if os.path.exists(&quot;songstemp/song.mp3&quot;):
          path = &quot;songstemp/song.mp3&quot;
          os.remove(path)
    else: #if the bot is already playing, then don't disturb the loops above
      pass
Prefer four-space indentation instead of a mix of four and two
queueurls should be queue_urls
else / pass is redundant and can be deleted
It's not a great idea to use a single, statically-named temporary file for your playback. Instead, use tempfile to generate you a random one-off temporary filename. It's possible that the inefficiency you see is actually some kind of contention around your single filename.
while queueurls != [] is better-stated as while len(queue_urls) &gt; 0.
while vcs.is_playing is a polling loop that's probably going to saturate your CPU. At the least, add a sleep(0.1) to the inside of your loop to release your thread from the OS scheduler. Better yet would be a blocking call to your audio library, though I don't know whether that's possible. Based on the documentation, you could wait until an after finalizer hook is called.