mirror of
https://github.com/RafaelSolVargas/Vulkan.git
synced 2025-10-29 16:57:23 +00:00
Adding pages to songs queue to move between all queue, adding buttons to queue embed to better user experience
This commit is contained in:
@@ -1,19 +1,23 @@
|
||||
from discord.ext.commands import Context
|
||||
from Config.Exceptions import InvalidIndex
|
||||
from Handlers.AbstractHandler import AbstractHandler
|
||||
from Handlers.HandlerResponse import HandlerResponse
|
||||
from Music.Downloader import Downloader
|
||||
from UI.Views.EmptyView import EmptyView
|
||||
from Utils.Utils import Utils
|
||||
from Music.VulkanBot import VulkanBot
|
||||
from typing import Union
|
||||
from discord import Interaction
|
||||
from Music.Song import Song
|
||||
from Music.Playlist import Playlist
|
||||
from typing import List, Union
|
||||
from discord import Button, Interaction
|
||||
from UI.Buttons.EmptyButton import EmptyButton
|
||||
from Config.Emojis import VEmojis
|
||||
|
||||
|
||||
class QueueHandler(AbstractHandler):
|
||||
def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:
|
||||
super().__init__(ctx, bot)
|
||||
self.__down = Downloader()
|
||||
|
||||
async def run(self) -> HandlerResponse:
|
||||
async def run(self, pageNumber=0) -> HandlerResponse:
|
||||
# Retrieve the process of the guild
|
||||
processManager = self.config.getProcessManager()
|
||||
processInfo = processManager.getRunningPlayerInfo(self.guild)
|
||||
@@ -25,7 +29,7 @@ class QueueHandler(AbstractHandler):
|
||||
processLock = processInfo.getLock()
|
||||
acquired = processLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
|
||||
if acquired:
|
||||
playlist = processInfo.getPlaylist()
|
||||
playlist: Playlist = processInfo.getPlaylist()
|
||||
|
||||
if playlist.isLoopingOne():
|
||||
song = playlist.getCurrentSong()
|
||||
@@ -33,13 +37,25 @@ class QueueHandler(AbstractHandler):
|
||||
processLock.release() # Release the Lock
|
||||
return HandlerResponse(self.ctx, embed)
|
||||
|
||||
songs_preload = playlist.getSongsToPreload()
|
||||
songsPages = playlist.getSongsPages()
|
||||
if pageNumber < 0 or pageNumber >= len(songsPages):
|
||||
embed = self.embeds.INVALID_INDEX()
|
||||
error = InvalidIndex()
|
||||
processLock.release() # Release the Lock
|
||||
return HandlerResponse(self.ctx, embed, error)
|
||||
|
||||
allSongs = playlist.getSongs()
|
||||
if len(songs_preload) == 0:
|
||||
if len(allSongs) == 0:
|
||||
embed = self.embeds.EMPTY_QUEUE()
|
||||
processLock.release() # Release the Lock
|
||||
return HandlerResponse(self.ctx, embed)
|
||||
|
||||
# Select the page in queue to be printed
|
||||
songs = songsPages[pageNumber]
|
||||
# Create view for this embed
|
||||
buttons = self.__createViewButtons(songsPages, pageNumber)
|
||||
queueView = EmptyView(self.bot, buttons, self.config.QUEUE_VIEW_TIMEOUT)
|
||||
|
||||
if playlist.isLoopingAll():
|
||||
title = self.messages.ALL_SONGS_LOOPING
|
||||
else:
|
||||
@@ -49,17 +65,33 @@ class QueueHandler(AbstractHandler):
|
||||
for song in allSongs]))
|
||||
total_songs = len(playlist.getSongs())
|
||||
|
||||
text = f'📜 Queue length: {total_songs} | ⌛ Duration: `{total_time}` downloaded \n\n'
|
||||
text = f'📜 Queue length: {total_songs} | Page Number: {pageNumber+1}/{len(songsPages)} | ⌛ Duration: `{total_time}` downloaded \n\n'
|
||||
|
||||
for pos, song in enumerate(songs_preload, start=1):
|
||||
# To work get the correct index of all songs
|
||||
startIndex = (pageNumber * self.config.MAX_SONGS_IN_PAGE) + 1
|
||||
for pos, song in enumerate(songs, start=startIndex):
|
||||
song_name = song.title if song.title else self.messages.SONG_DOWNLOADING
|
||||
text += f"**`{pos}` - ** {song_name} - `{Utils.format_time(song.duration)}`\n"
|
||||
|
||||
embed = self.embeds.QUEUE(title, text)
|
||||
# Release the acquired Lock
|
||||
processLock.release()
|
||||
return HandlerResponse(self.ctx, embed)
|
||||
return HandlerResponse(self.ctx, embed, view=queueView)
|
||||
else:
|
||||
processManager.resetProcess(self.guild, self.ctx)
|
||||
embed = self.embeds.PLAYER_RESTARTED()
|
||||
return HandlerResponse(self.ctx, embed)
|
||||
|
||||
def __createViewButtons(self, songsPages: List[List[Song]], pageNumber: int) -> List[Button]:
|
||||
buttons = []
|
||||
if pageNumber > 0:
|
||||
prevPageNumber = pageNumber - 1
|
||||
buttons.append(EmptyButton(self.bot, self.run, VEmojis().BACK,
|
||||
"Prev Page", pageNumber=prevPageNumber))
|
||||
|
||||
if pageNumber < len(songsPages) - 1:
|
||||
nextPageNumber = pageNumber + 1
|
||||
buttons.append(EmptyButton(self.bot, self.run, VEmojis().SKIP,
|
||||
"Next Page", pageNumber=nextPageNumber))
|
||||
|
||||
return buttons
|
||||
|
||||
Reference in New Issue
Block a user