Modifying pause, resume and move commands to work with process

This commit is contained in:
Rafael Vargas 2022-07-23 00:37:11 -03:00
parent 1ce6deaa48
commit cd3eddb125
6 changed files with 72 additions and 47 deletions

View File

@ -4,60 +4,60 @@ from discord import Client
from Controllers.AbstractController import AbstractController from Controllers.AbstractController import AbstractController
from Controllers.ControllerResponse import ControllerResponse from Controllers.ControllerResponse import ControllerResponse
from Exceptions.Exceptions import BadCommandUsage, VulkanError, InvalidInput, NumberRequired, UnknownError from Exceptions.Exceptions import BadCommandUsage, VulkanError, InvalidInput, NumberRequired, UnknownError
from Music.Downloader import Downloader from Music.Playlist import Playlist
from Parallelism.ProcessManager import ProcessManager
class MoveController(AbstractController): class MoveController(AbstractController):
def __init__(self, ctx: Context, bot: Client) -> None: def __init__(self, ctx: Context, bot: Client) -> None:
super().__init__(ctx, bot) super().__init__(ctx, bot)
self.__down = Downloader()
async def run(self, pos1: str, pos2: str) -> ControllerResponse: async def run(self, pos1: str, pos2: str) -> ControllerResponse:
if not self.player.playing: processManager = ProcessManager()
processContext = processManager.getRunningPlayerContext(self.guild)
if not processContext:
embed = self.embeds.NOT_PLAYING() embed = self.embeds.NOT_PLAYING()
error = BadCommandUsage() error = BadCommandUsage()
return ControllerResponse(self.ctx, embed, error) return ControllerResponse(self.ctx, embed, error)
error = self.__validate_input(pos1, pos2) with processContext.getLock():
if error: error = self.__validateInput(pos1, pos2)
embed = self.embeds.ERROR_EMBED(error.message) if error:
return ControllerResponse(self.ctx, embed, error) embed = self.embeds.ERROR_EMBED(error.message)
return ControllerResponse(self.ctx, embed, error)
pos1, pos2 = self.__sanitize_input(pos1, pos2) playlist = processContext.getPlaylist()
playlist = self.player.playlist pos1, pos2 = self.__sanitizeInput(playlist, pos1, pos2)
if not playlist.validate_position(pos1) or not playlist.validate_position(pos2): if not playlist.validate_position(pos1) or not playlist.validate_position(pos2):
error = InvalidInput() error = InvalidInput()
embed = self.embeds.PLAYLIST_RANGE_ERROR() embed = self.embeds.PLAYLIST_RANGE_ERROR()
return ControllerResponse(self.ctx, embed, error) return ControllerResponse(self.ctx, embed, error)
try: try:
song = self.player.playlist.move_songs(pos1, pos2) song = playlist.move_songs(pos1, pos2)
songs = self.player.playlist.getSongsToPreload() song_name = song.title if song.title else song.identifier
await self.__down.preload(songs) embed = self.embeds.SONG_MOVED(song_name, pos1, pos2)
return ControllerResponse(self.ctx, embed)
except:
embed = self.embeds.ERROR_MOVING()
error = UnknownError()
return ControllerResponse(self.ctx, embed, error)
song_name = song.title if song.title else song.identifier def __validateInput(self, pos1: str, pos2: str) -> Union[VulkanError, None]:
embed = self.embeds.SONG_MOVED(song_name, pos1, pos2)
return ControllerResponse(self.ctx, embed)
except:
embed = self.embeds.ERROR_MOVING()
error = UnknownError()
return ControllerResponse(self.ctx, embed, error)
def __validate_input(self, pos1: str, pos2: str) -> Union[VulkanError, None]:
try: try:
pos1 = int(pos1) pos1 = int(pos1)
pos2 = int(pos2) pos2 = int(pos2)
except: except:
return NumberRequired(self.messages.ERROR_NUMBER) return NumberRequired(self.messages.ERROR_NUMBER)
def __sanitize_input(self, pos1: int, pos2: int) -> tuple: def __sanitizeInput(self, playlist: Playlist, pos1: int, pos2: int) -> tuple:
pos1 = int(pos1) pos1 = int(pos1)
pos2 = int(pos2) pos2 = int(pos2)
if pos1 == -1: if pos1 == -1:
pos1 = len(self.player.playlist) pos1 = len(playlist.getSongs())
if pos2 == -1: if pos2 == -1:
pos2 = len(self.player.playlist) pos2 = len(playlist.getSongs())
return pos1, pos2 return pos1, pos2

