Handle track errors gracefully

pull/138/head
bobloy 4 years ago
parent a046102549
commit f69e8fdb1a

@ -1,8 +1,12 @@
"""Module to manage audio trivia sessions.""" """Module to manage audio trivia sessions."""
import asyncio import asyncio
import logging
import lavalink import lavalink
from redbot.cogs.trivia import TriviaSession 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): class AudioSession(TriviaSession):
@ -23,9 +27,9 @@ class AudioSession(TriviaSession):
async def run(self): async def run(self):
"""Run the audio trivia session. """Run the audio trivia session.
In order for the trivia session to be stopped correctly, this should In order for the trivia session to be stopped correctly, this should
only be called internally by `TriviaSession.start`. only be called internally by `TriviaSession.start`.
""" """
await self._send_startup_msg() await self._send_startup_msg()
max_score = self.settings["max_score"] max_score = self.settings["max_score"]
delay = self.settings["delay"] delay = self.settings["delay"]
@ -36,8 +40,8 @@ class AudioSession(TriviaSession):
self.count += 1 self.count += 1
await self.player.stop() await self.player.stop()
msg = "**Question number {}!**\n\nName this audio!".format(self.count) msg = bold(f"Question number {self.count}!") + "\n\nName this audio!"
await self.ctx.send(msg) await self.ctx.maybe_send_embed(msg)
# print("Audio question: {}".format(question)) # print("Audio question: {}".format(question))
# await self.ctx.invoke(self.audio.play(ctx=self.ctx, query=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) # await self.ctx.invoke(self.player.play, query=question)
query = question.strip("<>") 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 seconds = tracks[0].length / 1000
if self.settings["repeat"] and seconds < delay: if self.settings["repeat"] and seconds < delay:

@ -1,4 +1,5 @@
import datetime import datetime
import logging
import pathlib import pathlib
from typing import List from typing import List
@ -15,7 +16,7 @@ from redbot.core.utils.chat_formatting import box
from .audiosession import AudioSession from .audiosession import AudioSession
# from redbot.cogs.audio.utils import userlimit log = logging.getLogger("red.fox_v3.audiotrivia")
class AudioTrivia(Trivia): class AudioTrivia(Trivia):
@ -166,7 +167,10 @@ class AudioTrivia(Trivia):
# Delay in audiosettings overwrites delay in settings # Delay in audiosettings overwrites delay in settings
combined_settings = {**settings, **audiosettings} combined_settings = {**settings, **audiosettings}
session = AudioSession.start( 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) self.trivia_sessions.append(session)
LOG.debug("New audio trivia session; #%s in %d", ctx.channel, ctx.guild.id) LOG.debug("New audio trivia session; #%s in %d", ctx.channel, ctx.guild.id)

Loading…
Cancel
Save