1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Create CommandArgs class

This commit is contained in:
Steffo 2019-03-15 15:31:17 +01:00
parent 144ff53752
commit d42c1a6cd7
7 changed files with 78 additions and 59 deletions

View file

@ -1,4 +1,4 @@
from ..utils import Command, Call
from ..utils import Command, CommandArgs, Call
class NullCommand(Command):
@ -6,5 +6,5 @@ class NullCommand(Command):
command_name = "null"
command_title = "Do nothing"
async def common(self, call: Call, *args, **kwargs):
async def common(self, call: Call, args: CommandArgs):
pass

View file

@ -1,4 +1,4 @@
from ..utils import Command, Call
from ..utils import Command, CommandArgs, Call
class PingCommand(Command):
@ -6,5 +6,5 @@ class PingCommand(Command):
command_name = "ping"
command_title = "Ping pong!"
async def common(self, call: Call, *args, **kwargs):
async def common(self, call: Call, args: CommandArgs):
await call.reply("Pong!")

View file

@ -1,5 +1,5 @@
import re
from ..utils import Command, Call, safeformat
from ..utils import Command, CommandArgs, Call, safeformat
SHIP_RESULT = "💕 {one} + {two} = <b>{result}</b>"
@ -10,7 +10,7 @@ class ShipCommand(Command):
command_name = "ship"
command_title = "Create a ship between two items"
async def common(self, call: Call, *args, **kwargs):
async def common(self, call: Call, args: CommandArgs):
name_one = args[0]
name_two = args[1]
if name_two == "+":

View file

@ -1,5 +1,5 @@
import random
from ..utils import Command, Call, safeformat
from ..utils import Command, CommandArgs, Call, safeformat
DS_LIST = ["della secca", "del seccatore", "del secchiello", "del secchio", "del secchione", "del secondino",
@ -56,6 +56,6 @@ class SmecdsCommand(Command):
command_name = "smecds"
command_title = "Secondo me, è colpa dello stagista..."
async def common(self, call: Call, *args, **kwargs):
async def common(self, call: Call, args: CommandArgs):
ds = random.sample(DS_LIST, 1)[0]
return await call.reply(safeformat(SMECDS, ds=ds))

View file

@ -1,6 +1,6 @@
from .asyncify import asyncify
from .call import Call
from .command import Command, InvalidInputError, UnsupportedError
from .command import Command, CommandArgs, InvalidInputError, UnsupportedError
from .safeformat import safeformat
__all__ = ["asyncify", "Call", "Command", "safeformat", "InvalidInputError", "UnsupportedError"]
__all__ = ["asyncify", "Call", "Command", "safeformat", "InvalidInputError", "UnsupportedError", "CommandArgs"]

View file

@ -1,5 +1,4 @@
import typing
from .command import Command
from .command import Command, CommandArgs
class Call:
@ -25,4 +24,4 @@ class Call:
coroutine = getattr(self.command, self.interface_name)
except AttributeError:
coroutine = getattr(self.command, "common")
return await coroutine(self.command, self, *self.args, **self.kwargs)
return await coroutine(self.command, self, CommandArgs(*self.args, **self.kwargs))

View file

@ -13,11 +13,31 @@ class InvalidInputError(Exception):
pass
class CommandArgs:
"""The arguments of a command. Raises InvalidInputError if the requested argument does not exist."""
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
def __getitem__(self, item):
if isinstance(item, int):
try:
return self.args[item]
except IndexError:
raise InvalidInputError(f'Tried to get missing [{item}] arg from CommandArgs')
elif isinstance(item, str):
try:
return self.kwargs[item]
except IndexError:
raise InvalidInputError(f'Tried to get missing ["{item}"] kwarg from CommandArgs')
raise ValueError(f"Invalid type passed to CommandArgs.__getattr__: {type(item)}")
class Command:
"""A generic command, called from any source."""
command_name: str = NotImplemented
command_title: str = NotImplemented
async def common(self, call: "Call", *args, **kwargs):
async def common(self, call: "Call", args: CommandArgs):
raise NotImplementedError()