diff --git a/none/command.py b/none/command.py index b2dabc82..c7d6e004 100644 --- a/none/command.py +++ b/none/command.py @@ -160,12 +160,20 @@ def _find_command(name: Union[str, Tuple[str]]) -> Optional[Command]: class _FurtherInteractionNeeded(Exception): """ - Raised by session.get() indicating that the command should + Raised by session.pause() indicating that the command should enter interactive mode to ask the user for some arguments. """ pass +class _FinishException(Exception): + """ + Raised by session.finish() indicating that the command session + should be stop and removed. + """ + pass + + class CommandSession(BaseSession): __slots__ = ('cmd', 'current_key', 'current_arg', 'current_arg_text', 'current_arg_images', 'args', 'last_interaction') @@ -229,14 +237,25 @@ class CommandSession(BaseSession): # ask the user for more information if prompt_expr is not None: prompt = render(prompt_expr, key=key) - if prompt: - asyncio.ensure_future(self.send(prompt)) - raise _FurtherInteractionNeeded + self.pause(prompt) def get_optional(self, key: str, default: Optional[Any] = None) -> Optional[Any]: + """Simply get a argument with given key.""" return self.args.get(key, default) + def pause(self, message=None) -> None: + """Pause the session for further interaction.""" + if message: + asyncio.ensure_future(self.send(message)) + raise _FurtherInteractionNeeded + + def finish(self, message=None) -> None: + """Finish the session.""" + if message: + asyncio.ensure_future(self.send(message)) + raise _FinishException + def parse_command(bot: NoneBot, cmd_string: str) -> Tuple[Optional[Command], Optional[str]]: @@ -388,3 +407,5 @@ async def _real_run_command(session: CommandSession, session.last_interaction = datetime.now() # return True because this step of the session is successful return True + except _FinishException: + return True diff --git a/none/plugins/base.py b/none/plugins/base.py index 73d79427..0347e3e0 100644 --- a/none/plugins/base.py +++ b/none/plugins/base.py @@ -3,12 +3,12 @@ from aiocqhttp.message import unescape from none import on_command, CommandSession, permission as perm -@on_command('echo', only_to_me=False) +@on_command('echo') async def echo(session: CommandSession): await session.send(session.get_optional('message') or session.current_arg) -@on_command('say', permission=perm.SUPERUSER, only_to_me=False) +@on_command('say', permission=perm.SUPERUSER) async def _(session: CommandSession): await session.send( unescape(session.get_optional('message') or session.current_arg)) diff --git a/none_demo/plugins/repeater.py b/none_demo/plugins/repeater.py index 4468a70f..2dac60ed 100644 --- a/none_demo/plugins/repeater.py +++ b/none_demo/plugins/repeater.py @@ -15,5 +15,6 @@ async def _(session: NLPSession): _last_session.msg == session.msg: result = NLPResult(61.0, 'echo', {'message': _last_session.msg}) _last_session = None - _last_session = session + else: + _last_session = session return result