🐛 change matcher check-run

This commit is contained in:
yanyongyu 2021-02-01 11:42:05 +08:00
parent b140ebd149
commit f6c24ec92f
2 changed files with 26 additions and 40 deletions

View File

@ -99,47 +99,30 @@ def run_postprocessor(func: T_RunPostProcessor) -> T_RunPostProcessor:
return func return func
async def _check_matcher(priority: int, bot: "Bot", event: "Event", async def _check_matcher(priority: int, Matcher: Type[Matcher], bot: "Bot",
state: T_State) -> Iterable[Type[Matcher]]: event: "Event", state: T_State) -> None:
current_matchers = matchers[priority].copy() if Matcher.expire_time and datetime.now() > Matcher.expire_time:
async def _check(Matcher: Type[Matcher], bot: "Bot", event: "Event",
state: T_State) -> Optional[Type[Matcher]]:
try: try:
if (not Matcher.expire_time or datetime.now() <= Matcher.expire_time matchers[priority].remove(Matcher)
) and await Matcher.check_perm( except Exception:
bot, event) and await Matcher.check_rule(bot, event, state): pass
return Matcher return
try:
if not await Matcher.check_perm(
bot, event) or not await Matcher.check_rule(bot, event, state):
return
except Exception as e: except Exception as e:
logger.opt(colors=True, exception=e).error( logger.opt(colors=True, exception=e).error(
f"<r><bg #f8bbd0>Rule check failed for {Matcher}.</bg #f8bbd0></r>" f"<r><bg #f8bbd0>Rule check failed for {Matcher}.</bg #f8bbd0></r>")
)
return None
async def _check_expire(Matcher: Type[Matcher]) -> Optional[Type[Matcher]]: if Matcher.temp:
if Matcher.expire_time and datetime.now() > Matcher.expire_time:
return Matcher
return None
checking_tasks = [
_check(Matcher, bot, event, state) for Matcher in current_matchers
]
checking_expire_tasks = [
_check_expire(Matcher) for Matcher in current_matchers
]
results = await asyncio.gather(*checking_tasks)
expired = await asyncio.gather(*checking_expire_tasks)
for expired_matcher in filter(lambda x: x, expired):
try: try:
matchers[priority].remove(expired_matcher) # type: ignore matchers[priority].remove(Matcher)
except Exception: except Exception:
pass pass
for temp_matcher in filter(lambda x: x and x.temp, results):
try: await _run_matcher(Matcher, bot, event, state)
matchers[priority].remove(temp_matcher) # type: ignore
except Exception:
pass
return filter(lambda x: x, results) # type: ignore
async def _run_matcher(Matcher: Type[Matcher], bot: "Bot", event: "Event", async def _run_matcher(Matcher: Type[Matcher], bot: "Bot", event: "Event",
@ -244,11 +227,9 @@ async def handle_event(bot: "Bot", event: "Event"):
if show_log: if show_log:
logger.debug(f"Checking for matchers in priority {priority}...") logger.debug(f"Checking for matchers in priority {priority}...")
run_matchers = await _check_matcher(priority, bot, event, state)
pending_tasks = [ pending_tasks = [
_run_matcher(matcher, bot, event, state.copy()) _check_matcher(priority, matcher, bot, event, state.copy())
for matcher in run_matchers for matcher in matchers[priority]
] ]
results = await asyncio.gather(*pending_tasks, return_exceptions=True) results = await asyncio.gather(*pending_tasks, return_exceptions=True)

View File

@ -282,7 +282,8 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
根据正则表达式进行匹配 根据正则表达式进行匹配
可以通过 ``state["_matched"]`` 获取正则表达式匹配成功的文本 可以通过 ``state["_matched"]`` ``state["_matched_groups"]`` ``state["_matched_dict"]``
获取正则表达式匹配成功的文本
:参数: :参数:
@ -302,9 +303,13 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
matched = pattern.search(str(event.get_message())) matched = pattern.search(str(event.get_message()))
if matched: if matched:
state["_matched"] = matched.group() state["_matched"] = matched.group()
state["_matched_groups"] = matched.groups()
state["_matched_dict"] = matched.groupdict()
return True return True
else: else:
state["_matched"] = None state["_matched"] = None
state["_matched_groups"] = None
state["_matched_dict"] = None
return False return False
return Rule(_regex) return Rule(_regex)