added functionality to inspect current queue and skip multiple tracks at once

This commit is contained in:
Carsten Burgard 2022-04-15 11:53:43 +02:00
parent 19360e3101
commit 04da5dfd42

View File

@ -34,10 +34,11 @@ Plex:
show_playlists <ARG> <ARG> - Query for playlists with a name matching any of the arguments. show_playlists <ARG> <ARG> - Query for playlists with a name matching any of the arguments.
lyrics - Print the lyrics of the song (Requires Genius API) lyrics - Print the lyrics of the song (Requires Genius API)
np - Print the current playing song. np - Print the current playing song.
q - Print the current queue (This can take very long!)
stop - Halt playback and leave vc. stop - Halt playback and leave vc.
pause - Pause playback. pause - Pause playback.
resume - Resume playback. resume - Resume playback.
skip - Skip the current song. skip - Skip the current song. Give a number as argument to skip more than 1.
clear - Clear play queue. clear - Clear play queue.
[] - Optional args. [] - Optional args.
@ -203,6 +204,7 @@ class Plex(commands.Cog):
self.voice_channel = None self.voice_channel = None
self.current_track = None self.current_track = None
self.np_message_id = None self.np_message_id = None
self.show_queue_message_ids = []
self.ctx = None self.ctx = None
# Initialize events # Initialize events
@ -296,8 +298,8 @@ class Plex(commands.Cog):
track_url = self.current_track.getStreamURL() track_url = self.current_track.getStreamURL()
audio_stream = FFmpegPCMAudio(track_url) audio_stream = FFmpegPCMAudio(track_url)
while self.voice_channel.is_playing(): while self.voice_channel and self.voice_channel.is_playing():
asyncio.sleep(2) await asyncio.sleep(2)
self.voice_channel.play(audio_stream, after=self._toggle_next) self.voice_channel.play(audio_stream, after=self._toggle_next)
@ -339,7 +341,8 @@ class Plex(commands.Cog):
await self._play() await self._play()
await self.play_next_event.wait() await self.play_next_event.wait()
await self.np_message_id.delete() if self.np_message_id:
await self.np_message_id.delete()
def _toggle_next(self, error=None): def _toggle_next(self, error=None):
""" """
@ -391,6 +394,8 @@ class Plex(commands.Cog):
title = f"Now Playing - {track.title}" title = f"Now Playing - {track.title}"
elif type_ == "queue": elif type_ == "queue":
title = f"Added to queue - {track.title}" title = f"Added to queue - {track.title}"
elif type_ == "queued":
title = f"Next in line - {track.title}"
else: else:
raise ValueError(f"Unsupported type of embed {type_}") raise ValueError(f"Unsupported type of embed {type_}")
@ -739,7 +744,7 @@ class Plex(commands.Cog):
await ctx.send(":play_pause: Resumed") await ctx.send(":play_pause: Resumed")
@command() @command()
async def skip(self, ctx): async def skip(self, ctx, *args):
""" """
User command to skip song in queue User command to skip song in queue
@ -755,10 +760,16 @@ class Plex(commands.Cog):
Raises: Raises:
None None
""" """
bot_log.debug("Skip") n = 1
if args:
n = int(args[0])
bot_log.debug("Skipping "+str(n))
if self.voice_channel: if self.voice_channel:
self.voice_channel.stop() self.voice_channel.stop()
bot_log.debug("Skipped") bot_log.debug("Skipped")
if n>1:
for i in range(n-1):
await self.play_queue.get()
self._toggle_next() self._toggle_next()
@command(name="np") @command(name="np")
@ -779,15 +790,51 @@ class Plex(commands.Cog):
None None
""" """
if self.current_track: if self.current_track:
embed, img = self._build_embed_track(self.current_track) embed, img = self._build_embed_track(self.current_track,type_="play")
bot_log.debug("Now playing") bot_log.debug("Now playing")
if self.np_message_id: if self.np_message_id:
await self.np_message_id.delete() try:
bot_log.debug("Deleted old np status") await self.np_message_id.delete()
bot_log.debug("Deleted old np status")
except discord.errors.NotFound:
pass
bot_log.debug("Created np status") bot_log.debug("Created np status")
self.np_message_id = await ctx.send(embed=embed, file=img) self.np_message_id = await ctx.send(embed=embed, file=img)
@command(name="q")
async def show_queue(self, ctx):
"""
User command to print the current queue
Deletes old `now playing` status message,
Creates a new one with up to date information.
Args:
ctx: discord.ext.commands.Context message context from command
Returns:
None
Raises:
None
"""
bot_log.debug("Deleted old queue messages")
for msg in self.show_queue_message_ids:
await msg.delete()
# need to do this in order to avoid errors when queue is modified during inspection
elems = []
for track in self.play_queue._queue:
elems.append(track)
for track in elems:
embed, img = self._build_embed_track(track,type_="queued")
bot_log.debug("Show queue")
bot_log.debug("Created queue message")
self.show_queue_message_ids.append(await ctx.send(embed=embed, file=img))
@command() @command()
async def clear(self, ctx): async def clear(self, ctx):
""" """