mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-27 16:21:28 +00:00
Small things
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user