mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
Implement ship command through the Unity interface
This commit is contained in:
parent
c0dfcae156
commit
55ba97e89a
4 changed files with 41 additions and 9 deletions
|
@ -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()
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from .null import NullCommand
|
||||
from .ping import PingCommand
|
||||
from .ship import ShipCommand
|
||||
|
||||
__all__ = ["NullCommand", "PingCommand"]
|
||||
__all__ = ["NullCommand", "PingCommand", "ShipCommand"]
|
||||
|
|
33
royalnet/commands/ship.py
Normal file
33
royalnet/commands/ship.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import re
|
||||
from ..utils import Command, Call
|
||||
|
||||
|
||||
SHIP_RESULT = "💕 {one} + {two} = <b>{result}</b>"
|
||||
|
||||
|
||||
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()))
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue