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.ControllerResponse import ControllerResponse
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):
def __init__(self, ctx: Context, bot: Client) -> None:
super().__init__(ctx, bot)
self.__down = Downloader()
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()
error = BadCommandUsage()
return ControllerResponse(self.ctx, embed, error)
error = self.__validate_input(pos1, pos2)
if error:
embed = self.embeds.ERROR_EMBED(error.message)
return ControllerResponse(self.ctx, embed, error)
with processContext.getLock():
error = self.__validateInput(pos1, pos2)
if error:
embed = self.embeds.ERROR_EMBED(error.message)
return ControllerResponse(self.ctx, embed, error)
pos1, pos2 = self.__sanitize_input(pos1, pos2)
playlist = self.player.playlist
playlist = processContext.getPlaylist()
pos1, pos2 = self.__sanitizeInput(playlist, pos1, pos2)
if not playlist.validate_position(pos1) or not playlist.validate_position(pos2):
error = InvalidInput()
embed = self.embeds.PLAYLIST_RANGE_ERROR()
return ControllerResponse(self.ctx, embed, error)
try:
song = self.player.playlist.move_songs(pos1, pos2)
if not playlist.validate_position(pos1) or not playlist.validate_position(pos2):
error = InvalidInput()
embed = self.embeds.PLAYLIST_RANGE_ERROR()
return ControllerResponse(self.ctx, embed, error)
try:
song = playlist.move_songs(pos1, pos2)
songs = self.player.playlist.getSongsToPreload()
await self.__down.preload(songs)
song_name = song.title if song.title else song.identifier
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
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]:
def __validateInput(self, pos1: str, pos2: str) -> Union[VulkanError, None]:
try:
pos1 = int(pos1)
pos2 = int(pos2)
except:
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)
pos2 = int(pos2)
if pos1 == -1:
pos1 = len(self.player.playlist)
pos1 = len(playlist.getSongs())
if pos2 == -1:
pos2 = len(self.player.playlist)
pos2 = len(playlist.getSongs())
return pos1, pos2

View File

@ -2,6 +2,8 @@ from discord.ext.commands import Context
from discord import Client
from Controllers.AbstractController import AbstractController
from Controllers.ControllerResponse import ControllerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
class PauseController(AbstractController):
@ -9,8 +11,12 @@ class PauseController(AbstractController):
super().__init__(ctx, bot)
async def run(self) -> ControllerResponse:
if self.guild.voice_client is not None:
if self.guild.voice_client.is_playing():
self.guild.voice_client.pause()
processManager = ProcessManager()
processContext = processManager.getRunningPlayerContext(self.guild)
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)

View File

@ -2,6 +2,8 @@ from discord.ext.commands import Context
from discord import Client
from Controllers.AbstractController import AbstractController
from Controllers.ControllerResponse import ControllerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
class ResumeController(AbstractController):
@ -9,8 +11,12 @@ class ResumeController(AbstractController):
super().__init__(ctx, bot)
async def run(self) -> ControllerResponse:
if self.guild.voice_client is not None:
if self.guild.voice_client.is_paused():
self.guild.voice_client.resume()
processManager = ProcessManager()
processContext = processManager.getRunningPlayerContext(self.guild)
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)

View File

@ -2,6 +2,8 @@ from discord.ext.commands import Context
from discord import Client
from Controllers.AbstractController import AbstractController
from Controllers.ControllerResponse import ControllerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
class StopController(AbstractController):
@ -9,15 +11,12 @@ class StopController(AbstractController):
super().__init__(ctx, bot)
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)
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'
CONTEXT = 'Context'
PLAY = 'Play'
STOP = 'Stop'
class VCommands:

View File

@ -153,11 +153,24 @@ class PlayerProcess(Process):
self.__resume()
elif type == VCommandsType.SKIP:
self.__skip()
elif type == VCommandsType.STOP:
self.__loop.create_task(self.__stop())
else:
print(f'[ERROR] -> Unknown Command Received: {command}')
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:
pass