Adding Play and Prev Controller

This commit is contained in:
Rafael Vargas 2022-03-26 12:19:25 -04:00
parent 8ac80c216f
commit 362ec02fe4
5 changed files with 132 additions and 32 deletions

View File

@ -7,7 +7,9 @@ from Config.Helper import Helper
from Controllers.ClearController import ClearController from Controllers.ClearController import ClearController
from Controllers.MoveController import MoveController from Controllers.MoveController import MoveController
from Controllers.NowPlayingController import NowPlayingController from Controllers.NowPlayingController import NowPlayingController
from Controllers.PlayController import PlayController
from Controllers.PlayerController import PlayersController from Controllers.PlayerController import PlayersController
from Controllers.PrevController import PrevController
from Controllers.RemoveController import RemoveController from Controllers.RemoveController import RemoveController
from Controllers.ResetController import ResetController from Controllers.ResetController import ResetController
from Controllers.ShuffleController import ShuffleController from Controllers.ShuffleController import ShuffleController
@ -53,21 +55,14 @@ class Music(commands.Cog):
@commands.command(name="play", help=helper.HELP_PLAY, description=helper.HELP_PLAY_LONG, aliases=['p', 'tocar']) @commands.command(name="play", help=helper.HELP_PLAY, description=helper.HELP_PLAY_LONG, aliases=['p', 'tocar'])
async def play(self, ctx: Context, *args) -> None: async def play(self, ctx: Context, *args) -> None:
track = " ".join(args) controller = PlayController(ctx, self.__bot)
requester = ctx.author.name
player = self.__get_player(ctx) response = await controller.run(args)
if player is None: if response is not None:
await self.__send_embed(ctx, self.__config.ERROR_TITLE, self.__config.NO_GUILD, 'red') view1 = EmbedView(response)
return view2 = EmoteView(response)
await view1.run()
if is_connected(ctx) is None: await view2.run()
success = await player.connect(ctx)
if success == False:
await self.__send_embed(ctx, self.__config.IMPOSSIBLE_MOVE, self.__config.NO_CHANNEL, 'red')
return
await player.play(ctx, track, requester)
@commands.command(name="queue", help=helper.HELP_QUEUE, description=helper.HELP_QUEUE_LONG, aliases=['q', 'fila']) @commands.command(name="queue", help=helper.HELP_QUEUE, description=helper.HELP_QUEUE_LONG, aliases=['q', 'fila'])
async def queue(self, ctx: Context) -> None: async def queue(self, ctx: Context) -> None:
@ -123,17 +118,14 @@ class Music(commands.Cog):
@commands.command(name='prev', help=helper.HELP_PREV, description=helper.HELP_PREV_LONG, aliases=['anterior']) @commands.command(name='prev', help=helper.HELP_PREV, description=helper.HELP_PREV_LONG, aliases=['anterior'])
async def prev(self, ctx: Context) -> None: async def prev(self, ctx: Context) -> None:
player = self.__get_player(ctx) controller = PrevController(ctx, self.__bot)
if player is None:
return
if is_connected(ctx) is None: response = await controller.run()
success = await player.connect(ctx) if response is not None:
if success == False: view1 = EmbedView(response)
await self.__send_embed(ctx, self.__config.IMPOSSIBLE_MOVE, self.__config.NO_CHANNEL, 'red') view2 = EmoteView(response)
return await view1.run()
await view2.run()
await player.play_prev(ctx)
@commands.command(name='history', help=helper.HELP_HISTORY, description=helper.HELP_HISTORY_LONG, aliases=['historico']) @commands.command(name='history', help=helper.HELP_HISTORY, description=helper.HELP_HISTORY_LONG, aliases=['historico'])
async def history(self, ctx: Context) -> None: async def history(self, ctx: Context) -> None:

View File

