1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +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): class NullCommand(Command):
@ -6,5 +6,5 @@ class NullCommand(Command):
command_name = "null" command_name = "null"
command_title = "Do nothing" command_title = "Do nothing"
async def common(self, call: Call, *args, **kwargs): async def common(self, call: Call, args: CommandArgs):
pass pass

View file

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

View file

@ -1,5 +1,5 @@
import re import re
from ..utils import Command, Call, safeformat from ..utils import Command, CommandArgs, Call, safeformat
SHIP_RESULT = "💕 {one} + {two} = <b>{result}</b>" SHIP_RESULT = "💕 {one} + {two} = <b>{result}</b>"
@ -10,7 +10,7 @@ class ShipCommand(Command):
command_name = "ship" command_name = "ship"
command_title = "Create a ship between two items" 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_one = args[0]
name_two = args[1] name_two = args[1]
if name_two == "+": if name_two == "+":

View file

@ -1,5 +1,5 @@
import random 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", 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_name = "smecds"
command_title = "Secondo me, è colpa dello stagista..." 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] ds = random.sample(DS_LIST, 1)[0]
return await call.reply(safeformat(SMECDS, ds=ds)) return await call.reply(safeformat(SMECDS, ds=ds))

View file

@ -1,6 +1,6 @@
from .asyncify import asyncify from .asyncify import asyncify
from .call import Call from .call import Call
from .command import Command, InvalidInputError, UnsupportedError from .command import Command, CommandArgs, InvalidInputError, UnsupportedError
from .safeformat import safeformat 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, CommandArgs
from .command import Command
class Call: class Call:
@ -25,4 +24,4 @@ class Call:
coroutine = getattr(self.command, self.interface_name) coroutine = getattr(self.command, self.interface_name)
except AttributeError: except AttributeError:
coroutine = getattr(self.command, "common") 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 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: class Command:
"""A generic command, called from any source.""" """A generic command, called from any source."""
command_name: str = NotImplemented command_name: str = NotImplemented
command_title: str = NotImplemented command_title: str = NotImplemented
async def common(self, call: "Call", *args, **kwargs): async def common(self, call: "Call", args: CommandArgs):
raise NotImplementedError() raise NotImplementedError()