From 58e8285a9bbdbd3462d2250a58ca2febe232ee8f Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 2 Dec 2019 20:40:25 +0100 Subject: [PATCH] Skip is done --- royalpack/commands/__init__.py | 4 +-- royalpack/commands/skip.py | 49 +++++++------------------------- royalpack/events/__init__.py | 4 +-- royalpack/events/discord_play.py | 2 +- royalpack/events/discord_skip.py | 33 +++++++++++++++++++++ 5 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 royalpack/events/discord_skip.py diff --git a/royalpack/commands/__init__.py b/royalpack/commands/__init__.py index 2f41196d..c0c2ea56 100644 --- a/royalpack/commands/__init__.py +++ b/royalpack/commands/__init__.py @@ -14,7 +14,7 @@ from .videochannel import VideochannelCommand from .play import PlayCommand # from .playmode import PlaymodeCommand # from .queue import QueueCommand -# from .skip import SkipCommand +from .skip import SkipCommand from .summon import SummonCommand # from .youtube import YoutubeCommand # from .soundcloud import SoundcloudCommand @@ -42,7 +42,7 @@ available_commands = [ PlayCommand, # PlaymodeCommand, # QueueCommand, - # SkipCommand, + SkipCommand, SummonCommand, # YoutubeCommand, # SoundcloudCommand, diff --git a/royalpack/commands/skip.py b/royalpack/commands/skip.py index 48e2a4ed..8b9a0f38 100644 --- a/royalpack/commands/skip.py +++ b/royalpack/commands/skip.py @@ -1,48 +1,21 @@ -import typing import discord +from typing import * from royalnet.commands import * -from royalnet.bots import DiscordBot class SkipCommand(Command): name: str = "skip" - aliases = ["s", "next", "n"] + aliases = ["s"] - description: str = "Salta la canzone attualmente in riproduzione in chat vocale." - - syntax: str = "[ [guild] ]" - - @staticmethod - async def _legacy_skip_handler(bot: "DiscordBot", guild_name: typing.Optional[str]): - # Find the matching guild - if guild_name: - guilds: typing.List[discord.Guild] = bot.client.find_guild_by_name(guild_name) - else: - guilds = bot.client.guilds - if len(guilds) == 0: - raise CommandError("No guilds with the specified name found.") - if len(guilds) > 1: - raise CommandError("Multiple guilds with the specified name found.") - guild = list(bot.client.guilds)[0] - # Set the currently playing source as ended - voice_client: discord.VoiceClient = bot.client.find_voice_client_by_guild(guild) - if voice_client and not (voice_client.is_playing() or voice_client.is_paused()): - raise CommandError("Nothing to skip") - # noinspection PyProtectedMember - voice_client._player.stop() - return {} - - _event_name = "_legacy_skip" - - def __init__(self, interface: CommandInterface): - super().__init__(interface) - if interface.name == "discord": - interface.register_herald_action(self._event_name, self._legacy_skip_handler) + description: str = "Salta il file attualmente in riproduzione." async def run(self, args: CommandArgs, data: CommandData) -> None: - guild_name, = args.match(r"(?:\[(.+)])?") - await self.interface.call_herald_action("discord", self._event_name, { - "guild_name": guild_name - }) - await data.reply(f"⏩ Richiesto lo skip della canzone attuale.") + if self.interface.name == "discord": + message: discord.Message = data.message + guild: discord.Guild = message.guild + guild_id: Optional[int] = guild.id + else: + guild_id = None + response: Dict[str, Any] = await self.interface.call_herald_event("discord", "discord_skip", guild_id=guild_id) + await data.reply("⏩ File attuale saltato!") diff --git a/royalpack/events/__init__.py b/royalpack/events/__init__.py index 8299a307..83593c84 100644 --- a/royalpack/events/__init__.py +++ b/royalpack/events/__init__.py @@ -2,15 +2,15 @@ from .discord_cv import DiscordCvEvent from .discord_summon import DiscordSummonEvent from .discord_play import DiscordPlayEvent +from .discord_skip import DiscordSkipEvent # Enter the commands of your Pack here! available_events = [ DiscordCvEvent, DiscordSummonEvent, DiscordPlayEvent, + DiscordSkipEvent, ] -# noinspection PyUnreachableCode - # Don't change this, it should automatically generate __all__ __all__ = [command.__name__ for command in available_events] diff --git a/royalpack/events/discord_play.py b/royalpack/events/discord_play.py index 5f6575d2..294bb735 100644 --- a/royalpack/events/discord_play.py +++ b/royalpack/events/discord_play.py @@ -36,7 +36,7 @@ class DiscordPlayEvent(Event): added: List[YtdlDiscord] = [] too_long: List[YtdlDiscord] = [] if isinstance(voice_player.playing, PlayableYTDQueue): - for ytd in ytds: + for index, ytd in enumerate(ytds): if ytd.info.duration >= datetime.timedelta(seconds=self.config["Play"]["max_song_duration"]): too_long.append(ytd) continue diff --git a/royalpack/events/discord_skip.py b/royalpack/events/discord_skip.py new file mode 100644 index 00000000..cb186f35 --- /dev/null +++ b/royalpack/events/discord_skip.py @@ -0,0 +1,33 @@ +import discord +from typing import * +from royalnet.commands import * +from royalnet.serf.discord import * + + +class DiscordSkipEvent(Event): + name = "discord_skip" + + async def run(self, + guild_id: Optional[int] = None, + **kwargs) -> dict: + if not isinstance(self.serf, DiscordSerf): + raise UnsupportedError() + client: discord.Client = self.serf.client + if len(self.serf.voice_players) == 1: + voice_player: VoicePlayer = self.serf.voice_players[0] + else: + if guild_id is None: + # TODO: trovare un modo per riprodurre canzoni su più server da Telegram + raise InvalidInputError("Non so in che Server riprodurre questo file...\n" + "Invia il comando su Discord, per favore!") + guild: discord.Guild = client.get_guild(guild_id) + if guild is None: + raise InvalidInputError("Impossibile trovare il Server specificato.") + voice_player: VoicePlayer = self.serf.find_voice_player(guild) + if voice_player is None: + raise UserError("Il bot non è in nessun canale vocale.\n" + "Evocalo prima con [c]summon[/c]!") + # Stop the playback of the current song + voice_player.voice_client.stop() + # Done! + return {}