From 04151d337ad7d83135e951d78650df8694fc8344 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 24 Apr 2019 17:27:05 +0200 Subject: [PATCH] Add !videochannel command --- royalgames.py | 3 ++- royalnet/commands/__init__.py | 3 ++- royalnet/commands/error_handler.py | 12 ++++----- royalnet/commands/ping.py | 2 +- royalnet/commands/videochannel.py | 42 ++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 royalnet/commands/videochannel.py diff --git a/royalgames.py b/royalgames.py index 4385b901..5e3e12e6 100644 --- a/royalgames.py +++ b/royalgames.py @@ -24,7 +24,8 @@ logging.getLogger("royalnet.bots.telegram").setLevel(logging.DEBUG) commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, AuthorCommand, DiarioCommand, RageCommand, DateparserCommand, ReminderCommand, KvactiveCommand, KvCommand, - KvrollCommand, VideoinfoCommand, SummonCommand, PlayCommand, SkipCommand, PlaymodeCommand] + KvrollCommand, VideoinfoCommand, SummonCommand, PlayCommand, SkipCommand, PlaymodeCommand, + VideochannelCommand] address, port = "localhost", 1234 diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index a7eb3d87..e083a329 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -18,9 +18,10 @@ from .summon import SummonCommand from .play import PlayCommand from .skip import SkipCommand from .playmode import PlaymodeCommand +from .videochannel import VideochannelCommand __all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", "SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand", "KvactiveCommand", "KvCommand", "KvrollCommand", "VideoinfoCommand", "SummonCommand", "PlayCommand", - "SkipCommand", "PlaymodeCommand"] + "SkipCommand", "PlaymodeCommand", "VideochannelCommand"] diff --git a/royalnet/commands/error_handler.py b/royalnet/commands/error_handler.py index d7f2d2a4..7c9293dd 100644 --- a/royalnet/commands/error_handler.py +++ b/royalnet/commands/error_handler.py @@ -23,29 +23,29 @@ class ErrorHandlerCommand(Command): async def common(cls, call: Call): exception: Exception = call.kwargs["exception"] if isinstance(exception, NoneFoundError): - await call.reply("⚠️ L'elemento richiesto non è stato trovato.") + await call.reply(f"⚠️ L'elemento richiesto non è stato trovato.\n[p]{exception}[/p]") return if isinstance(exception, TooManyFoundError): - await call.reply("⚠️ La richiesta effettuata è ambigua, pertanto è stata annullata.") + await call.reply(f"⚠️ La richiesta effettuata è ambigua, pertanto è stata annullata.\n[p]{exception}[/p]") return if isinstance(exception, UnregisteredError): - await call.reply("⚠️ Devi essere registrato a Royalnet per usare questo comando!") + await call.reply("⚠️ Devi essere registrato a Royalnet per usare questo comando.\nUsa il comando [c]sync[/c] per registrarti!") return if isinstance(exception, UnsupportedError): - await call.reply("⚠️ Il comando richiesto non è disponibile tramite questa interfaccia.") + await call.reply(f"⚠️ Il comando richiesto non è disponibile tramite l'interfaccia [c]{call.interface_name}[/c].") return if isinstance(exception, InvalidInputError): command = call.kwargs["previous_command"] await call.reply(f"⚠️ Sintassi non valida.\nSintassi corretta: [c]{call.interface_prefix}{command.command_name} {command.command_syntax}[/c]") return if isinstance(exception, InvalidConfigError): - await call.reply("⚠️ Il bot non è stato configurato correttamente, quindi questo comando non può essere eseguito. L'errore è stato segnalato all'amministratore.") + await call.reply(f"⚠️ Il bot non è stato configurato correttamente, quindi questo comando non può essere eseguito.\n[p]{exception}[/p]") return if isinstance(exception, RoyalnetError): await call.reply(f"⚠️ La richiesta a Royalnet ha restituito un errore: [p]{exception.exc}[/p]") return if isinstance(exception, ExternalError): - await call.reply("⚠️ Una risorsa esterna necessaria per l'esecuzione del comando non ha funzionato correttamente, quindi il comando è stato annullato.") + await call.reply(f"⚠️ Una risorsa esterna necessaria per l'esecuzione del comando non ha funzionato correttamente, quindi il comando è stato annullato.\n[p]{exception}[/p]") return await call.reply(f"❌ Eccezione non gestita durante l'esecuzione del comando:\n[b]{exception.__class__.__name__}[/b]\n[p]{exception}[/p]") log.error(f"Unhandled exception - {exception.__class__.__name__}: {exception}") diff --git a/royalnet/commands/ping.py b/royalnet/commands/ping.py index 7b2608d4..63ba7e37 100644 --- a/royalnet/commands/ping.py +++ b/royalnet/commands/ping.py @@ -6,7 +6,7 @@ from ..error import InvalidInputError class PingCommand(Command): command_name = "ping" - command_description = "Ping pong!" + command_description = "Ping pong dopo un po' di tempo!" command_syntax = "[time_to_wait]" @classmethod diff --git a/royalnet/commands/videochannel.py b/royalnet/commands/videochannel.py new file mode 100644 index 00000000..d8395f67 --- /dev/null +++ b/royalnet/commands/videochannel.py @@ -0,0 +1,42 @@ +import discord +import typing +from ..utils import Command, Call + + +class VideochannelCommand(Command): + + command_name = "videochannel" + command_description = "Converti il canale vocale in un canale video." + command_syntax = "[channelname]" + + @classmethod + async def discord(cls, call: Call): + bot = call.interface_obj.client + message: discord.Message = call.kwargs["message"] + channel_name: str = call.args.optional(0) + if channel_name: + guild: typing.Optional[discord.Guild] = message.guild + if guild is not None: + channels: typing.List[discord.abc.GuildChannel] = guild.channels + else: + channels = bot.get_all_channels() + matching_channels: typing.List[discord.VoiceChannel] = [] + for channel in channels: + if isinstance(channel, discord.VoiceChannel): + if channel.name == channel_name: + matching_channels.append(channel) + if len(matching_channels) == 0: + await call.reply("⚠️ Non esiste alcun canale vocale con il nome specificato.") + return + elif len(matching_channels) > 1: + await call.reply("⚠️ Esiste più di un canale vocale con il nome specificato.") + return + channel = matching_channels[0] + else: + author: discord.Member = message.author + voice: typing.Optional[discord.VoiceState] = author.voice + if voice is None: + await call.reply("⚠️ Non sei connesso a nessun canale vocale!") + return + channel = voice.channel + await call.reply(f"📹 Per entrare in modalità video, clicca qui: ")