'calling await function in "after = lambda"

I'm currently working on a discord bot (python 3.8.10) that plays music in a voice channel. I have a music queue and a function that plays the first song in the queue, deletes it and then calls itself when the song ends. The problem is that lambda functions must be non-await and I really don't know how to avoid this problem. Thank you for your helping

# plays next song in the queue
    # this function can be called from
        # the play command if the user adds a song to the music queue (and the bot is not playing anything)
        # the play_music() function when the previous song ends
        # the skip command (called by the user)
    async def play_music(self, ctx):
        try:
            if len(self.music_queue) > 0:
                # music queue is a list, each element composed by a playlist (dictionary) and a voice channel
                # so [0] means the first song, [0] means the playlist, ['source'] the value of source in the disctionary
                song_url = self.music_queue[0][0]['source']
                self.now_playing = self.music_queue[0]
                # removes the first element because it is about to be played
                self.music_queue.pop(0)
                if not self.voice_channel.is_playing():
                    # here's the error!!!
                    self.voice_channel.play(discord.FFmpegPCMAudio(song_url, **self.FFMPEG_OPTIONS), after = lambda e: self.play_music(ctx))
                # transforms the volume
                self.voice_channel.source = discord.PCMVolumeTransformer(self.voice_channel.source, volume=self.volume)
                # gets the time stamp of the playing song
                self.music_position = (datetime.datetime.now() - datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
                self.is_playing = True
            else:
                self.is_playing = False
        except youtube_dl.DownloadError or HTTPError:
            # TODO: send message
            print('download error')
            return

The exception i have is:

RuntimeWarning: coroutine 'music_cog.play_music' was never awaited

P.S.=Strange thing is, it works anyway, although i get an error!



Solution 1:[1]

I actually found a solution here: how-to-use-await-in-a-python-lambda

In my case, that's how I rewrote my code:

self.voice_channel.play(discord.FFmpegPCMAudio(song_url, **self.FFMPEG_OPTIONS),
                                        after = lambda ctx: (await self.play_music(ctx) for _ in '_').__anext__())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Liuk23