1

I have a method which calls itself after an audio is finished as a discord bot. However since the method is async, I can't use the await keyword since I am using a lambda expression. How can I get around this?

async def play_next(self, voice_client, ctx):
    if self.queue:
        self.is_playing = True
        array  = (self.queue.pop(0))
        audio, filename = discord.FFmpegPCMAudio(array[0]), array[1]

    await ctx.send(f"Now playing: {filename}")
    voice_client.play(audio, after=lambda e: await (self.play_next(voice_client, ctx)))
    else:
        self.is_playing = False

I tried using __anext__() but didn't work out.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • See [this question](https://stackoverflow.com/q/40746213/9731347). – SanguineL Jun 19 '23 at 12:39
  • 1
    Does this answer your question? [How to use await in a python lambda](https://stackoverflow.com/questions/40746213/how-to-use-await-in-a-python-lambda) – mkrieger1 Jun 19 '23 at 12:43
  • The answers in the forum didn't solve my problem. I worked my way around it so that I don't have to use the await keyword. – Simant Singh Jul 02 '23 at 20:37

1 Answers1

0

use the await keyword within a lambda expression, you can define a separate async function that calls self.play_next() and use that as the callback for voice_client.play().

async def play_next(self, voice_client, ctx):
    if self.queue:
        self.is_playing = True
        array = self.queue.pop(0)
        audio, filename = discord.FFmpegPCMAudio(array[0]), array[1]

        await ctx.send(f"Now playing: {filename}")

        async def callback(e):
            await self.play_next(voice_client, ctx)

        voice_client.play(audio, after=callback)
    else:
        self.is_playing = False
Shanu
  • 124
  • 4