add matched_string when rule regex

当使用正则匹配消息成功时,向state添加matched_string以保存匹配到的内容供接下来使用。
This commit is contained in:
rkroom 2020-10-26 17:18:26 +08:00 committed by GitHub
parent c85e50d73d
commit 59a8bd8c97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -247,8 +247,12 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
pattern = re.compile(regex, flags) pattern = re.compile(regex, flags)
async def _regex(bot: Bot, event: Event, state: dict) -> bool: async def _regex(bot: Bot, event: Event, state: dict) -> bool:
return bool(pattern.search(str(event.message))) matched_string = pattern.search(str(event.message))
if matched_string:
state["matched_string"] = matched_string.group()
return True
else:
return False
return Rule(_regex) return Rule(_regex)