Skip to content

Commit

Permalink
Adding a queue for player send commands to Main process
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelSolVargas committed Jul 28, 2022
1 parent 5902a0d commit 60a3642
Show file tree
Hide file tree
Showing 23 changed files with 233 additions and 77 deletions.
6 changes: 6 additions & 0 deletions Config/Configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ def __init__(self) -> None:

self.MY_ERROR_BAD_COMMAND = 'This string serves to verify if some error was raised by myself on purpose'
self.INVITE_URL = 'https://discordapp.com/oauth2/authorize?client_id={}&scope=bot'

def getProcessManager(self):
return self.__manager

def setProcessManager(self, newManager):
self.__manager = newManager
3 changes: 3 additions & 0 deletions DiscordCogs/MusicCog.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from UI.Responses.EmbedCogResponse import EmbedCommandResponse
from UI.Views.PlayerView import PlayerView
from Music.VulkanBot import VulkanBot
from Config.Configs import VConfigs
from Parallelism.ProcessManager import ProcessManager

helper = Helper()

Expand All @@ -32,6 +34,7 @@ class MusicCog(Cog):

def __init__(self, bot: VulkanBot) -> None:
self.__bot: VulkanBot = bot
VConfigs().setProcessManager(ProcessManager(bot))

@command(name="play", help=helper.HELP_PLAY, description=helper.HELP_PLAY_LONG, aliases=['p', 'tocar'])
async def play(self, ctx: Context, *args) -> None:
Expand Down
3 changes: 1 addition & 2 deletions Handlers/ClearHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from Music.VulkanBot import VulkanBot
from Handlers.AbstractHandler import AbstractHandler
from Handlers.HandlerResponse import HandlerResponse
from Parallelism.ProcessManager import ProcessManager


class ClearHandler(AbstractHandler):
Expand All @@ -13,7 +12,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:

async def run(self) -> HandlerResponse:
# Get the current process of the guild
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo:
# Clear the playlist
Expand Down
3 changes: 1 addition & 2 deletions Handlers/HistoryHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from Handlers.HandlerResponse import HandlerResponse
from Utils.Utils import Utils
from typing import Union
from Parallelism.ProcessManager import ProcessManager
from discord import Interaction


Expand All @@ -14,7 +13,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:

async def run(self) -> HandlerResponse:
# Get the current process of the guild
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo:
processLock = processInfo.getLock()
Expand Down
3 changes: 1 addition & 2 deletions Handlers/LoopHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from Handlers.AbstractHandler import AbstractHandler
from Handlers.HandlerResponse import HandlerResponse
from Config.Exceptions import BadCommandUsage
from Parallelism.ProcessManager import ProcessManager
from typing import Union
from discord import Interaction

Expand All @@ -14,7 +13,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:

async def run(self, args: str) -> HandlerResponse:
# Get the current process of the guild
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if not processInfo:
embed = self.embeds.NOT_PLAYING()
Expand Down
3 changes: 1 addition & 2 deletions Handlers/MoveHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from Handlers.HandlerResponse import HandlerResponse
from Config.Exceptions import BadCommandUsage, VulkanError, InvalidInput, NumberRequired, UnknownError
from Music.Playlist import Playlist
from Parallelism.ProcessManager import ProcessManager
from typing import Union
from discord import Interaction

Expand All @@ -15,7 +14,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:
super().__init__(ctx, bot)

async def run(self, pos1: str, pos2: str) -> HandlerResponse:
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if not processInfo:
embed = self.embeds.NOT_PLAYING()
Expand Down
3 changes: 1 addition & 2 deletions Handlers/NowPlayingHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from Handlers.HandlerResponse import HandlerResponse
from Music.VulkanBot import VulkanBot
from Utils.Cleaner import Cleaner
from Parallelism.ProcessManager import ProcessManager
from typing import Union
from discord import Interaction

Expand All @@ -15,7 +14,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:

async def run(self) -> HandlerResponse:
# Get the current process of the guild
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if not processInfo:
embed = self.embeds.NOT_PLAYING()
Expand Down
5 changes: 2 additions & 3 deletions Handlers/PauseHandler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from discord.ext.commands import Context
from Handlers.AbstractHandler import AbstractHandler
from Handlers.HandlerResponse import HandlerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
from Music.VulkanBot import VulkanBot
from typing import Union
Expand All @@ -13,12 +12,12 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:
super().__init__(ctx, bot)