View File

@ -2,6 +2,8 @@ from discord.ext.commands import Context
from discord import Client from discord import Client
from Controllers.AbstractController import AbstractController from Controllers.AbstractController import AbstractController
from Controllers.ControllerResponse import ControllerResponse from Controllers.ControllerResponse import ControllerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
class PauseController(AbstractController): class PauseController(AbstractController):
@ -9,8 +11,12 @@ class PauseController(AbstractController):
super().__init__(ctx, bot) super().__init__(ctx, bot)
async def run(self) -> ControllerResponse: async def run(self) -> ControllerResponse:
if self.guild.voice_client is not None: processManager = ProcessManager()
if self.guild.voice_client.is_playing(): processContext = processManager.getRunningPlayerContext(self.guild)
self.guild.voice_client.pause() if processContext:
# Send Pause command to be execute by player process
command = VCommands(VCommandsType.PAUSE, None)
queue = processContext.getQueue()
queue.put(command)
return ControllerResponse(self.ctx) return ControllerResponse(self.ctx)

View File

@ -2,6 +2,8 @@ from discord.ext.commands import Context
from discord import Client from discord import Client
from Controllers.AbstractController import AbstractController from Controllers.AbstractController import AbstractController
from Controllers.ControllerResponse import ControllerResponse from Controllers.ControllerResponse import ControllerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
class ResumeController(AbstractController): class ResumeController(AbstractController):
@ -9,8 +11,12 @@ class ResumeController(AbstractController):
super().__init__(ctx, bot) super().__init__(ctx, bot)
async def run(self) -> ControllerResponse: async def run(self) -> ControllerResponse:
if self.guild.voice_client is not None: processManager = ProcessManager()
if self.guild.voice_client.is_paused(): processContext = processManager.getRunningPlayerContext(self.guild)
self.guild.voice_client.resume() if processContext:
# Send Resume command to be execute by player process
command = VCommands(VCommandsType.RESUME, None)
queue = processContext.getQueue()
queue.put(command)
return ControllerResponse(self.ctx) return ControllerResponse(self.ctx)

View File

@ -2,6 +2,8 @@ from discord.ext.commands import Context
from discord import Client from discord import Client
from Controllers.AbstractController import AbstractController from Controllers.AbstractController import AbstractController
from Controllers.ControllerResponse import ControllerResponse from Controllers.ControllerResponse import ControllerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
class StopController(AbstractController): class StopController(AbstractController):
@ -9,15 +11,12 @@ class StopController(AbstractController):
super().__init__(ctx, bot) super().__init__(ctx, bot)
async def run(self) -> ControllerResponse: async def run(self) -> ControllerResponse:
if self.guild.voice_client is None: processManager = ProcessManager()
processContext = processManager.getRunningPlayerContext(self.guild)
if processContext:
# Send command to player process stop
command = VCommands(VCommandsType.STOP, None)
queue = processContext.getQueue()
queue.put(command)
return ControllerResponse(self.ctx) return ControllerResponse(self.ctx)
if self.guild.voice_client.is_connected():
self.player.playlist.clear()
self.player.playlist.loop_off()
self.guild.voice_client.stop()
await self.guild.voice_client.disconnect()
return ControllerResponse(self.ctx)

View File

@ -9,6 +9,7 @@ class VCommandsType(Enum):
RESUME = 'Resume' RESUME = 'Resume'
CONTEXT = 'Context' CONTEXT = 'Context'
PLAY = 'Play' PLAY = 'Play'
STOP = 'Stop'
class VCommands: class VCommands:

View File

@ -153,11 +153,24 @@ class PlayerProcess(Process):
self.__resume() self.__resume()
elif type == VCommandsType.SKIP: elif type == VCommandsType.SKIP:
self.__skip() self.__skip()
elif type == VCommandsType.STOP:
self.__loop.create_task(self.__stop())
else: else:
print(f'[ERROR] -> Unknown Command Received: {command}') print(f'[ERROR] -> Unknown Command Received: {command}')
def __pause(self) -> None: def __pause(self) -> None:
pass if self.__guild.voice_client is not None:
if self.__guild.voice_client.is_playing():
self.__guild.voice_client.pause()
async def __stop(self) -> None:
if self.__guild.voice_client is not None:
if self.__guild.voice_client.is_connected():
with self.__lock:
self.__playlist.clear()
self.__playlist.loop_off()
self.__guild.voice_client.stop()
await self.__guild.voice_client.disconnect()
def __resume(self) -> None: def __resume(self) -> None:
pass pass