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:
Rafael Vargas 2022-03-21 16:11:38 -04:00
parent 2bd100a3e1
commit 5c4d09bf9d
13 changed files with 241 additions and 6 deletions

16
config/Singleton.py Normal file
View File

@ -0,0 +1,16 @@
class Singleton(object):
__instance = None
__created = False
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = object.__new__(cls)
return cls.__instance
@property
def created(cls):
if cls.__created == False:
cls.__created = True
return False
else:
return True

View File

@ -4,7 +4,7 @@ BOT_TOKEN = config('BOT_TOKEN')
SPOTIFY_ID = config('SPOTIFY_ID')
SPOTIFY_SECRET = config('SPOTIFY_SECRET')
BOT_PREFIX = '!'
BOT_PREFIX = '$'
VC_TIMEOUT = 600
STARTUP_MESSAGE = 'Starting Vulkan...'

View File

@ -6,6 +6,7 @@ from config import config
from config import help
from vulkan.music.Player import Player
from vulkan.music.utils import is_connected
from vulkan.controllers.SkipController import SkipHandler
class Music(commands.Cog):
@ -65,14 +66,15 @@ class Music(commands.Cog):
@commands.command(name="skip", help=help.HELP_SKIP, description=help.HELP_SKIP_LONG, aliases=['s', 'pular'])
async def skip(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
await player.skip(ctx)
print(ctx.bot == self.__bot)
handler = SkipHandler(ctx, self.__bot)
await handler.run()
@commands.command(name='stop', help=help.HELP_STOP, description=help.HELP_STOP_LONG, aliases=['parar'])
async def stop(self, ctx: Context) -> None:
print(ctx.bot == self.__bot)
player = self.__get_player(ctx)
if player is None:
return

View 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

View 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

View 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

View 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()

View File

@ -27,6 +27,10 @@ class Player(commands.Cog):
self.FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'}
@property
def playlist(self) -> Playlist:
return self.__playlist
async def connect(self, ctx: Context) -> bool:
if not ctx.author.voice:
return False

View File

@ -0,0 +1,20 @@
from typing import Union
from discord.ext.commands import Context
from vulkan.controllers.Exceptions import Error
class AbstractResult:
def __init__(self, ctx: Context, success: bool) -> None:
self.__success: bool = success
self.__ctx: Context = ctx
@property
def ctx(self) -> Context:
return self.__ctx
@property
def success(self) -> bool:
return self.__success
def error(self) -> Union[Error, None]:
pass

View File

@ -0,0 +1,9 @@
from vulkan.results import AbstractResult
from typing import Union
from discord.ext.commands import Context
from vulkan.controllers.Exceptions import Error
class SuccessResult(AbstractResult):
def __init__(self) -> None:
super().__init__()

View File

@ -0,0 +1,34 @@
from abc import ABC, abstractmethod
from numpy import result_type
from vulkan.results.AbstractResult import AbstractResult
from discord.ext.commands import Context
from discord import Client, Message
class AbstractView(ABC):
def __init__(self, result: AbstractResult) -> None:
self.__result: AbstractResult = result
self.__context: Context = result.ctx
self.__message: Message = result.ctx.message
self.__bot: Client = result.ctx.bot
@property
def result(self) -> AbstractResult:
return self.__result
@property
def bot(self) -> Client:
return self.__result.ctx.bot
@property
def message(self) -> Message:
return self.__message
@property
def context(self) -> Context:
return self.__context
@abstractmethod
def run(self) -> None:
pass

10
vulkan/views/EmoteView.py Normal file
View File

@ -0,0 +1,10 @@
from vulkan.views.AbstractView import AbstractView
from vulkan.results import AbstractResult
class EmoteView(AbstractView):
def __init__(self, result: AbstractResult) -> None:
super().__init__(result)
def run(self) -> None:
return super().run()

View File

@ -0,0 +1,10 @@
from vulkan.views.AbstractView import AbstractView
from vulkan.results import AbstractResult
class MessageView(AbstractView):
def __init__(self, result: AbstractResult) -> None:
super().__init__(result)
def run(self) -> None:
return super().run()