async def run(self) -> HandlerResponse:
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo:
# Send Pause command to be execute by player process
command = VCommands(VCommandsType.PAUSE, None)
queue = processInfo.getQueue()
queue = processInfo.getQueueToPlayer()
queue.put(command)

return HandlerResponse(self.ctx)
Expand Down
9 changes: 4 additions & 5 deletions Handlers/PlayHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from Music.Downloader import Downloader
from Music.Searcher import Searcher
from Music.Song import Song
from Parallelism.ProcessManager import ProcessManager
from Parallelism.ProcessInfo import ProcessInfo
from Parallelism.Commands import VCommands, VCommandsType
from Music.VulkanBot import VulkanBot
Expand Down Expand Up @@ -38,7 +37,7 @@ async def run(self, args: str) -> HandlerResponse:
raise InvalidInput(self.messages.INVALID_INPUT, self.messages.ERROR_TITLE)

# Get the process context for the current guild
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getPlayerInfo(self.guild, self.ctx)
playlist = processInfo.getPlaylist()
process = processInfo.getProcess()
Expand Down Expand Up @@ -74,7 +73,7 @@ async def run(self, args: str) -> HandlerResponse:
playlist.add_song(song)
# Release the acquired Lock
processLock.release()
queue = processInfo.getQueue()
queue = processInfo.getQueueToPlayer()
playCommand = VCommands(VCommandsType.PLAY, None)
queue.put(playCommand)
else:
Expand Down Expand Up @@ -106,7 +105,7 @@ async def run(self, args: str) -> HandlerResponse:

