From 9548409bd596ca3b59b30585c82fc24fa9068cf4 Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Tue, 15 Oct 2024 15:09:32 +0800 Subject: [PATCH] fix(event): refine handler invocation and improve class loading checks --- app/core/event.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/app/core/event.py b/app/core/event.py index 90dd5237..43ea8f7c 100644 --- a/app/core/event.py +++ b/app/core/event.py @@ -409,18 +409,29 @@ class EventManager(metaclass=Singleton): """ # 检查类是否在全局变量中 if class_name in globals(): - class_obj = globals()[class_name]() - else: - # 如果类不在全局变量中,尝试动态导入模块并创建实例 - # 导入模块,除了插件和Command,只有chain能响应事件 try: - module = importlib.import_module(f"app.chain.{class_name[:-5].lower()}") - class_obj = getattr(module, class_name)() + class_obj = globals()[class_name]() + return class_obj except Exception as e: - logger.error(f"事件处理出错:{str(e)} - {traceback.format_exc()}") + logger.error(f"事件处理出错:创建全局类实例出错:{str(e)} - {traceback.format_exc()}") return None - return class_obj + # 如果类不在全局变量中,尝试动态导入模块并创建实例 + try: + # 导入模块,除了插件和Command,只有chain能响应事件 + if not class_name.endswith("Chain"): + logger.debug(f"事件处理出错:无效的 Chain 类名: {class_name},类名必须以 'Chain' 结尾") + return None + module_name = f"app.chain.{class_name[:-5].lower()}" + module = importlib.import_module(module_name) + if hasattr(module, class_name): + class_obj = getattr(module, class_name)() + return class_obj + else: + logger.debug(f"事件处理出错:模块 {module_name} 中没有找到类 {class_name}") + except Exception as e: + logger.error(f"事件处理出错:{str(e)} - {traceback.format_exc()}") + return None def __broadcast_consumer_loop(self): """