From 55ba97e89a713de099ed51b8f4a9943a08c292e0 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 15 Mar 2019 12:02:00 +0100 Subject: [PATCH] Implement ship command through the Unity interface --- royalnet/bots/telegram.py | 2 +- royalnet/commands/__init__.py | 3 ++- royalnet/commands/ship.py | 33 +++++++++++++++++++++++++++++++++ royalnet/utils/call.py | 12 +++++------- 4 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 royalnet/commands/ship.py diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index 153c19b8..218695a1 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -61,4 +61,4 @@ class TelegramBot: # Skip inexistent commands command = self.missing_command # Call the command - return await self.Call(message.chat, command, parameters).run() + return await self.Call(message.chat, command, *parameters).run() diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index 0fc0550e..29a94302 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -1,4 +1,5 @@ from .null import NullCommand from .ping import PingCommand +from .ship import ShipCommand -__all__ = ["NullCommand", "PingCommand"] +__all__ = ["NullCommand", "PingCommand", "ShipCommand"] diff --git a/royalnet/commands/ship.py b/royalnet/commands/ship.py new file mode 100644 index 00000000..d0451a66 --- /dev/null +++ b/royalnet/commands/ship.py @@ -0,0 +1,33 @@ +import re +from ..utils import Command, Call + + +SHIP_RESULT = "💕 {one} + {two} = {result}" + + +class ShipCommand(Command): + + command_name = "ship" + command_title = "Create a ship between two items" + + async def common(self, call: Call, *args, **kwargs): + name_one = args[0] + name_two = args[1] + if name_two == "+": + name_two = args[2] + name_one = name_one.lower() + name_two = name_two.lower() + match_one = re.search(r"^[A-Za-z][^aeiouAEIOU]*[aeiouAEIOU]?", name_one) + if match_one is None: + part_one = name_one[:int(len(name_one) / 2)] + else: + part_one = match_one.group(0) + match_two = re.search(r"[^aeiouAEIOU]*[aeiouAEIOU]?[A-Za-z]$", name_two) + if match_two is None: + part_two = name_two[int(len(name_two) / 2):] + else: + part_two = match_two.group(0) + mixed = part_one + part_two # TODO: find out what exceptions this could possibly raise + await call.reply(SHIP_RESULT.format(one=name_one.capitalize(), + two=name_two.capitalize(), + result=mixed.capitalize())) diff --git a/royalnet/utils/call.py b/royalnet/utils/call.py index 045ea4fb..f8f8853c 100644 --- a/royalnet/utils/call.py +++ b/royalnet/utils/call.py @@ -14,17 +14,15 @@ class Call: raise NotImplementedError() # These parameters / methods should be left alone - def __init__(self, channel, command: Command, parameters: typing.List[str]=None): + def __init__(self, channel, command: Command, *args, **kwargs): self.channel = channel self.command = command - if parameters is None: - self.parameters = [] - else: - self.parameters = parameters + self.args = args + self.kwargs = kwargs - async def run(self, *args, **kwargs): + async def run(self): try: coroutine = getattr(self.command, self.interface_name) except AttributeError: coroutine = getattr(self.command, "common") - return await coroutine(self.command, self, *args, **kwargs) + return await coroutine(self.command, self, *self.args, **self.kwargs)