async def __downloadSongsAndStore(self, songs: List[Song], processInfo: ProcessInfo) -> None:
playlist = processInfo.getPlaylist()
queue = processInfo.getQueue()
queue = processInfo.getQueueToPlayer()
playCommand = VCommands(VCommandsType.PLAY, None)
# Trigger a task for each song to be downloaded
tasks: List[asyncio.Task] = []
Expand All @@ -115,7 +114,7 @@ async def __downloadSongsAndStore(self, songs: List[Song], processInfo: ProcessI
tasks.append(task)

# In the original order, await for the task and then if successfully downloaded add in the playlist
processManager = ProcessManager()
processManager = self.config.getProcessManager()
for index, task in enumerate(tasks):
await task
song = songs[index]
Expand Down
5 changes: 2 additions & 3 deletions Handlers/PrevHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from Handlers.AbstractHandler import AbstractHandler
from Config.Exceptions import BadCommandUsage, ImpossibleMove
from Handlers.HandlerResponse import HandlerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
from Music.VulkanBot import VulkanBot
from typing import Union
Expand All @@ -14,7 +13,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:
super().__init__(ctx, bot)

async def run(self) -> HandlerResponse:
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getPlayerInfo(self.guild, self.ctx)
if not processInfo:
embed = self.embeds.NOT_PLAYING()
Expand Down Expand Up @@ -44,7 +43,7 @@ async def run(self) -> HandlerResponse:

# Send a prev command, together with the user voice channel
prevCommand = VCommands(VCommandsType.PREV, self.author.voice.channel.id)
queue = processInfo.getQueue()
queue = processInfo.getQueueToPlayer()
queue.put(prevCommand)
return HandlerResponse(self.ctx)

Expand Down
3 changes: 1 addition & 2 deletions Handlers/QueueHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from Handlers.HandlerResponse import HandlerResponse
from Music.Downloader import Downloader
from Utils.Utils import Utils
from Parallelism.ProcessManager import ProcessManager
from Music.VulkanBot import VulkanBot
from typing import Union
from discord import Interaction
Expand All @@ -16,7 +15,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:

async def run(self) -> HandlerResponse:
# Retrieve the process of the guild
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if not processInfo: # If no process return empty list
embed = self.embeds.EMPTY_QUEUE()
Expand Down
3 changes: 1 addition & 2 deletions Handlers/RemoveHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from Handlers.HandlerResponse import HandlerResponse
from Config.Exceptions import BadCommandUsage, VulkanError, ErrorRemoving, InvalidInput, NumberRequired
from Music.Playlist import Playlist
from Parallelism.ProcessManager import ProcessManager
from Music.VulkanBot import VulkanBot
from typing import Union
from discord import Interaction
Expand All @@ -16,7 +15,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:

async def run(self, position: str) -> HandlerResponse:
# Get the current process of the guild
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if not processInfo:
# Clear the playlist
Expand Down
5 changes: 2 additions & 3 deletions Handlers/ResetHandler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from discord.ext.commands import Context
from Handlers.AbstractHandler import AbstractHandler
from Handlers.HandlerResponse import HandlerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
from Music.VulkanBot import VulkanBot
from typing import Union
Expand All @@ -14,11 +13,11 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:

async def run(self) -> HandlerResponse:
# Get the current process of the guild
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo:
command = VCommands(VCommandsType.RESET, None)
queue = processInfo.getQueue()
queue = processInfo.getQueueToPlayer()
queue.put(command)

return HandlerResponse(self.ctx)
Expand Down
5 changes: 2 additions & 3 deletions Handlers/ResumeHandler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from discord.ext.commands import Context
from Handlers.AbstractHandler import AbstractHandler
from Handlers.HandlerResponse import HandlerResponse
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
from Music.VulkanBot import VulkanBot
from typing import Union
Expand All @@ -13,12 +12,12 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:
super().__init__(ctx, bot)

async def run(self) -> HandlerResponse:
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo:
# Send Resume command to be execute by player process
command = VCommands(VCommandsType.RESUME, None)
queue = processInfo.getQueue()
queue = processInfo.getQueueToPlayer()
queue.put(command)

return HandlerResponse(self.ctx)
Expand Down
3 changes: 1 addition & 2 deletions Handlers/ShuffleHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from Handlers.AbstractHandler import AbstractHandler
from Handlers.HandlerResponse import HandlerResponse
from Config.Exceptions import UnknownError
from Parallelism.ProcessManager import ProcessManager
from Music.VulkanBot import VulkanBot
from typing import Union
from discord import Interaction
Expand All @@ -13,7 +12,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:
super().__init__(ctx, bot)

async def run(self) -> HandlerResponse:
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo:
try:
Expand Down
5 changes: 2 additions & 3 deletions Handlers/SkipHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from Config.Exceptions import BadCommandUsage
from Handlers.HandlerResponse import HandlerResponse
from Music.VulkanBot import VulkanBot
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
from typing import Union
from discord import Interaction
Expand All @@ -14,7 +13,7 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:
super().__init__(ctx, bot)

async def run(self) -> HandlerResponse:
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo: # Verify if there is a running process
playlist = processInfo.getPlaylist()
Expand All @@ -25,7 +24,7 @@ async def run(self) -> HandlerResponse:

# Send a command to the player process to skip the music
command = VCommands(VCommandsType.SKIP, None)
queue = processInfo.getQueue()
queue = processInfo.getQueueToPlayer()
queue.put(command)

return HandlerResponse(self.ctx)
Expand Down
5 changes: 2 additions & 3 deletions Handlers/StopHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from Handlers.AbstractHandler import AbstractHandler
from Handlers.HandlerResponse import HandlerResponse
from Music.VulkanBot import VulkanBot
from Parallelism.ProcessManager import ProcessManager
from Parallelism.Commands import VCommands, VCommandsType
from typing import Union
from discord import Interaction
Expand All @@ -13,12 +12,12 @@ def __init__(self, ctx: Union[Context, Interaction], bot: VulkanBot) -> None:
super().__init__(ctx, bot)

async def run(self) -> HandlerResponse:
processManager = ProcessManager()
processManager = self.config.getProcessManager()
processInfo = processManager.getRunningPlayerInfo(self.guild)
if processInfo:
# Send command to player process stop
command = VCommands(VCommandsType.STOP, None)
queue = processInfo.getQueue()
queue = processInfo.getQueueToPlayer()
queue.put(command)

return HandlerResponse(self.ctx)
Expand Down
Loading

0 comments on commit 60a3642

Please sign in to comment.