From 844ade5c3134260703808215ef61d36908946010 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Mon, 25 Jun 2018 10:41:48 +0800 Subject: [PATCH] Fix args and return values --- none/command.py | 15 ++++++++------- plugins/base.py | 12 +++--------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/none/command.py b/none/command.py index 084b0b6b..080561e2 100644 --- a/none/command.py +++ b/none/command.py @@ -27,8 +27,9 @@ class Command: self.func = func self.permission = permission - async def run(self, bot, ctx, session) -> Any: + async def run(self, bot, session) -> bool: permission = 0 + ctx = session.ctx if ctx['user_id'] in bot.config.SUPERUSERS: permission |= perm.IS_SUPERUSER if ctx['message_type'] == 'private': @@ -56,8 +57,9 @@ class Command: permission |= perm.IS_DISCUSS if isinstance(self.func, Callable) and permission & self.permission: - return await self.func(bot, ctx, session) - return None + await self.func(bot, session) + return True + return False def _find_command(name: Tuple[str]) -> Optional[Command]: @@ -89,8 +91,8 @@ class Session: self.args = {} self.last_interaction = None - def require_arg(self, key: str, *, - interactive: bool = True, prompt: str = ''): + def require_arg(self, key: str, prompt: str = '', *, + interactive: bool = True): # TODO: 检查 key 是否在 args 中,如果不在,抛出异常,保存 session,等待用户填充 pass @@ -138,8 +140,7 @@ async def handle_command(bot: CQHttp, ctx: Dict[str, Any]) -> bool: session = Session(cmd=cmd, ctx=ctx, current_arg=''.join(cmd_remained)) session.images = [s.data['url'] for s in ctx['message'] if s.type == 'image' and 'url' in s.data] - await cmd.run(bot, ctx, session) - return True + return await cmd.run(bot, session) def on_command(name: Union[str, Tuple[str]], aliases: Iterable = (), diff --git a/plugins/base.py b/plugins/base.py index 1c704775..9743673c 100644 --- a/plugins/base.py +++ b/plugins/base.py @@ -7,16 +7,10 @@ from none.helpers import send @none.on_command('echo') -async def echo(bot, ctx, session: Session): - text = session.require_arg('text') - await send(bot, session.ctx, text) - - -@echo.argparser -def _(session: Session): - session.args['text'] = session.current_arg +async def echo(bot, session: Session): + await send(bot, session.ctx, session.current_arg) @none.on_command('say', permission=perm.SUPERUSER) -async def _(bot, ctx, session: Session): +async def _(bot, session: Session): await send(bot, session.ctx, unescape(session.current_arg))