class Aliases:
@property
def commands(self) -> tuple:
return (
self.new_alias,
)
@command(
command="новый алиас",
args={"алиас": "str"}
)
def new_alias(self, message: types.Message):
...
def command(**kwargs):
def wrapper(func_ref):
func_ref.command_kwargs = kwargs
return func_ref
return wrapper
class Aliases:
def __init__(self, bot: Bot):
self.bot = bot
@property
def commands(self) -> tuple:
return (
self.new_alias,
)
@command(
name="новый алиас",
args={"алиас": "str"}
)
def new_alias(self, message: types.Message = None):
...
def setup(bot: Bot, dp: Dispatcher):
aliases = Aliases(bot)
for function in aliases.commands:
print(function, function.command_kwargs)
<\bound method Aliases.new_alias of <\aliases.py.Aliases object at 0x7f4b0d0c2560>> {'name': 'новый алиас', 'args': {'алиас': 'str'}}
from functools import wraps, partial
def command(**cmdargs):
def _run(func):
@wraps(func)
def wrapper(self, *args):
return partial(func, self, args, cmdargs)
return wrapper
return _run
class Aliases:
@property
def commands(self) -> tuple:
return (
self.new_alias(),
)
@command(
command="новый алиас",
args={"алиас": "str"}
)
def new_alias(self, *args):
for arg in args:
if isinstance(arg, str): # заменить на types.Message):
return arg
return args
>>> aliases = Aliases()
>>> aliases.commands
(functools.partial(<function Aliases.new_alias at 0x778154c550>, <__main__.Aliases object at 0x778205af50>, (), {'command': 'новый алиас', 'args': {'алиас': 'str'}}),)
>>> aliases.commands[0]
functools.partial(<function Aliases.new_alias at 0x778154c550>, <__main__.Aliases object at 0x778205af50>, (), {'command': 'новый алиас', 'args': {'алиас': 'str'}})
>>> aliases.commands[0].args[2]
{'command': 'новый алиас', 'args': {'алиас': 'str'}}
>>> aliases.commands[0]("types.Message object")
'types.Message object'