From 8faee1151f990861aa9c9dfbed3ac501f99a3952 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Sun, 24 Jun 2018 23:00:37 +0800 Subject: [PATCH] Some new things --- none/command.py | 26 +++++++++++++++++++------- plugins/base.py | 15 +++++++++++---- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/none/command.py b/none/command.py index 7b3a618e..084b0b6b 100644 --- a/none/command.py +++ b/none/command.py @@ -74,17 +74,26 @@ def _find_command(name: Tuple[str]) -> Optional[Command]: class Session: - __slots__ = ('cmd', 'arg', 'arg_text', - 'images', 'data', 'last_interaction') + __slots__ = ('cmd', 'ctx', + 'current_key', 'current_arg', 'current_arg_text', + 'images', 'args', 'last_interaction') - def __init__(self, cmd: Command, arg: str = ''): + def __init__(self, cmd: Command, ctx: Dict[str, Any], + current_arg: str = ''): self.cmd = cmd - self.arg = arg - self.arg_text = Message(arg).extract_plain_text() + self.ctx = ctx + self.current_key = None + self.current_arg = current_arg + self.current_arg_text = Message(current_arg).extract_plain_text() self.images = [] - self.data = {} + self.args = {} self.last_interaction = None + def require_arg(self, key: str, *, + interactive: bool = True, prompt: str = ''): + # TODO: 检查 key 是否在 args 中,如果不在,抛出异常,保存 session,等待用户填充 + pass + async def handle_command(bot: CQHttp, ctx: Dict[str, Any]) -> bool: # TODO: check if there is a session @@ -126,7 +135,7 @@ async def handle_command(bot: CQHttp, ctx: Dict[str, Any]) -> bool: if not cmd: return False - session = Session(cmd=cmd, arg=''.join(cmd_remained)) + 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) @@ -150,6 +159,9 @@ def on_command(name: Union[str, Tuple[str]], aliases: Iterable = (), name=cmd_name, func=func, permission=permission) for alias in aliases: _aliases[alias] = cmd_name + + # TODO: 给 func 添加一个 argparser 装饰器,用于注册它的参数解析器 + return func return deco diff --git a/plugins/base.py b/plugins/base.py index c217efbf..1c704775 100644 --- a/plugins/base.py +++ b/plugins/base.py @@ -2,14 +2,21 @@ from aiocqhttp.message import unescape import none from none import permissions as perm +from none.command import Session from none.helpers import send @none.on_command('echo') -async def _(bot, ctx, session): - await send(bot, ctx, session.arg) +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 @none.on_command('say', permission=perm.SUPERUSER) -async def _(bot, ctx, session): - await send(bot, ctx, unescape(session.arg)) +async def _(bot, ctx, session: Session): + await send(bot, session.ctx, unescape(session.current_arg))