From abe268391f3f6b3b98da8c13995ec24817219c42 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 26 Sep 2021 01:43:49 +0200 Subject: [PATCH] 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):