Add queue paging with buttons

This commit is contained in:
Parker M. 2024-11-30 00:16:17 -06:00
parent 0d3bb6732e
commit b716f3660b
Signed by: parker
GPG Key ID: 505ED36FC12B5D5E

View File

@ -8,6 +8,36 @@ import lavalink
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):
def __init__(self, bot):
@ -22,6 +52,11 @@ 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 = create_embed(
title="Nothing Queued",
@ -34,34 +69,51 @@ class Queue(commands.Cog):
embed=embed, ephemeral=True
)
items_per_page = 10
pages = math.ceil(len(player.queue) / items_per_page)
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:]
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(player.queue)} total tracks**\n\n{queue_list}"
),
footer=f"Page {page}/{pages}",
)
await interaction.response.send_message(embed=embed)
embed = create_queue_embed(player.queue, page, pages)
view = QueueView(page, pages, player.queue)
await interaction.response.send_message(embed=embed, view=view)
async def setup(bot):
await bot.add_cog(Queue(bot))
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
)
# 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 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)