From ab4d12d21473ee2929c0d98771cbda63b91badf2 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Thu, 21 Feb 2019 21:57:05 +0800 Subject: [PATCH] Add argfilter type "controllers" --- nonebot/command/argfilter/controllers.py | 34 ++++++++++++++++++++++++ nonebot/default_config.py | 1 + 2 files changed, 35 insertions(+) create mode 100644 nonebot/command/argfilter/controllers.py diff --git a/nonebot/command/argfilter/controllers.py b/nonebot/command/argfilter/controllers.py new file mode 100644 index 00000000..b8b57173 --- /dev/null +++ b/nonebot/command/argfilter/controllers.py @@ -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 diff --git a/nonebot/default_config.py b/nonebot/default_config.py index 19cb9432..86e1b9e2 100644 --- a/nonebot/default_config.py +++ b/nonebot/default_config.py @@ -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 = '您的输入不符合要求,请重新输入'