MCPcopy
hub / github.com/nonebot/nonebot2 / handle_event

Function handle_event

nonebot/message.py:519–606  ·  view source on GitHub ↗

处理一个事件。调用该函数以实现分发事件。 参数: bot: Bot 对象 event: Event 对象 用法: ```python driver.task_group.start_soon(handle_event, bot, event) ```

(bot: "Bot", event: "Event")

Source from the content-addressed store, hash-verified

517
518
519async def handle_event(bot: "Bot", event: "Event") -> None:
520 """处理一个事件。调用该函数以实现分发事件。
521
522 参数:
523 bot: Bot 对象
524 event: Event 对象
525
526 用法:
527 ```python
528 driver.task_group.start_soon(handle_event, bot, event)
529 ```
530 """
531 show_log = True
532 log_msg = f"<m>{escape_tag(bot.type)} {escape_tag(bot.self_id)}</m> | "
533 try:
534 log_msg += event.get_log_string()
535 except NoLogException:
536 show_log = False
537 if show_log:
538 logger.opt(colors=True).success(log_msg)
539
540 state: dict[Any, Any] = {}
541 dependency_cache: T_DependencyCache = {}
542
543 # create event scope context
544 async with AsyncExitStack() as stack:
545 if not await _apply_event_preprocessors(
546 bot=bot,
547 event=event,
548 state=state,
549 stack=stack,
550 dependency_cache=dependency_cache,
551 ):
552 return
553
554 # Trie Match
555 try:
556 TrieRule.get_value(bot, event, state)
557 except Exception as e:
558 logger.opt(colors=True, exception=e).warning(
559 "Error while parsing command for event"
560 )
561
562 break_flag = False
563
564 def _handle_stop_propagation(exc_group: BaseExceptionGroup) -> None:
565 nonlocal break_flag
566
567 break_flag = True
568 logger.debug("Stop event propagation")
569
570 # iterate through all priority until stop propagation
571 for priority in sorted(matchers.keys()):
572 if break_flag:
573 break
574
575 if show_log:
576 logger.debug(f"Checking for matchers in priority {priority}...")

Callers

nothing calls this directly

Calls 9

escape_tagFunction · 0.90
_handle_exceptionFunction · 0.85
check_and_run_matcherFunction · 0.85
get_log_stringMethod · 0.80
get_valueMethod · 0.80
keysMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected