From d6f63626b2faafee1b426837c126b39f72230159 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 31 May 2019 14:29:47 +0200 Subject: [PATCH] Aggiungi comando pause --- royalnet/commands/__init__.py | 3 +- royalnet/commands/pause.py | 52 +++++++++++++++++++++++++++++++++++ royalnet/royalgames.py | 2 +- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 royalnet/commands/pause.py diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index 9a2b9db3..c4094de7 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -25,9 +25,10 @@ from .playmode import PlaymodeCommand from .videochannel import VideochannelCommand from .missing import MissingCommand from .cv import CvCommand +from .pause import PauseCommand __all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", "SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand", "KvactiveCommand", "KvCommand", "KvrollCommand", "VideoinfoCommand", "SummonCommand", "PlayCommand", - "SkipCommand", "PlaymodeCommand", "VideochannelCommand", "MissingCommand", "CvCommand"] + "SkipCommand", "PlaymodeCommand", "VideochannelCommand", "MissingCommand", "CvCommand", "PauseCommand"] diff --git a/royalnet/commands/pause.py b/royalnet/commands/pause.py new file mode 100644 index 00000000..a27a778d --- /dev/null +++ b/royalnet/commands/pause.py @@ -0,0 +1,52 @@ +import typing +import discord +from ..network import Request, ResponseSuccess +from ..utils import Command, Call, NetworkHandler +from ..error import TooManyFoundError, NoneFoundError +if typing.TYPE_CHECKING: + from ..bots import DiscordBot + + +class PauseNH(NetworkHandler): + message_type = "music_pause" + + @classmethod + async def discord(cls, bot: "DiscordBot", data: dict): + # Find the matching guild + if data["guild_name"]: + guild = bot.client.find_guild_by_name(data["guild_name"]) + else: + if len(bot.music_data) == 0: + raise NoneFoundError("No voice clients active") + if len(bot.music_data) > 1: + raise TooManyFoundError("Multiple guilds found") + guild = list(bot.music_data)[0] + # Set the currently playing source as ended + voice_client: discord.VoiceClient = bot.client.find_voice_client_by_guild(guild) + if not voice_client.is_playing(): + raise NoneFoundError("Nothing to pause") + # Toggle pause + resume = voice_client._player.is_paused() + if resume: + voice_client._player.resume() + else: + voice_client._player.pause() + return ResponseSuccess({"resume": resume}) + + +class PauseCommand(Command): + + command_name = "pause" + command_description = "Mette in pausa o riprende la riproduzione della canzone attuale." + command_syntax = "[ [guild] ]" + + network_handlers = [PauseNH] + + @classmethod + async def common(cls, call: Call): + guild, = call.args.match(r"(?:\[(.+)])?") + response = await call.net_request(Request("music_pause", {"guild_name": guild}), "discord") + if response["resume"]: + await call.reply(f"▶️ Riproduzione ripresa.") + else: + await call.reply(f"⏸ Riproduzione messa in pausa.") diff --git a/royalnet/royalgames.py b/royalnet/royalgames.py index 867a66e7..46ebddfb 100644 --- a/royalnet/royalgames.py +++ b/royalnet/royalgames.py @@ -20,7 +20,7 @@ log.setLevel(logging.WARNING) commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, AuthorCommand, DiarioCommand, RageCommand, DateparserCommand, ReminderCommand, KvactiveCommand, KvCommand, KvrollCommand, VideoinfoCommand, SummonCommand, PlayCommand, SkipCommand, PlaymodeCommand, - VideochannelCommand, CvCommand] + VideochannelCommand, CvCommand, PauseCommand] address, port = "127.0.0.1", 1234