From f69e8fdb1a039c1b5f8f3e376d669188d4d22d73 Mon Sep 17 00:00:00 2001 From: bobloy Date: Mon, 21 Sep 2020 11:35:35 -0400 Subject: [PATCH] Handle track errors gracefully --- audiotrivia/audiosession.py | 21 +++++++++++++++------ audiotrivia/audiotrivia.py | 8 ++++++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/audiotrivia/audiosession.py b/audiotrivia/audiosession.py index 780d4b9..4207fab 100644 --- a/audiotrivia/audiosession.py +++ b/audiotrivia/audiosession.py @@ -1,8 +1,12 @@ """Module to manage audio trivia sessions.""" import asyncio +import logging import lavalink from redbot.cogs.trivia import TriviaSession +from redbot.core.utils.chat_formatting import bold + +log = logging.getLogger("red.fox_v3.audiotrivia.audiosession") class AudioSession(TriviaSession): @@ -23,9 +27,9 @@ class AudioSession(TriviaSession): async def run(self): """Run the audio trivia session. - In order for the trivia session to be stopped correctly, this should - only be called internally by `TriviaSession.start`. - """ + In order for the trivia session to be stopped correctly, this should + only be called internally by `TriviaSession.start`. + """ await self._send_startup_msg() max_score = self.settings["max_score"] delay = self.settings["delay"] @@ -36,8 +40,8 @@ class AudioSession(TriviaSession): self.count += 1 await self.player.stop() - msg = "**Question number {}!**\n\nName this audio!".format(self.count) - await self.ctx.send(msg) + msg = bold(f"Question number {self.count}!") + "\n\nName this audio!" + await self.ctx.maybe_send_embed(msg) # print("Audio question: {}".format(question)) # await self.ctx.invoke(self.audio.play(ctx=self.ctx, query=question)) @@ -45,7 +49,12 @@ class AudioSession(TriviaSession): # await self.ctx.invoke(self.player.play, query=question) query = question.strip("<>") - tracks = await self.player.get_tracks(query) + load_result = await self.player.load_tracks(query) + if load_result.has_error: + await self.ctx.maybe_send_embed(f"Track has error, skipping. See logs for details") + log.info(f"Track has error: {load_result.exception_message}") + continue # Skip tracks with error + tracks = load_result.tracks seconds = tracks[0].length / 1000 if self.settings["repeat"] and seconds < delay: diff --git a/audiotrivia/audiotrivia.py b/audiotrivia/audiotrivia.py index c0c88fd..ec7b5ea 100644 --- a/audiotrivia/audiotrivia.py +++ b/audiotrivia/audiotrivia.py @@ -1,4 +1,5 @@ import datetime +import logging import pathlib from typing import List @@ -15,7 +16,7 @@ from redbot.core.utils.chat_formatting import box from .audiosession import AudioSession -# from redbot.cogs.audio.utils import userlimit +log = logging.getLogger("red.fox_v3.audiotrivia") class AudioTrivia(Trivia): @@ -166,7 +167,10 @@ class AudioTrivia(Trivia): # Delay in audiosettings overwrites delay in settings combined_settings = {**settings, **audiosettings} session = AudioSession.start( - ctx=ctx, question_list=trivia_dict, settings=combined_settings, player=lavaplayer, + ctx=ctx, + question_list=trivia_dict, + settings=combined_settings, + player=lavaplayer, ) self.trivia_sessions.append(session) LOG.debug("New audio trivia session; #%s in %d", ctx.channel, ctx.guild.id)