playlist shuffling implemented

This commit is contained in:
Carsten Burgard 2022-04-15 15:05:37 +02:00
parent aa51cc4301
commit ed9e677108

View File

@ -619,6 +619,37 @@ class Plex(commands.Cog):
for track in album.tracks(): for track in album.tracks():
await self.play_queue.put(track) await self.play_queue.put(track)
async def play_playlist(self, title, shuffle=False):
try:
playlist = self._search_playlists(title)
except MediaNotFoundError:
await self.ctx.send(f"Can't find playlist: {title}")
bot_log.debug("Failed to queue playlist, can't find - %s", title)
return
try:
await self._validate(self.ctx)
except VoiceChannelError:
pass
try:
embed, img = self._build_embed_playlist(self, playlist, "Added playlist to queue", playlist.title)
await self.ctx.send(embed=embed, file=img)
items = [ item for item in playlist.items() if item.TYPE == "track" ]
if shuffle:
from random import shuffle
shuffle(items)
for item in items:
await self.play_queue.put(item)
bot_log.debug("Added to queue - %s", title)
except MediaNotFoundError:
await self.ctx.send(message="Playlist "+title+" seems to be empty!")
bot_log.debug("Playlist empty - %s", title)
@command() @command()
async def playlist(self, ctx, *args): async def playlist(self, ctx, *args):
""" """
@ -640,30 +671,32 @@ class Plex(commands.Cog):
# Save the context to use with async callbacks # Save the context to use with async callbacks
self.ctx = ctx self.ctx = ctx
title = " ".join(args) title = " ".join(args)
await self.play_playlist(title)
try: @command()
playlist = self._search_playlists(title) async def playlist_shuffle(self, ctx, *args):
except MediaNotFoundError: """
await ctx.send(f"Can't find playlist: {title}") User command to play playlist in shuffle mode
bot_log.debug("Failed to queue playlist, can't find - %s", title)
return
try: Searchs plex db and either, initiates playback, or
await self._validate(ctx) adds to queue. Handles invalid usage from the user.
except VoiceChannelError:
pass Args:
ctx: discord.ext.commands.Context message context from command
*args: Title of playlist to play
Returns:
None
Raises:
None
"""
# Save the context to use with async callbacks
self.ctx = ctx
title = " ".join(args)
await self.play_playlist(title,shuffle=True)
try:
embed, img = self._build_embed_playlist(self, playlist, "Added playlist to queue", playlist.title)
await ctx.send(embed=embed, file=img)
for item in playlist.items():
if (item.TYPE == "track"):
await self.play_queue.put(item)
bot_log.debug("Added to queue - %s", title)
except MediaNotFoundError:
await ctx.send(message="Playlist "+title+" seems to be empty!")
bot_log.debug("Playlist empty - %s", title)
@command() @command()
async def show_playlists(self, ctx, *args): async def show_playlists(self, ctx, *args):
""" """