@ -0,0 +1,53 @@
from discord.ext.commands import Context
from discord import Client
from Controllers.AbstractController import AbstractController
from Exceptions.Exceptions import ImpossibleMove, UnknownError
from Controllers.ControllerResponse import ControllerResponse
class PlayController(AbstractController):
def __init__(self, ctx: Context, bot: Client) -> None:
super().__init__(ctx, bot)
async def run(self, args: str) -> ControllerResponse:
track = " ".join(args)
requester = self.ctx.author.name
if not self.__user_connected():
error = ImpossibleMove()
embed = self.embeds.NO_CHANNEL()
return ControllerResponse(self.ctx, embed, error)
if not self.__is_connected():
success = await self.__connect()
if not success:
error = UnknownError()
embed = self.embeds.UNKNOWN_ERROR()
return ControllerResponse(self.ctx, embed, error)
await self.player.play(self.ctx, track, requester)
def __user_connected(self) -> bool:
if self.ctx.author.voice:
return True
else:
return False
def __is_connected(self) -> bool:
try:
voice_channel = self.guild.voice_client.channel
if not self.guild.voice_client.is_connected():
return False
else:
return True
except:
return False
async def __connect(self) -> bool:
# if self.guild.voice_client is None:
try:
await self.ctx.author.voice.channel.connect(reconnect=True, timeout=None)
return True
except:
return False

View File

@ -0,0 +1,55 @@
from discord.ext.commands import Context
from discord import Client
from Controllers.AbstractController import AbstractController
from Exceptions.Exceptions import ImpossibleMove, UnknownError
from Controllers.ControllerResponse import ControllerResponse
class PrevController(AbstractController):
def __init__(self, ctx: Context, bot: Client) -> None:
super().__init__(ctx, bot)
async def run(self) -> ControllerResponse:
if len(self.player.playlist.history()) == 0:
error = ImpossibleMove()
embed = self.embeds.NOT_PREVIOUS_SONG()
return ControllerResponse(self.ctx, embed, error)
if not self.__user_connected():
error = ImpossibleMove()
embed = self.embeds.NO_CHANNEL()
return ControllerResponse(self.ctx, embed, error)
if not self.__is_connected():
success = await self.__connect()
if not success:
error = UnknownError()
embed = self.embeds.UNKNOWN_ERROR()
return ControllerResponse(self.ctx, embed, error)
await self.player.play_prev(self.ctx)
def __user_connected(self) -> bool:
if self.ctx.author.voice:
return True
else:
return False
def __is_connected(self) -> bool:
try:
voice_channel = self.guild.voice_client.channel
if not self.guild.voice_client.is_connected():
return False
else:
return True
except:
return False
async def __connect(self) -> bool:
# if self.guild.voice_client is None:
try:
await self.ctx.author.voice.channel.connect(reconnect=True, timeout=None)
return True
except:
return False

View File

@ -36,14 +36,6 @@ class Player(commands.Cog):
def playlist(self) -> Playlist: def playlist(self) -> Playlist:
return self.__playlist return self.__playlist
async def connect(self, ctx: Context) -> bool:
if not ctx.author.voice:
return False
if self.__guild.voice_client == None:
await ctx.author.voice.channel.connect(reconnect=True, timeout=None)
return True
def __play_next(self, error, ctx: Context) -> None: def __play_next(self, error, ctx: Context) -> None:
if self.__force_stop: # If it's forced to stop player if self.__force_stop: # If it's forced to stop player
self.__force_stop = False self.__force_stop = False

View File

@ -201,6 +201,14 @@ class Embeds:
) )
return embed return embed
def NO_CHANNEL(self) -> Embed:
embed = Embed(
title=self.__config.IMPOSSIBLE_MOVE,
description=self.__config.NO_CHANNEL,
colour=self.__colors.BLACK
)
return embed
def ERROR_DUE_LOOP_ONE_ON(self) -> Embed: def ERROR_DUE_LOOP_ONE_ON(self) -> Embed:
embed = Embed( embed = Embed(
title=self.__config.BAD_COMMAND_TITLE, title=self.__config.BAD_COMMAND_TITLE,