catching a few exceptions and handling them accordingly

This commit is contained in:
Carsten Burgard 2022-04-15 12:42:49 +02:00
parent 04da5dfd42
commit 15b7e0f50e

View File

@ -299,14 +299,24 @@ class Plex(commands.Cog):
audio_stream = FFmpegPCMAudio(track_url) audio_stream = FFmpegPCMAudio(track_url)
while self.voice_channel and self.voice_channel.is_playing(): while self.voice_channel and self.voice_channel.is_playing():
bot_log.debug("waiting for track to finish")
await asyncio.sleep(2) await asyncio.sleep(2)
bot_log.debug("track finished")
self.voice_channel.play(audio_stream, after=self._toggle_next) if self.voice_channel:
self.voice_channel.play(audio_stream, after=self._toggle_next)
plex_log.debug("%s - URL: %s", self.current_track, track_url) plex_log.debug("%s - URL: %s", self.current_track, track_url)
embed, img = self._build_embed_track(self.current_track)
self.np_message_id = await self.ctx.send(embed=embed, file=img)
async def _play_next(self):
try:
self.current_track = await self.play_queue.get()
except asyncio.exceptions.CancelledError:
bot_log.debug("failed to pop queue")
embed, img = self._build_embed_track(self.current_track)
self.np_message_id = await self.ctx.send(embed=embed, file=img)
async def _audio_player_task(self): async def _audio_player_task(self):
""" """
@ -331,13 +341,14 @@ class Plex(commands.Cog):
try: try:
# Disconnect after 15 seconds idle # Disconnect after 15 seconds idle
async with timeout(15): async with timeout(15):
self.current_track = await self.play_queue.get() await self._play_next()
except asyncio.TimeoutError: except asyncio.TimeoutError:
bot_log("timeout - disconnecting")
await self.voice_channel.disconnect() await self.voice_channel.disconnect()
self.voice_channel = None self.voice_channel = None
if not self.current_track: if not self.current_track:
self.current_track = await self.play_queue.get() await self._play_next()
await self._play() await self._play()
await self.play_next_event.wait() await self.play_next_event.wait()
@ -510,8 +521,11 @@ class Plex(commands.Cog):
# Connect to voice if not already # Connect to voice if not already
if not self.voice_channel: if not self.voice_channel:
self.voice_channel = await ctx.author.voice.channel.connect() try:
bot_log.debug("Connected to vc.") self.voice_channel = await ctx.author.voice.channel.connect()
bot_log.debug("Connected to vc.")
except asyncio.exceptions.TimeoutError:
bot_log.debug("Cannot connect to vc - timeout")
@command() @command()
async def play(self, ctx, *args): async def play(self, ctx, *args):
@ -548,7 +562,7 @@ class Plex(commands.Cog):
pass pass
# Specific add to queue message # Specific add to queue message
if self.voice_channel.is_playing(): if self.voice_channel and self.voice_channel.is_playing():
bot_log.debug("Added to queue - %s", title) bot_log.debug("Added to queue - %s", title)
embed, img = self._build_embed_track(track, type_="queue") embed, img = self._build_embed_track(track, type_="queue")
await ctx.send(embed=embed, file=img) await ctx.send(embed=embed, file=img)