Small things

This commit is contained in:
Richard Chien 2018-07-06 19:07:02 +08:00
parent 1b3980b5d2
commit c4b99e1a1c
3 changed files with 29 additions and 7 deletions

View File

@ -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

View File

@ -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))

View File

@ -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