继续尝试迁移,但是组件获取插件配置存在问题

This commit is contained in:
Windpicker-owo
2025-09-06 05:45:00 +08:00
parent f5fa235823
commit 03d1f7c57c
11 changed files with 94 additions and 584 deletions

View File

@@ -23,17 +23,20 @@ class BaseEventHandler(ABC):
"""是否拦截消息,默认为否"""
init_subscribe: List[Union[EventType, str]] = [EventType.UNKNOWN]
"""初始化时订阅的事件名称"""
plugin_name = None
def __init__(self):
self.log_prefix = "[EventHandler]"
"""对应插件名"""
self.plugin_config: Optional[Dict] = None
"""插件配置字典"""
self.subscribed_events = []
"""订阅的事件列表"""
if EventType.UNKNOWN in self.init_subscribe:
raise NotImplementedError("事件处理器必须指定 event_type")
from src.plugin_system.core.component_registry import component_registry
self.plugin_config = component_registry.get_plugin_config(self.plugin_name)
@abstractmethod
async def execute(self, kwargs: dict | None) -> Tuple[bool, bool, Optional[str]]:
"""执行事件处理的抽象方法,子类必须实现
@@ -89,15 +92,7 @@ class BaseEventHandler(ABC):
weight=cls.weight,
intercept_message=cls.intercept_message,
)
def set_plugin_config(self, plugin_config: Dict) -> None:
"""设置插件配置
Args:
plugin_config (dict): 插件配置字典
"""
self.plugin_config = plugin_config
def set_plugin_name(self, plugin_name: str) -> None:
"""设置插件名称

View File

@@ -248,6 +248,7 @@ class ComponentRegistry:
logger.error(f"注册失败: {handler_name} 不是有效的EventHandler")
return False
handler_class.plugin_name = handler_info.plugin_name
self._event_handler_registry[handler_name] = handler_class
if not handler_info.enabled:

View File

@@ -145,11 +145,12 @@ class EventManager:
logger.info(f"事件 {event_name} 已禁用")
return True
def register_event_handler(self, handler_class: Type[BaseEventHandler]) -> bool:
def register_event_handler(self, handler_class: Type[BaseEventHandler], plugin_config: Optional[dict] = None) -> bool:
"""注册事件处理器
Args:
handler_class (Type[BaseEventHandler]): 事件处理器类
plugin_config (Optional[dict]): 插件配置字典默认为None
Returns:
bool: 注册成功返回True已存在返回False
@@ -163,7 +164,12 @@ class EventManager:
logger.warning(f"事件处理器 {handler_name} 已存在,跳过注册")
return False
self._event_handlers[handler_name] = handler_class()
# 创建事件处理器实例,传递插件配置
handler_instance = handler_class()
if plugin_config is not None and hasattr(handler_instance, 'set_plugin_config'):
handler_instance.set_plugin_config(plugin_config)
self._event_handlers[handler_name] = handler_instance
# 处理init_subscribe缓存失败的订阅
if self._event_handlers[handler_name].init_subscribe: