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