Merge branch '12-handle-playback-error-handling' into 'develop'

Resolve "Crashes in handle_playback cancel loop"

Closes #12

See merge request ekzyis/musicube!13
This commit is contained in:
Ramdip Gill 2021-09-25 23:58:44 +00:00
commit 5585e0060d
1 changed files with 31 additions and 13 deletions

View File

@ -9,7 +9,7 @@ from dotenv import load_dotenv
import youtube_dl import youtube_dl
from error import ErrorHandler from error import ErrorHandler
from message import NowPlayingMessage, QueuedMessage from message import NowPlayingMessage, QueuedMessage, ErrorMessage
load_dotenv() load_dotenv()
@ -54,22 +54,40 @@ class Music(commands.Cog):
# pylint: disable=no-member # pylint: disable=no-member
self._handle_playback.cancel() 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() @tasks.loop()
async def _handle_playback(self): async def _handle_playback(self):
while True: while True:
await self._queue_lock.acquire() try:
ctx, song = await self._queue.get() await self._queue_lock.acquire()
audio = discord.FFmpegPCMAudio(song.audio_url, **self._ffmpeg_options) 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._next()
continue
audio = discord.FFmpegPCMAudio(song.audio_url, **self._ffmpeg_options)
def after(err): def after(err):
if err: if err:
print(f"Player error: {err}") print(f"Player error: {err}")
self._queue.task_done() self._next()
self._queue_lock.release() ctx.voice_client.play(audio, after=after)
ctx.voice_client.play(audio, after=after) embed = NowPlayingMessage(title=song.title, url=song.webpage_url)
await ctx.send(embed=embed)
embed = NowPlayingMessage(title=song.title, url=song.webpage_url) # pylint: disable=broad-except
await ctx.send(embed=embed) except Exception as err:
print(f"Error during playback: {err}")
if ctx:
embed = ErrorMessage(str(err))
await ctx.send(embed=embed)
self._next()
@_handle_playback.before_loop @_handle_playback.before_loop
async def before_handle_playback(self): async def before_handle_playback(self):