aboutsummaryrefslogtreecommitdiff
path: root/code/cogs/queue.py
blob: 48931cf93cfc8abdcf356d4d493fd07a1da2e633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import discord
import datetime
from discord import app_commands
from discord.ext import commands
from cogs.music import Music
import math
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):
        self.bot = bot

    @app_commands.command()
    @app_commands.check(Music.create_player)
    @app_commands.describe(
        page="Queue page number - leave blank if you are unsure"
    )
    async def queue(self, interaction: discord.Interaction, page: int = 1):
        "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",
                description=(
                    "Nothing is currently in the queue, add a song with the"
                    " </play:1224840890368000172> command."
                ),
            )
            return await interaction.response.send_message(
                embed=embed, ephemeral=True
            )

        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)