Add skip button as reaction on "Now playing:"

This commit is contained in:
ekzyis 2021-10-02 02:05:34 +02:00
parent 5585e0060d
commit 5a91d8a5ec
1 changed files with 26 additions and 4 deletions

View File

@ -29,6 +29,7 @@ class Music(commands.Cog):
self._bot = bot self._bot = bot
self._queue = asyncio.Queue() self._queue = asyncio.Queue()
self._queue_lock = asyncio.Lock() self._queue_lock = asyncio.Lock()
self._current_skip_message = None
self._ytdl_params = { self._ytdl_params = {
'cookiefile': os.environ.get('YOUTUBE_COOKIES'), 'cookiefile': os.environ.get('YOUTUBE_COOKIES'),
'format': 'bestaudio/best', 'format': 'bestaudio/best',
@ -57,6 +58,7 @@ class Music(commands.Cog):
def _next(self): def _next(self):
"""Trigger playback of next song.""" """Trigger playback of next song."""
self._queue.task_done() self._queue.task_done()
self._current_skip_message = None
if self._queue_lock.locked(): if self._queue_lock.locked():
self._queue_lock.release() self._queue_lock.release()
@ -80,7 +82,8 @@ class Music(commands.Cog):
self._next() self._next()
ctx.voice_client.play(audio, after=after) ctx.voice_client.play(audio, after=after)
embed = NowPlayingMessage(title=song.title, url=song.webpage_url) embed = NowPlayingMessage(title=song.title, url=song.webpage_url)
await ctx.send(embed=embed) msg = await ctx.send(embed=embed)
await self._add_skip_button(msg)
# pylint: disable=broad-except # pylint: disable=broad-except
except Exception as err: except Exception as err:
print(f"Error during playback: {err}") print(f"Error during playback: {err}")
@ -89,6 +92,19 @@ class Music(commands.Cog):
await ctx.send(embed=embed) await ctx.send(embed=embed)
self._next() self._next()
async def _add_skip_button(self, msg):
await msg.add_reaction("⏭️")
self._current_skip_message = msg
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
if not user.bot \
and self._current_skip_message \
and reaction.message.id == self._current_skip_message.id \
and reaction.emoji == "⏭️":
voice_client = reaction.message.guild.voice_client
self._skip(voice_client)
@_handle_playback.before_loop @_handle_playback.before_loop
async def before_handle_playback(self): async def before_handle_playback(self):
await self._bot.wait_until_ready() await self._bot.wait_until_ready()
@ -109,12 +125,18 @@ class Music(commands.Cog):
embed = QueuedMessage(title=song.title, url=song.webpage_url) embed = QueuedMessage(title=song.title, url=song.webpage_url)
await ctx.send(embed=embed) await ctx.send(embed=embed)
def _skip(self, voice_client):
"""Skip to next song."""
if voice_client is None or not voice_client.is_playing():
raise commands.CommandError("No song playing")
# This skips to next song because the bot does not differentiate between
# a song stopping because it is finished or because it was manually stopped.
voice_client.stop()
@commands.command() @commands.command()
async def skip(self, ctx): async def skip(self, ctx):
async with ctx.typing(): async with ctx.typing():
if ctx.voice_client is None or not ctx.voice_client.is_playing(): self._skip(ctx.voice_client)
raise commands.CommandError("No song playing")
ctx.voice_client.stop()
@commands.command() @commands.command()
async def stop(self, ctx): async def stop(self, ctx):