From abe268391f3f6b3b98da8c13995ec24817219c42 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 26 Sep 2021 01:43:49 +0200 Subject: [PATCH 1/3] Add try/except --- src/bot.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/bot.py b/src/bot.py index ac85f26..0d2cde4 100644 --- a/src/bot.py +++ b/src/bot.py @@ -9,7 +9,7 @@ from dotenv import load_dotenv import youtube_dl from error import ErrorHandler -from message import NowPlayingMessage, QueuedMessage +from message import NowPlayingMessage, QueuedMessage, ErrorMessage load_dotenv() @@ -57,19 +57,27 @@ class Music(commands.Cog): @tasks.loop() async def _handle_playback(self): while True: - await self._queue_lock.acquire() - ctx, song = await self._queue.get() - audio = discord.FFmpegPCMAudio(song.audio_url, **self._ffmpeg_options) + try: + await self._queue_lock.acquire() + ctx, song = await self._queue.get() + audio = discord.FFmpegPCMAudio(song.audio_url, **self._ffmpeg_options) - def after(err): - if err: - print(f"Player error: {err}") + def after(err): + if err: + print(f"Player error: {err}") + self._queue.task_done() + self._queue_lock.release() + ctx.voice_client.play(audio, after=after) + embed = NowPlayingMessage(title=song.title, url=song.webpage_url) + await ctx.send(embed=embed) + # pylint: disable=broad-except + except Exception as err: + print(f"Error during playback: {err}") + if ctx: + embed = ErrorMessage(str(err)) + await ctx.send(embed=embed) self._queue.task_done() self._queue_lock.release() - ctx.voice_client.play(audio, after=after) - - embed = NowPlayingMessage(title=song.title, url=song.webpage_url) - await ctx.send(embed=embed) @_handle_playback.before_loop async def before_handle_playback(self): From 94d018246c01483e71d0e5497743d6692010c3e0 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 26 Sep 2021 01:51:59 +0200 Subject: [PATCH 2/3] Skip song if voice_client is None --- src/bot.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/bot.py b/src/bot.py index 0d2cde4..df7e1d2 100644 --- a/src/bot.py +++ b/src/bot.py @@ -60,6 +60,13 @@ class Music(commands.Cog): try: await self._queue_lock.acquire() ctx, song = await self._queue.get() + if ctx.voice_client is None: + # Bot is no longer in a voice channel. + # This could be the case because a stop command was issued. + # We will skip this (and possibly all remaining songs) in the queue + self._queue.task_done() + self._queue_lock.release() + continue audio = discord.FFmpegPCMAudio(song.audio_url, **self._ffmpeg_options) def after(err): From 45244896c82d2b68f5d1d732b2b31c76bdced405 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 26 Sep 2021 01:56:05 +0200 Subject: [PATCH 3/3] Refactor code with self._next() --- src/bot.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/bot.py b/src/bot.py index df7e1d2..c6a5771 100644 --- a/src/bot.py +++ b/src/bot.py @@ -54,6 +54,12 @@ class Music(commands.Cog): # pylint: disable=no-member self._handle_playback.cancel() + def _next(self): + """Trigger playback of next song.""" + self._queue.task_done() + if self._queue_lock.locked(): + self._queue_lock.release() + @tasks.loop() async def _handle_playback(self): while True: @@ -64,16 +70,14 @@ class Music(commands.Cog): # Bot is no longer in a voice channel. # This could be the case because a stop command was issued. # We will skip this (and possibly all remaining songs) in the queue - self._queue.task_done() - self._queue_lock.release() + self._next() continue audio = discord.FFmpegPCMAudio(song.audio_url, **self._ffmpeg_options) def after(err): if err: print(f"Player error: {err}") - self._queue.task_done() - self._queue_lock.release() + self._next() ctx.voice_client.play(audio, after=after) embed = NowPlayingMessage(title=song.title, url=song.webpage_url) await ctx.send(embed=embed) @@ -83,8 +87,7 @@ class Music(commands.Cog): if ctx: embed = ErrorMessage(str(err)) await ctx.send(embed=embed) - self._queue.task_done() - self._queue_lock.release() + self._next() @_handle_playback.before_loop async def before_handle_playback(self):