0

I have recently been writing a Discord bot recently which plays local mp3 files. The function I've used to play the files looks like this.

conn.play(discord.FFmpegPCMAudio("path/to/file"))
while conn.is_playing():
    time.sleep(0.1)
await conn.disconnect()

Now I was thinking about setting up the bot for multiple servers. My problem is that if I want to play two different sounds on two different servers at the same time, the bot is currently only able to play them sequentially and not simultaneously. I thought about having multiple python instances for each server but it looks kind of hard to manage for N server N python instances. It's ok for me to have multiple mp3 files for each server so I don't lock any files. But using multiple python programs looks a bit complicated. Is there any easier solution?

Ruli
  • 2,592
  • 12
  • 30
  • 40
Kapuzinaa
  • 3
  • 2
  • I know that this isn't an answer to your question if you want it to be local, but could you not just upload the mp3 files online? then when someone calls that file all the bot has to do is go to that online link. – Insula Feb 03 '21 at 09:21
  • @Insula that would be my worst case because i want to be able to edit/cut the files and therefore have to store them myself :/ – Kapuzinaa Feb 03 '21 at 09:49

1 Answers1

0

Your problem arises from the use of time.sleep(). discord.py is an asynchronous library, which means that it handles being able to perform multiple functionalities at the same time to ensure that a bot is not blocked during its use, but adding a synchronous call like time.sleep() makes it stop its operations while that call lasts. As you are calling time.sleep() for the time the mp3 is playing, it is doing a synchronous operation in that function.

When you are working with asynchronous libraries it is best to look for the asynchronous version of other utilities as well so you do not fall into the temptation of using a blocking synchronous call within an asynchronous operation. In this case I recommend you to use await asyncio.sleep(0.1) for your purpose. You will need to import asyncio as well to use that function.

References:

Shunya
  • 2,344
  • 4
  • 16
  • 28