mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 04:26:24 +08:00
Small things
This commit is contained in:
parent
1b3980b5d2
commit
c4b99e1a1c
@ -160,12 +160,20 @@ def _find_command(name: Union[str, Tuple[str]]) -> Optional[Command]:
|
|||||||
|
|
||||||
class _FurtherInteractionNeeded(Exception):
|
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.
|
enter interactive mode to ask the user for some arguments.
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class _FinishException(Exception):
|
||||||
|
"""
|
||||||
|
Raised by session.finish() indicating that the command session
|
||||||
|
should be stop and removed.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class CommandSession(BaseSession):
|
class CommandSession(BaseSession):
|
||||||
__slots__ = ('cmd', 'current_key', 'current_arg', 'current_arg_text',
|
__slots__ = ('cmd', 'current_key', 'current_arg', 'current_arg_text',
|
||||||
'current_arg_images', 'args', 'last_interaction')
|
'current_arg_images', 'args', 'last_interaction')
|
||||||
@ -229,14 +237,25 @@ class CommandSession(BaseSession):
|
|||||||
# ask the user for more information
|
# ask the user for more information
|
||||||
if prompt_expr is not None:
|
if prompt_expr is not None:
|
||||||
prompt = render(prompt_expr, key=key)
|
prompt = render(prompt_expr, key=key)
|
||||||
if prompt:
|
self.pause(prompt)
|
||||||
asyncio.ensure_future(self.send(prompt))
|
|
||||||
raise _FurtherInteractionNeeded
|
|
||||||
|
|
||||||
def get_optional(self, key: str,
|
def get_optional(self, key: str,
|
||||||
default: Optional[Any] = None) -> Optional[Any]:
|
default: Optional[Any] = None) -> Optional[Any]:
|
||||||
|
"""Simply get a argument with given key."""
|
||||||
return self.args.get(key, default)
|
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,
|
def parse_command(bot: NoneBot,
|
||||||
cmd_string: str) -> Tuple[Optional[Command], Optional[str]]:
|
cmd_string: str) -> Tuple[Optional[Command], Optional[str]]:
|
||||||
@ -388,3 +407,5 @@ async def _real_run_command(session: CommandSession,
|
|||||||
session.last_interaction = datetime.now()
|
session.last_interaction = datetime.now()
|
||||||
# return True because this step of the session is successful
|
# return True because this step of the session is successful
|
||||||
return True
|
return True
|
||||||
|
except _FinishException:
|
||||||
|
return True
|
||||||
|
@ -3,12 +3,12 @@ from aiocqhttp.message import unescape
|
|||||||
from none import on_command, CommandSession, permission as perm
|
from none import on_command, CommandSession, permission as perm
|
||||||
|
|
||||||
|
|
||||||
@on_command('echo', only_to_me=False)
|
@on_command('echo')
|
||||||
async def echo(session: CommandSession):
|
async def echo(session: CommandSession):
|
||||||
await session.send(session.get_optional('message') or session.current_arg)
|
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):
|
async def _(session: CommandSession):
|
||||||
await session.send(
|
await session.send(
|
||||||
unescape(session.get_optional('message') or session.current_arg))
|
unescape(session.get_optional('message') or session.current_arg))
|
||||||
|
@ -15,5 +15,6 @@ async def _(session: NLPSession):
|
|||||||
_last_session.msg == session.msg:
|
_last_session.msg == session.msg:
|
||||||
result = NLPResult(61.0, 'echo', {'message': _last_session.msg})
|
result = NLPResult(61.0, 'echo', {'message': _last_session.msg})
|
||||||
_last_session = None
|
_last_session = None
|
||||||
|
else:
|
||||||
_last_session = session
|
_last_session = session
|
||||||
return result
|
return result
|
||||||
|
Loading…
Reference in New Issue
Block a user