1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Make commands @classmethods

This commit is contained in:
Steffo 2019-04-09 00:08:14 +02:00
parent 8d0e6d8c5c
commit 92e032864e
14 changed files with 41 additions and 25 deletions

View file

@ -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)

View file

@ -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"]

View file

@ -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:

View file

@ -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]
""")

View file

@ -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.")

View file

@ -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())

View file

@ -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

View file

@ -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:

View file

@ -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

View file

@ -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:

View file

@ -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 == "+":

View file

@ -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))

View file

@ -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

View file

@ -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