From 354cfb318c41ad4ae099184fc56fe6832a4291d6 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 29 Mar 2020 00:14:55 +0100 Subject: [PATCH] Add playmode command --- royalpack/commands/__init__.py | 2 + royalpack/commands/playmode.py | 57 ++++++++++++++++++++++++++++ royalpack/events/__init__.py | 2 + royalpack/events/discord_playable.py | 48 +++++++++++++++++++++++ royalpack/utils/__init__.py | 2 + royalpack/utils/royalqueue.py | 13 ++++--- 6 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 royalpack/commands/playmode.py create mode 100644 royalpack/events/discord_playable.py diff --git a/royalpack/commands/__init__.py b/royalpack/commands/__init__.py index 0586cea2..317dbe6a 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -39,6 +39,7 @@ from .brawlhalla import BrawlhallaCommand from .diarioshuffle import DiarioshuffleCommand from .funkwhaleplaylist import FunkwhaleplaylistCommand from .voicestatus import VoicestatusCommand +from .playmode import PlaymodeCommand # Enter the commands of your Pack here! available_commands = [ @@ -82,6 +83,7 @@ available_commands = [ DiarioshuffleCommand, FunkwhaleplaylistCommand, VoicestatusCommand, + PlaymodeCommand, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/commands/playmode.py b/royalpack/commands/playmode.py new file mode 100644 index 00000000..01d62a37 --- /dev/null +++ b/royalpack/commands/playmode.py @@ -0,0 +1,57 @@ +from typing import * +import royalnet +import royalnet.commands as rc +import discord +from royalnet.backpack.tables import User, Discord + + +class PlaymodeCommand(rc.Command): + name: str = "playmode" + + aliases = ["pm"] + + description: str = "Seleziona la modalità di riproduzione musicale." + + syntax: str = "{queue|pool}" + + async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None: + if self.interface.name == "discord": + message: discord.Message = data.message + guild: discord.Guild = message.guild + if guild is None: + guild_id = None + else: + guild_id: Optional[int] = guild.id + else: + guild_id = None + + user: User = await data.get_author() + user_str = None + + if user is not None: + try: + user_discord: Discord = user.discord[0] + except (AttributeError, IndexError): + user_str = str(user) + else: + user_str = str(f"<@{user_discord.discord_id}>") + + response = await self.interface.call_herald_event("discord", "discord_playmode", + playable_string=args[0], + guild_id=guild_id, + user=user_str) + + if response["name"] == "RoyalQueue": + await data.reply(f"✅ Modalità di riproduzione impostata a [b]Queue[/b]:\n" + f"- Riproduci le canzoni nell'ordine scelto\n" + f"- Rimuovi le canzoni dopo averle riprodotte") + + elif response["name"] == "RoyalPool": + await data.reply(f"✅ Modalità di riproduzione impostata a [b]Pool[/b]:\n" + f"- Aggiungi canzoni al pool con [c]!p[/c], [c]!yt[/c] e [c]!fw[/c]\n" + f"- Riproduci all'infinito canzoni casuali dal pool\n" + f"- Non è possibile rimuovere canzoni dal pool, [c]!skip[/c]parle manderà solo avanti il pool\n" + f"- Interrompi la riproduzione del pool cambiando modalità di riproduzione") + + else: + await data.reply(f"✅ Modalità di riproduzione impostata a [c]{response['name']}[/c]!") \ No newline at end of file diff --git a/royalpack/events/__init__.py b/royalpack/events/__init__.py index 64834fb5..f956ee21 100644 --- a/royalpack/events/__init__.py +++ b/royalpack/events/__init__.py @@ -5,6 +5,7 @@ from .discord_play import DiscordPlayEvent from .discord_skip import DiscordSkipEvent from .discord_queue import DiscordQueueEvent from .discord_pause import DiscordPauseEvent +from .discord_playable import DiscordPlaymodeEvent # Enter the commands of your Pack here! available_events = [ @@ -14,6 +15,7 @@ available_events = [ DiscordSkipEvent, DiscordQueueEvent, DiscordPauseEvent, + DiscordPlaymodeEvent, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/events/discord_playable.py b/royalpack/events/discord_playable.py new file mode 100644 index 00000000..2c395ad7 --- /dev/null +++ b/royalpack/events/discord_playable.py @@ -0,0 +1,48 @@ +import datetime +from typing import * + +import discord +import royalnet.commands as rc +import royalnet.serf.discord as rsd +import royalnet.bard.discord as rbd + +from ..utils import RoyalQueue, RoyalPool + + +class DiscordPlaymodeEvent(rc.Event): + name = "discord_playmode" + + async def run(self, + playable_string: str, + guild_id: Optional[int] = None, + user: Optional[str] = None, + **kwargs) -> dict: + if not isinstance(self.serf, rsd.DiscordSerf): + raise rc.UnsupportedError() + + serf: rsd.DiscordSerf = self.serf + client: discord.Client = self.serf.client + guild: discord.Guild = client.get_guild(guild_id) if guild_id is not None else None + candidate_players: List[rsd.VoicePlayer] = serf.find_voice_players(guild) + + if len(candidate_players) == 0: + raise rc.UserError("Il bot non è in nessun canale vocale.\n" + "Evocalo prima con [c]summon[/c]!") + elif len(candidate_players) == 1: + voice_player = candidate_players[0] + else: + raise rc.CommandError("Non so a che Server cambiare Playable...\n" + "Invia il comando su Discord, per favore!") + + if playable_string.upper() == "QUEUE": + playable = RoyalQueue() + elif playable_string.upper() == "POOL": + playable = RoyalPool() + else: + raise rc.InvalidInputError(f"Unknown playable '{playable_string.upper()}'") + + await voice_player.change_playing(playable) + + return { + "name": f"{playable.__class__.__qualname__}" + } diff --git a/royalpack/utils/__init__.py b/royalpack/utils/__init__.py index 814c9eed..619a095d 100644 --- a/royalpack/utils/__init__.py +++ b/royalpack/utils/__init__.py @@ -1,7 +1,9 @@ from .royalqueue import RoyalQueue from .finduser import find_user_api +from .royalpool import RoyalPool __all__ = [ "RoyalQueue", "find_user_api", + "RoyalPool", ] diff --git a/royalpack/utils/royalqueue.py b/royalpack/utils/royalqueue.py index f1be3e96..a8af9110 100644 --- a/royalpack/utils/royalqueue.py +++ b/royalpack/utils/royalqueue.py @@ -46,12 +46,13 @@ class RoyalQueue(Playable): self.now_playing = None async def destroy(self): - log.debug(f"Deleting: {self.now_playing}") - await self.now_playing.delete_asap() - log.debug(f"Deleting: {self.now_playing.ytdl_file}") - await self.now_playing.ytdl_file.delete_asap() - log.debug(f"Deleted successfully!") - self.now_playing = None + if self.now_playing is not None: + log.debug(f"Deleting: {self.now_playing}") + await self.now_playing.delete_asap() + log.debug(f"Deleting: {self.now_playing.ytdl_file}") + await self.now_playing.ytdl_file.delete_asap() + log.debug(f"Deleted successfully!") + self.now_playing = None for file in self.contents: log.debug(f"Deleting: {file}") await file.delete_asap()