From 92e032864ed38e00aa8774e675020ff8ca2f9b8a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 9 Apr 2019 00:08:14 +0200 Subject: [PATCH] Make commands @classmethods --- royalgames.py | 4 ++-- royalnet/commands/__init__.py | 3 ++- royalnet/commands/ciaoruozi.py | 7 ++++++- royalnet/commands/color.py | 3 ++- royalnet/commands/debug_author.py | 3 ++- royalnet/commands/debug_create.py | 3 ++- royalnet/commands/diario.py | 17 ++++++++--------- royalnet/commands/error_handler.py | 6 ++++-- royalnet/commands/null.py | 3 ++- royalnet/commands/ping.py | 3 ++- royalnet/commands/ship.py | 3 ++- royalnet/commands/smecds.py | 3 ++- royalnet/commands/sync.py | 6 ++++-- royalnet/utils/call.py | 2 +- 14 files changed, 41 insertions(+), 25 deletions(-) diff --git a/royalgames.py b/royalgames.py index d9be4d73..8589584e 100644 --- a/royalgames.py +++ b/royalgames.py @@ -1,7 +1,7 @@ import os import asyncio from royalnet.bots import TelegramBot -from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand, DiarioCommand +from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand, DiarioCommand, RageCommand from royalnet.commands.debug_create import DebugCreateCommand from royalnet.commands.debug_author import DebugAuthorCommand from royalnet.commands.error_handler import ErrorHandlerCommand @@ -11,7 +11,7 @@ from royalnet.database.tables import Royal, Telegram loop = asyncio.get_event_loop() commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, - DebugAuthorCommand, DiarioCommand] + DebugAuthorCommand, DiarioCommand, RageCommand] master = RoyalnetServer("localhost", 1234, "sas") tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, os.environ["DB_PATH"], Royal, Telegram, "tg_id", error_command=ErrorHandlerCommand) diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index edc01972..78f7657f 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -6,7 +6,8 @@ from .ciaoruozi import CiaoruoziCommand from .color import ColorCommand from .sync import SyncCommand from .diario import DiarioCommand +from .rage import RageCommand __all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", - "SyncCommand", "DiarioCommand"] + "SyncCommand", "DiarioCommand", "RageCommand"] diff --git a/royalnet/commands/ciaoruozi.py b/royalnet/commands/ciaoruozi.py index b30169e2..9a4ef70b 100644 --- a/royalnet/commands/ciaoruozi.py +++ b/royalnet/commands/ciaoruozi.py @@ -8,7 +8,12 @@ class CiaoruoziCommand(Command): command_description = "Saluta Ruozi, anche se non è più in RYG." command_syntax = "" - async def telegram(self, call: Call): + @classmethod + async def common(cls, call: "Call"): + await call.reply("👋 Ciao Ruozi!") + + @classmethod + async def telegram(cls, call: Call): update: Update = call.kwargs["update"] user: User = update.effective_user if user.id == 112437036: diff --git a/royalnet/commands/color.py b/royalnet/commands/color.py index 62daa487..4c920486 100644 --- a/royalnet/commands/color.py +++ b/royalnet/commands/color.py @@ -7,7 +7,8 @@ class ColorCommand(Command): command_description = "Invia un colore in chat...?" command_syntax = "" - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): await call.reply(""" [i]I am sorry, unknown error occured during working with your request, Admin were notified[/i] """) diff --git a/royalnet/commands/debug_author.py b/royalnet/commands/debug_author.py index 7d3f4a78..7400ee85 100644 --- a/royalnet/commands/debug_author.py +++ b/royalnet/commands/debug_author.py @@ -10,7 +10,8 @@ class DebugAuthorCommand(Command): require_alchemy_tables = {Royal, Telegram} - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): author = await call.get_author() if author is None: await call.reply(f"☁️ L'autore di questa chiamata è sconosciuto.") diff --git a/royalnet/commands/debug_create.py b/royalnet/commands/debug_create.py index e99fd448..faaedfb2 100644 --- a/royalnet/commands/debug_create.py +++ b/royalnet/commands/debug_create.py @@ -10,7 +10,8 @@ class DebugCreateCommand(Command): require_alchemy_tables = {Royal, Alias} - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): royal = call.alchemy.Royal(username=call.args[0], role="Member") call.session.add(royal) alias = call.alchemy.Alias(royal=royal, alias=royal.username.lower()) diff --git a/royalnet/commands/diario.py b/royalnet/commands/diario.py index a19eafbe..ac036efc 100644 --- a/royalnet/commands/diario.py +++ b/royalnet/commands/diario.py @@ -18,7 +18,8 @@ class DiarioCommand(Command): require_alchemy_tables = {Royal, Diario, Alias} - async def _telegram_to_imgur(self, photosizes: typing.List[telegram.PhotoSize], caption="") -> str: + @classmethod + async def _telegram_to_imgur(cls, photosizes: typing.List[telegram.PhotoSize], caption="") -> str: # Select the largest photo largest_photo = sorted(photosizes, key=lambda p: p.width * p.height)[-1] # Get the photo url @@ -41,7 +42,8 @@ class DiarioCommand(Command): raise ExternalError("imgur returned an error in the image upload.") return response["data"]["link"] - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): # Find the creator of the quotes creator = await call.get_author() if creator is None: @@ -94,7 +96,8 @@ class DiarioCommand(Command): await asyncify(call.session.commit) await call.reply(f"✅ {str(diario)}") - async def telegram(self, call: Call): + @classmethod + async def telegram(cls, call: Call): update: telegram.Update = call.kwargs["update"] message: telegram.Message = update.message reply: telegram.Message = message.reply_to_message @@ -110,9 +113,7 @@ class DiarioCommand(Command): if photosizes: # Text is a caption text = reply.caption - # Python is doing some weird stuff here, self._telegram_to_imgur appears to be unbound? - # noinspection PyArgumentList - media_url = await self._telegram_to_imgur(self, photosizes, text if text is not None else "") + media_url = await cls._telegram_to_imgur(photosizes, text if text is not None else "") else: media_url = None # Ensure there is a text or an image @@ -137,9 +138,7 @@ class DiarioCommand(Command): # Check if there's an image associated with the reply photosizes: typing.Optional[typing.List[telegram.PhotoSize]] = message.photo if photosizes: - # Python is doing some weird stuff here, self._telegram_to_imgur appears to be unbound? - # noinspection PyArgumentList - media_url = await self._telegram_to_imgur(self, photosizes, raw_text if raw_text is not None else "") + media_url = await cls._telegram_to_imgur(photosizes, raw_text if raw_text is not None else "") else: media_url = None # Parse the text, if it exists diff --git a/royalnet/commands/error_handler.py b/royalnet/commands/error_handler.py index 1df8bf0a..13f2a299 100644 --- a/royalnet/commands/error_handler.py +++ b/royalnet/commands/error_handler.py @@ -9,10 +9,12 @@ class ErrorHandlerCommand(Command): command_description = "Gestisce gli errori causati dagli altri comandi." command_syntax = "" - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): raise UnsupportedError() - async def telegram(self, call: Call): + @classmethod + async def telegram(cls, call: Call): try: e_type, e_value, e_tb = call.kwargs["exception_info"] except InvalidInputError: diff --git a/royalnet/commands/null.py b/royalnet/commands/null.py index 29811f57..441d090b 100644 --- a/royalnet/commands/null.py +++ b/royalnet/commands/null.py @@ -7,5 +7,6 @@ class NullCommand(Command): command_description = "Non fa nulla." command_syntax = "" - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): pass diff --git a/royalnet/commands/ping.py b/royalnet/commands/ping.py index 87d689e5..04ea42bd 100644 --- a/royalnet/commands/ping.py +++ b/royalnet/commands/ping.py @@ -8,7 +8,8 @@ class PingCommand(Command): command_description = "Ping pong!" command_syntax = "[time_to_wait]" - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): try: time = int(call.args[0]) except InvalidInputError: diff --git a/royalnet/commands/ship.py b/royalnet/commands/ship.py index 779d319a..c0b014af 100644 --- a/royalnet/commands/ship.py +++ b/royalnet/commands/ship.py @@ -11,7 +11,8 @@ class ShipCommand(Command): command_description = "Crea una ship tra due cose." command_syntax = "(uno) (due)" - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): name_one = call.args[0] name_two = call.args[1] if name_two == "+": diff --git a/royalnet/commands/smecds.py b/royalnet/commands/smecds.py index 437727e7..7e8bc21c 100644 --- a/royalnet/commands/smecds.py +++ b/royalnet/commands/smecds.py @@ -57,6 +57,7 @@ class SmecdsCommand(Command): command_description = "Secondo me, è colpa dello stagista..." command_syntax = "" - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): ds = random.sample(DS_LIST, 1)[0] return await call.reply(safeformat(SMECDS, ds=ds)) diff --git a/royalnet/commands/sync.py b/royalnet/commands/sync.py index 4ffa2820..2dc7dadd 100644 --- a/royalnet/commands/sync.py +++ b/royalnet/commands/sync.py @@ -12,10 +12,12 @@ class SyncCommand(Command): require_alchemy_tables = {Royal, Telegram} - async def common(self, call: Call): + @classmethod + async def common(cls, call: Call): raise UnsupportedError() - async def telegram(self, call: Call): + @classmethod + async def telegram(cls, call: Call): update: Update = call.kwargs["update"] # Find the user user: typing.Optional[User] = update.effective_user diff --git a/royalnet/utils/call.py b/royalnet/utils/call.py index 0b57781a..ab31c6bc 100644 --- a/royalnet/utils/call.py +++ b/royalnet/utils/call.py @@ -58,7 +58,7 @@ class Call: except AttributeError: coroutine = getattr(self.command, "common") try: - result = await coroutine(self.command, self) + result = await coroutine(self) finally: await self.session_end() return result