Search code examples
pythonasynchronousdiscord.pypydub

Export file asynchronously


I am trying to save a song edited using pydub in an await expression (because it's a Discord bot) but it always blocks the gateway and stops the bot from sending anything.

I tried

await song.export("audios/"+filename+".ogg", format="ogg")

but it gives me

TypeError: object _io.BufferedRandom can't be used in 'await' expression

Solution

  • Try this:

    import asyncio
    def SongExport(Arg1,Arg2):#example
        song.export("audios/"+filename+".ogg", format="ogg")
        return 1#example
    @bot.tree.command(name="exportSong")
    def exportSong(interaction: discord.Interaction)
    blocking_coro = asyncio.to_thread(SongExport, Arg1, Arg2)
    result = await blocking_coro
    

    Let me know how it goes!