From 3c3c777c834b1640302032da6661c5ffccb5908c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 10 Sep 2019 01:30:37 +0200 Subject: [PATCH] Add ZA WARUDO command --- royalnet/bots/discord.py | 5 +- royalnet/commands/royalgames/__init__.py | 4 +- royalnet/commands/royalgames/mm.py | 2 + royalnet/commands/royalgames/zawarudo.py | 88 ++++++++++++++++++++++++ royalnet/royalgames.py | 3 +- 5 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 royalnet/commands/royalgames/zawarudo.py diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index 7b67278c..5b61d92c 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -123,7 +123,7 @@ class DiscordBot(GenericBot): await command.run(CommandArgs(parameters), data=data) except InvalidInputError as e: await data.reply(f":warning: {' '.join(e.args)}\n" - f"Syntax: [c]/{command.name} {command.syntax}[/c]") + f"Syntax: [c]!{command.name} {command.syntax}[/c]") except Exception as e: sentry_sdk.capture_exception(e) error_message = f"🦀 {e.__class__.__name__} 🦀\n" @@ -235,7 +235,7 @@ class DiscordBot(GenericBot): async def advance_music_data(self, guild: discord.Guild): """Try to play the next song, while it exists. Otherwise, just return.""" guild_music_data = self.music_data[guild] - voice_client = self.client.find_voice_client_by_guild(guild) + voice_client: discord.VoiceClient = self.client.find_voice_client_by_guild(guild) next_source: discord.AudioSource = await guild_music_data.next() await self.update_activity_with_source_title() if next_source is None: @@ -244,6 +244,7 @@ class DiscordBot(GenericBot): def advance(error=None): if error: + voice_client.disconnect(force=True) log.error(f"Error while advancing music_data: {error}") return self.loop.create_task(self.advance_music_data(guild)) diff --git a/royalnet/commands/royalgames/__init__.py b/royalnet/commands/royalgames/__init__.py index 8b78fa0b..aac75dfd 100644 --- a/royalnet/commands/royalgames/__init__.py +++ b/royalnet/commands/royalgames/__init__.py @@ -24,6 +24,7 @@ from .dnditem import DnditemCommand from .dndspell import DndspellCommand from .trivia import TriviaCommand from .mm import MmCommand +from .zawarudo import ZawarudoCommand __all__ = [ "CiaoruoziCommand", @@ -46,5 +47,6 @@ __all__ = [ "DnditemCommand", "DndspellCommand", "TriviaCommand", - "MmCommand" + "MmCommand", + "ZawarudoCommand" ] diff --git a/royalnet/commands/royalgames/mm.py b/royalnet/commands/royalgames/mm.py index 0448b323..92e30b96 100644 --- a/royalnet/commands/royalgames/mm.py +++ b/royalnet/commands/royalgames/mm.py @@ -334,6 +334,8 @@ class MmCommand(Command): def __init__(self, interface): super().__init__(interface) + if self.interface.name != "telegram": + return log.debug("Loading pending MMEvents from the database") mmevents = self.interface.session.query(self.interface.alchemy.MMEvent) \ .filter(self.interface.alchemy.MMEvent.datetime > datetime.datetime.now()) \ diff --git a/royalnet/commands/royalgames/zawarudo.py b/royalnet/commands/royalgames/zawarudo.py new file mode 100644 index 00000000..05d91222 --- /dev/null +++ b/royalnet/commands/royalgames/zawarudo.py @@ -0,0 +1,88 @@ +import typing +import discord +import asyncio +from ..command import Command +from ..commandinterface import CommandInterface +from ..commandargs import CommandArgs +from ..commanddata import CommandData +from ...utils import NetworkHandler, asyncify +from ...network import Request, ResponseSuccess +from ...error import * +from ...audio import YtdlDiscord +from ...audio.playmodes import Playlist +if typing.TYPE_CHECKING: + from ...bots import DiscordBot + + +class ZawarudoNH(NetworkHandler): + message_type = "music_zawarudo" + + ytdl_args = { + "format": "bestaudio", + "outtmpl": f"./downloads/%(title)s.%(ext)s" + } + + @classmethod + async def discord(cls, bot: "DiscordBot", data: dict): + # Find the matching guild + if data["guild_name"]: + guild = bot.client.find_guild(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] + # Ensure the guild has a PlayMode before adding the file to it + if not bot.music_data.get(guild): + # TODO: change Exception + raise Exception("No music_data for this guild") + # Start downloading + zw_start: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, + "https://scaleway.steffo.eu/jojo/zawarudo_intro.mp3", + **cls.ytdl_args) + zw_end: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, + "https://scaleway.steffo.eu/jojo/zawarudo_outro.mp3", + **cls.ytdl_args) + # Clear playlist + if bot.music_data[guild] is not None: + bot.music_data[guild].delete() + bot.music_data[guild] = Playlist() + # Get voice client + vc: discord.VoiceClient = bot.client.find_voice_client_by_guild(guild) + channel: discord.VoiceChannel = vc.channel + await bot.add_to_music_data(zw_start, guild) + for member in channel.members: + member: typing.Union[discord.User, discord.Member] + if member.bot: + continue + await member.edit(mute=True) + await asyncio.sleep(data["time"]) + await bot.add_to_music_data(zw_end, guild) + for member in channel.members: + member: typing.Union[discord.User, discord.Member] + if member.bot: + continue + await member.edit(mute=False) + return ResponseSuccess() + + +class ZawarudoCommand(Command): + name: str = "zawarudo" + + description: str = "Ferma il tempo!" + + syntax = "[ [guild] ] [durata]" + + def __init__(self, interface: CommandInterface): + super().__init__(interface) + interface.register_net_handler(ZawarudoNH.message_type, ZawarudoNH) + + async def run(self, args: CommandArgs, data: CommandData) -> None: + guild_name, time = args.match(r"(?:\[(.+)])?\s*(.+)?") + if time is None: + time = 5 + else: + time = int(time) + await data.reply(f"🕒 ZA WARUDO! TOKI WO TOMARE!") + await self.interface.net_request(Request(ZawarudoNH.message_type, {"time": time, "guild_name": guild_name}), "discord") diff --git a/royalnet/royalgames.py b/royalnet/royalgames.py index 5c740aec..28605d3a 100644 --- a/royalnet/royalgames.py +++ b/royalnet/royalgames.py @@ -42,7 +42,8 @@ if __debug__: SummonCommand, VideochannelCommand, TriviaCommand, - MmCommand + MmCommand, + ZawarudoCommand ] log.setLevel(logging.DEBUG) else: