diff options
Diffstat (limited to 'code/cogs/queue.py')
-rw-r--r-- | code/cogs/queue.py | 112 |
1 files changed, 78 insertions, 34 deletions
diff --git a/code/cogs/queue.py b/code/cogs/queue.py index d11a4a9..48931cf 100644 --- a/code/cogs/queue.py +++ b/code/cogs/queue.py @@ -6,7 +6,37 @@ from cogs.music import Music import math import lavalink -from utils.config import BOT_COLOR +from utils.config import create_embed + +""" +Create an embed for the queue given the current queue and desired page number/pages +""" + + +def create_queue_embed(queue: list, page: int, pages: int): + items_per_page = 10 + start = (page - 1) * items_per_page + end = start + items_per_page + + queue_list = "" + for index, track in enumerate(queue[start:end], start=start): + # Change ms duration to hour, min, sec in the format of 00:00:00 + track_duration = lavalink.utils.format_time(track.duration) + # If the track is less than an hour, remove the hour from the duration + if track_duration.split(":")[0] == "00": + track_duration = track_duration[3:] + + queue_list += ( + f"`{index+1}. ` [{track.title}]({track.uri}) -" + f" {track.author} `({track_duration})`\n" + ) + + embed = create_embed( + title=f"Current Song Queue", + description=f"**{len(queue)} total tracks**\n\n{queue_list}", + footer=f"Page {page}/{pages}", + ) + return embed class Queue(commands.Cog): @@ -22,54 +52,68 @@ class Queue(commands.Cog): "See the current queue of songs" player = self.bot.lavalink.player_manager.get(interaction.guild.id) + pages = math.ceil(len(player.queue) / 10) + # Force page to 1 if an invalid page is provided + if page < 1 or page > pages: + page = 1 + if not player.queue: - embed = discord.Embed( + embed = create_embed( title="Nothing Queued", description=( "Nothing is currently in the queue, add a song with the" " </play:1224840890368000172> command." ), - color=BOT_COLOR, - ) - embed.set_footer( - text=datetime.datetime.now(datetime.timezone.utc).strftime( - "%Y-%m-%d %H:%M:%S" - ) - + " UTC" ) return await interaction.response.send_message( embed=embed, ephemeral=True ) - items_per_page = 10 - pages = math.ceil(len(player.queue) / items_per_page) + embed = create_queue_embed(player.queue, page, pages) + view = QueueView(page, pages, player.queue) + await interaction.response.send_message(embed=embed, view=view) - start = (page - 1) * items_per_page - end = start + items_per_page - queue_list = "" - for index, track in enumerate(player.queue[start:end], start=start): - # Change ms duration to hour, min, sec in the format of 00:00:00 - track_duration = lavalink.utils.format_time(track.duration) - # If the track is less than an hour, remove the hour from the duration - if track_duration.split(":")[0] == "00": - track_duration = track_duration[3:] +async def setup(bot): + await bot.add_cog(Queue(bot)) - queue_list += ( - f"`{index+1}. ` [{track.title}]({track.uri}) -" - f" {track.author} `({track_duration})`\n" - ) - embed = discord.Embed( - title=f"Queue for {interaction.guild.name}", - description=( - f"**{len(player.queue)} tracks total**\n\n{queue_list}" - ), - color=BOT_COLOR, +class QueueView(discord.ui.View): + def __init__(self, page: int, pages: int, queue: list): + super().__init__() + self.page = page + self.pages = pages + self.queue = queue + # Create the previous and next buttons + self.previous_button = discord.ui.Button( + label="Previous", style=discord.ButtonStyle.gray ) - embed.set_footer(text=f"Viewing page {page}/{pages}") - await interaction.response.send_message(embed=embed) + # Determine if the button should be disabled, add callback, add to view + self.previous_button.disabled = self.page <= 1 + self.previous_button.callback = self.previous_page + self.add_item(self.previous_button) + self.next_button = discord.ui.Button( + label="Next", style=discord.ButtonStyle.gray + ) + self.next_button.disabled = self.page >= self.pages + self.next_button.callback = self.next_page + self.add_item(self.next_button) -async def setup(bot): - await bot.add_cog(Queue(bot)) + async def previous_page(self, interaction: discord.Interaction): + # Decrement the page number, recreate the embed, determine if the + # button should be disabled, and update the message + self.page -= 1 + embed = create_queue_embed(self.queue, self.page, self.pages) + self.previous_button.disabled = self.page <= 1 + self.next_button.disabled = self.page >= self.pages + await interaction.response.edit_message(embed=embed, view=self) + + async def next_page(self, interaction: discord.Interaction): + # Increment the page number, recreate the embed, determine if the + # button should be disabled, and update the message + self.page += 1 + embed = create_queue_embed(self.queue, self.page, self.pages) + self.previous_button.disabled = self.page <= 1 + self.next_button.disabled = self.page >= self.pages + await interaction.response.edit_message(embed=embed, view=self) |