mirror of
https://github.com/RafaelSolVargas/Vulkan.git
synced 2025-10-29 16:57:23 +00:00
Continuing the refactoring
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import asyncio
|
||||
import traceback
|
||||
from typing import List
|
||||
from typing import List, Union
|
||||
from Config.Exceptions import DownloadingError, InvalidInput, VulkanError
|
||||
from discord.ext.commands import Context
|
||||
from Handlers.AbstractHandler import AbstractHandler
|
||||
@@ -10,12 +10,9 @@ from Music.Downloader import Downloader
|
||||
from Music.Searcher import Searcher
|
||||
from Music.Song import Song
|
||||
from Parallelism.AbstractProcessManager import AbstractPlayersManager
|
||||
from Parallelism.ProcessInfo import PlayerInfo
|
||||
from Parallelism.Commands import VCommands, VCommandsType
|
||||
from Music.VulkanBot import VulkanBot
|
||||
from typing import Union
|
||||
from discord import Interaction
|
||||
from Music.Playlist import Playlist
|
||||
|
||||
|
||||
class PlayHandler(AbstractHandler):
|
||||
@@ -37,13 +34,12 @@ class PlayHandler(AbstractHandler):
|
||||
if musicsInfo is None or len(musicsInfo) == 0:
|
||||
raise InvalidInput(self.messages.INVALID_INPUT, self.messages.ERROR_TITLE)
|
||||
|
||||
# Get the process context for the current guild
|
||||
processManager: AbstractPlayersManager = self.config.getPlayersManager()
|
||||
processInfo = processManager.getOrCreatePlayerInfo(self.guild, self.ctx)
|
||||
playlist: Playlist = processInfo.getPlaylist()
|
||||
process = processInfo.getProcess()
|
||||
if not process.is_alive(): # If process has not yet started, start
|
||||
process.start()
|
||||
# If there is no executing player for the guild then we create the player
|
||||
playersManager: AbstractPlayersManager = self.config.getPlayersManager()
|
||||
if not playersManager.verifyIfPlayerExists(self.guild):
|
||||
playersManager.createPlayerForGuild(self.guild, self.ctx)
|
||||
|
||||
playlist = playersManager.getPlayerPlaylist(self.guild)
|
||||
|
||||
# Create the Songs objects
|
||||
songs: List[Song] = []
|
||||
@@ -67,19 +63,17 @@ class PlayHandler(AbstractHandler):
|
||||
embed = self.embeds.SONG_ADDED_TWO(song.info, pos)
|
||||
response = HandlerResponse(self.ctx, embed)
|
||||
|
||||
# Add the unique song to the playlist and send a command to player process
|
||||
processLock = processInfo.getLock()
|
||||
acquired = processLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
|
||||
# Add the unique song to the playlist and send a command to player
|
||||
playerLock = playersManager.getPlayerLock(self.guild)
|
||||
acquired = playerLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
|
||||
if acquired:
|
||||
playlist.add_song(song)
|
||||
# Release the acquired Lock
|
||||
processLock.release()
|
||||
queue = processInfo.getQueueToPlayer()
|
||||
playerLock.release()
|
||||
playCommand = VCommands(VCommandsType.PLAY, None)
|
||||
self.putCommandInQueue(queue, playCommand)
|
||||
|
||||
playersManager.sendCommandToPlayer(playCommand, self.guild)
|
||||
else:
|
||||
processManager.resetProcess(self.guild, self.ctx)
|
||||
playersManager.resetPlayer(self.guild, self.ctx)
|
||||
embed = self.embeds.PLAYER_RESTARTED()
|
||||
return HandlerResponse(self.ctx, embed)
|
||||
|
||||
@@ -89,10 +83,10 @@ class PlayHandler(AbstractHandler):
|
||||
if len(songs) > 10:
|
||||
fiveFirstSongs = songs[0:5]
|
||||
songs = songs[5:]
|
||||
await self.__downloadSongsAndStore(fiveFirstSongs, processInfo)
|
||||
await self.__downloadSongsAndStore(fiveFirstSongs, playersManager)
|
||||
|
||||
# Trigger a task to download all songs and then store them in the process playlist
|
||||
asyncio.create_task(self.__downloadSongsAndStore(songs, processInfo))
|
||||
# Trigger a task to download all songs and then store them in the playlist
|
||||
asyncio.create_task(self.__downloadSongsAndStore(songs, playersManager))
|
||||
|
||||
embed = self.embeds.SONGS_ADDED(len(songs))
|
||||
return HandlerResponse(self.ctx, embed)
|
||||
@@ -102,7 +96,7 @@ class PlayHandler(AbstractHandler):
|
||||
return HandlerResponse(self.ctx, embed, error)
|
||||
except Exception as error:
|
||||
print(f'ERROR IN PLAYHANDLER -> {traceback.format_exc()}', {type(error)})
|
||||
if isinstance(error, VulkanError): # If error was already processed
|
||||
if isinstance(error, VulkanError):
|
||||
embed = self.embeds.CUSTOM_ERROR(error)
|
||||
else:
|
||||
error = UnknownError()
|
||||
@@ -110,9 +104,8 @@ class PlayHandler(AbstractHandler):
|
||||
|
||||
return HandlerResponse(self.ctx, embed, error)
|
||||
|
||||
async def __downloadSongsAndStore(self, songs: List[Song], processInfo: PlayerInfo) -> None:
|
||||
playlist = processInfo.getPlaylist()
|
||||
queue = processInfo.getQueueToPlayer()
|
||||
async def __downloadSongsAndStore(self, songs: List[Song], playersManager: AbstractPlayersManager) -> None:
|
||||
playlist = playersManager.getPlayerPlaylist(self.guild)
|
||||
playCommand = VCommands(VCommandsType.PLAY, None)
|
||||
tooManySongs = len(songs) > 100
|
||||
|
||||
@@ -126,21 +119,18 @@ class PlayHandler(AbstractHandler):
|
||||
task = asyncio.create_task(self.__down.download_song(song))
|
||||
tasks.append(task)
|
||||
|
||||
# In the original order, await for the task and then, if successfully downloaded, add to the playlist
|
||||
processManager: AbstractPlayersManager = self.config.getPlayersManager()
|
||||
for index, task in enumerate(tasks):
|
||||
await task
|
||||
song = songs[index]
|
||||
if not song.problematic: # If downloaded add to the playlist and send play command
|
||||
processInfo = processManager.getOrCreatePlayerInfo(self.guild, self.ctx)
|
||||
processLock = processInfo.getLock()
|
||||
acquired = processLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
|
||||
playerLock = playersManager.getPlayerLock(self.guild)
|
||||
acquired = playerLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
|
||||
if acquired:
|
||||
playlist.add_song(song)
|
||||
self.putCommandInQueue(queue, playCommand)
|
||||
processLock.release()
|
||||
playersManager.sendCommandToPlayer(playCommand, self.guild)
|
||||
playerLock.release()
|
||||
else:
|
||||
processManager.resetProcess(self.guild, self.ctx)
|
||||
playersManager.resetPlayer(self.guild, self.ctx)
|
||||
|
||||
def __isUserConnected(self) -> bool:
|
||||
if self.ctx.author.voice:
|
||||
|
||||
Reference in New Issue
Block a user