Modifying clear and skip commands to work with process

This commit is contained in:
Rafael Vargas 2022-07-22 22:57:27 -03:00
parent 19ae59c5b8
commit 1ce6deaa48
3 changed files with 67 additions and 45 deletions

View File

@ -2,6 +2,7 @@ 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
class ClearController(AbstractController): class ClearController(AbstractController):
@ -9,5 +10,13 @@ class ClearController(AbstractController):
super().__init__(ctx, bot) super().__init__(ctx, bot)
async def run(self) -> ControllerResponse: async def run(self) -> ControllerResponse:
self.player.playlist.clear() # Get the current process of the guild
processManager = ProcessManager()
processContext = processManager.getRunningPlayerContext(self.guild)
if processContext:
# Clear the playlist
playlist = processContext.getPlaylist()
with processContext.getLock():
playlist.clear()
return ControllerResponse(self.ctx) return ControllerResponse(self.ctx)

View File

@ -3,6 +3,8 @@ from discord import Client
from Controllers.AbstractController import AbstractController from Controllers.AbstractController import AbstractController
from Exceptions.Exceptions import BadCommandUsage from Exceptions.Exceptions import BadCommandUsage
from Controllers.ControllerResponse import ControllerResponse from Controllers.ControllerResponse import ControllerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
class SkipController(AbstractController): class SkipController(AbstractController):
@ -10,14 +12,17 @@ class SkipController(AbstractController):
super().__init__(ctx, bot) super().__init__(ctx, bot)
async def run(self) -> ControllerResponse: async def run(self) -> ControllerResponse:
if self.player.playlist.isLoopingOne(): processManager = ProcessManager()
embed = self.embeds.ERROR_DUE_LOOP_ONE_ON() processContext = processManager.getRunningPlayerContext(self.guild)
error = BadCommandUsage() if processContext: # Verify if there is a running process
return ControllerResponse(self.ctx, embed, error) playlist = processContext.getPlaylist()
if playlist.isLoopingOne():
embed = self.embeds.ERROR_DUE_LOOP_ONE_ON()
error = BadCommandUsage()
return ControllerResponse(self.ctx, embed, error)
voice = self.controller.get_guild_voice(self.guild) # Send a command to the player process to skip the music
if voice is None: command = VCommands(VCommandsType.SKIP, None)
return ControllerResponse(self.ctx) queue = processContext.getQueue()
else: queue.put(command)
voice.stop() return ControllerResponse(self.ctx)
return ControllerResponse(self.ctx)

View File

@ -62,12 +62,15 @@ class PlayerProcess(Process):
def run(self) -> None: def run(self) -> None:
"""Method called by process.start(), this will exec the actually _run method in a event loop""" """Method called by process.start(), this will exec the actually _run method in a event loop"""
self.__loop = asyncio.get_event_loop() try:
self.__configs = Configs() self.__loop = asyncio.get_event_loop()
self.__configs = Configs()
self.__semStopPlaying = Semaphore(0) self.__semStopPlaying = Semaphore(0)
self.__stopped = asyncio.Event() self.__stopped = asyncio.Event()
self.__loop.run_until_complete(self._run()) self.__loop.run_until_complete(self._run())
except Exception as e:
print(f'[Error in Process {self.name}] -> {e}')
async def _run(self) -> None: async def _run(self) -> None:
# Recreate the bot instance and objects using discord API # Recreate the bot instance and objects using discord API
@ -102,9 +105,9 @@ class PlayerProcess(Process):
async def __playSong(self, song: Song) -> None: async def __playSong(self, song: Song) -> None:
try: try:
source = await self.__ensureSource(song) if song.source is None:
if source is None: return self.__playNext(None)
self.__playNext(None)
self.__playing = True self.__playing = True
player = FFmpegPCMAudio(song.source, **self.FFMPEG_OPTIONS) player = FFmpegPCMAudio(song.source, **self.FFMPEG_OPTIONS)
@ -141,20 +144,27 @@ class PlayerProcess(Process):
print(f'Command Received: {type}') print(f'Command Received: {type}')
if type == VCommandsType.PAUSE: if type == VCommandsType.PAUSE:
self.pause() self.__pause()
elif type == VCommandsType.PLAY: elif type == VCommandsType.PLAY:
self.__loop.create_task(self.__playPlaylistSongs()) self.__loop.create_task(self.__playPlaylistSongs())
elif type == VCommandsType.PLAY_PREV: elif type == VCommandsType.PLAY_PREV:
self.__playPrev() self.__playPrev()
elif type == VCommandsType.RESUME: elif type == VCommandsType.RESUME:
pass self.__resume()
elif type == VCommandsType.SKIP: elif type == VCommandsType.SKIP:
pass self.__skip()
else: else:
print(f'[ERROR] -> Unknown Command Received: {command}') print(f'[ERROR] -> Unknown Command Received: {command}')
def pause(self): def __pause(self) -> None:
print(id(self)) pass
def __resume(self) -> None:
pass
def __skip(self) -> None:
if self.__guild.voice_client is not None:
self.__guild.voice_client.stop()
async def __playPrev(self, ctx: Context) -> None: async def __playPrev(self, ctx: Context) -> None:
with self.__lock: with self.__lock:
@ -208,29 +218,27 @@ class PlayerProcess(Process):
return bot return bot
async def __timeoutHandler(self) -> None: async def __timeoutHandler(self) -> None:
if self.__guild.voice_client is None: try:
return print('TimeoutHandler')
if self.__guild.voice_client is None:
print('return')
return
if self.__guild.voice_client.is_playing() or self.__guild.voice_client.is_paused(): if self.__guild.voice_client.is_playing() or self.__guild.voice_client.is_paused():
self.__timer = TimeoutClock(self.__timeoutHandler) print('Resetting')
self.__timer = TimeoutClock(self.__timeoutHandler, self.__loop)
elif self.__guild.voice_client.is_connected(): elif self.__guild.voice_client.is_connected():
with self.__lock: print('Finish')
self.__playlist.clear() with self.__lock:
self.__playlist.loop_off() self.__playlist.clear()
await self.__guild.voice_client.disconnect() self.__playlist.loop_off()
# Release semaphore to finish process self.__playing = False
self.__semStopPlaying.release() await self.__guild.voice_client.disconnect()
# Release semaphore to finish process
async def __ensureSource(self, song: Song) -> str: self.__semStopPlaying.release()
while True: except Exception as e:
if song.source is not None: # If song got downloaded print(f'[Error in Timeout] -> {e}')
return song.source
if song.problematic: # If song got any error
return None
await asyncio.sleep(0.1)
def __is_connected(self) -> bool: def __is_connected(self) -> bool:
try: try: