mirror of
https://github.com/RafaelSolVargas/Vulkan.git
synced 2025-10-29 16:57:23 +00:00
Sending initial changes, adding the concept of Handlers, Views, Exceptions and Result, these class will optimaze the code in terms of cleanliness and organizational
This commit is contained in:
34
vulkan/controllers/AbstractHandler.py
Normal file
34
vulkan/controllers/AbstractHandler.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from discord.ext.commands import Context
|
||||
from discord import Client, Guild
|
||||
from vulkan.controllers.PlayerController import PlayersController
|
||||
from vulkan.music.Player import Player
|
||||
from vulkan.results.AbstractResult import AbstractResult
|
||||
|
||||
|
||||
class AbstractHandler(ABC):
|
||||
def __init__(self, ctx: Context, bot: Client) -> None:
|
||||
self.__bot: Client = bot
|
||||
self.__controller = PlayersController(self.__bot)
|
||||
self.__player: Player = self.__controller.get_player(ctx.guild)
|
||||
self.__guild: Guild = ctx.guild
|
||||
|
||||
@abstractmethod
|
||||
async def run(self) -> AbstractResult:
|
||||
pass
|
||||
|
||||
@property
|
||||
def guild(self) -> Guild:
|
||||
return self.__guild
|
||||
|
||||
@property
|
||||
def player(self) -> Player:
|
||||
return self.__player
|
||||
|
||||
@property
|
||||
def controller(self) -> PlayersController:
|
||||
return self.__controller
|
||||
|
||||
@property
|
||||
def bot(self) -> Client:
|
||||
return self.__bot
|
||||
26
vulkan/controllers/Exceptions.py
Normal file
26
vulkan/controllers/Exceptions.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class Error(Exception, ABC):
|
||||
@abstractmethod
|
||||
def message():
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def title():
|
||||
pass
|
||||
|
||||
|
||||
class MusicUnavailable(Error):
|
||||
def __init__(self, message: str) -> None:
|
||||
self.__message = message
|
||||
super().__init__(message)
|
||||
|
||||
def message():
|
||||
pass
|
||||
|
||||
def title():
|
||||
pass
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__message
|
||||
44
vulkan/controllers/PlayerController.py
Normal file
44
vulkan/controllers/PlayerController.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from typing import Dict, List, Union
|
||||
from config.Singleton import Singleton
|
||||
from discord import Guild, Client, VoiceClient
|
||||
from vulkan.music.Player import Player
|
||||
|
||||
|
||||
class PlayersController(Singleton):
|
||||
def __init__(self, bot: Client = None) -> None:
|
||||
if not super().created:
|
||||
self.__bot: Client = bot
|
||||
if bot is not None:
|
||||
self.__players: Dict[Guild, Player] = self.__create_players()
|
||||
|
||||
def set_bot(self, bot: Client) -> None:
|
||||
self.__bot: Client = bot
|
||||
self.__players: Dict[Guild, Player] = self.__create_players()
|
||||
|
||||
def get_player(self, guild: Guild) -> Player:
|
||||
if guild not in self.__players.keys():
|
||||
player = Player(self.__bot, guild)
|
||||
self.__players[guild] = player
|
||||
|
||||
return self.__players[guild]
|
||||
|
||||
def reset_player(self, guild: Guild) -> None:
|
||||
if isinstance(guild, Guild):
|
||||
player = Player(self.__bot, guild)
|
||||
self.__players[guild] == player
|
||||
|
||||
def get_guild_voice(self, guild: Guild) -> Union[VoiceClient, None]:
|
||||
if guild.voice_client is None:
|
||||
return None
|
||||
else:
|
||||
return guild.voice_client
|
||||
|
||||
def __create_players(self) -> Dict[Guild, Player]:
|
||||
list_guilds: List[Guild] = self.__bot.guilds
|
||||
players: Dict[Guild, Player] = {}
|
||||
|
||||
for guild in list_guilds:
|
||||
player = Player(self.__bot, guild)
|
||||
players[guild] = player
|
||||
|
||||
return players
|
||||
26
vulkan/controllers/SkipController.py
Normal file
26
vulkan/controllers/SkipController.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from discord.ext.commands import Context
|
||||
from discord import Client
|
||||
from vulkan.controllers.AbstractHandler import AbstractHandler
|
||||
|
||||
|
||||
class SkipHandler(AbstractHandler):
|
||||
def __init__(self, ctx: Context, bot: Client) -> None:
|
||||
super().__init__(ctx, bot)
|
||||
|
||||
async def run(self) -> None:
|
||||
if self.player.playlist.looping_one:
|
||||
""" embed = Embed(
|
||||
title=config.SONG_PLAYER,
|
||||
description=config.LOOP_ON,
|
||||
colour=config.COLOURS['blue']
|
||||
)
|
||||
await ctx.send(embed=embed)
|
||||
return False
|
||||
"""
|
||||
return None
|
||||
|
||||
voice = self.controller.get_guild_voice(self.guild)
|
||||
if voice is None:
|
||||
return None
|
||||
else:
|
||||
voice.stop()
|
||||
Reference in New Issue
Block a user