Add argfilter type "controllers"

This commit is contained in:
Richard Chien 2019-02-21 21:57:05 +08:00
parent 838213d438
commit ab4d12d214
2 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,34 @@
import re
from nonebot import CommandSession
from nonebot.helpers import render_expression
def handle_cancellation(session: CommandSession):
"""
If the input is a string of cancellation word, finish the command session.
"""
def control(value):
if _is_cancellation(value):
session.finish(render_expression(
session.bot.config.SESSION_CANCEL_EXPRESSION))
return value
return control
async def _is_cancellation(sentence: str) -> bool:
for kw in ('', '', '', '', '取消'):
if kw in sentence:
# a keyword matches
break
else:
# no keyword matches
return False
if re.match(r'^那?[算别不停]\w{0,3}了?吧?$', sentence) or \
re.match(r'^那?(?:[给帮]我)?取消了?吧?$', sentence):
return True
return False

View File

@ -33,6 +33,7 @@ COMMAND_SEP: Iterable[Union[str, Pattern]] = {'/', '.'}
SESSION_EXPIRE_TIMEOUT: Optional[timedelta] = timedelta(minutes=5)
SESSION_RUN_TIMEOUT: Optional[timedelta] = None
SESSION_RUNNING_EXPRESSION: Expression_T = '您有命令正在执行,请稍后再试'
SESSION_CANCEL_EXPRESSION = '好的'
SHORT_MESSAGE_MAX_LENGTH: int = 50
DEFAULT_VALIDATION_FAILURE_EXPRESSION: Expression_T = '您的输入不符合要求,请重新输入'