增加样例插件,修复统计数据(部分),修复一个bug

This commit is contained in:
UnCLAS-Prommer
2025-07-19 00:46:04 +08:00
parent 7895cac8c2
commit 400ffd0b53
6 changed files with 73 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ logger = get_logger("plugin_register")
def register_plugin(cls):
from src.plugin_system.core.plugin_manager import plugin_manager
from src.plugin_system.base.base_plugin import BasePlugin
from src.plugin_system.base.base_event_plugin import BaseEventPlugin
"""插件注册装饰器
@@ -18,7 +19,7 @@ def register_plugin(cls):
plugin_description = "我的插件"
...
"""
if not issubclass(cls, BasePlugin):
if not issubclass(cls, BasePlugin) and not issubclass(cls, BaseEventPlugin):
logger.error(f"{cls.__name__} 不是 BasePlugin 的子类")
return cls

View File

@@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
from typing import Tuple, Optional
from src.common.logger import get_logger
from .component_types import MaiMessages, EventType
from .component_types import MaiMessages, EventType, EventHandlerInfo, ComponentType
logger = get_logger("base_event_handler")
@@ -14,7 +14,7 @@ class BaseEventHandler(ABC):
"""
event_type: EventType = EventType.UNKNOWN # 事件类型,默认为未知
handler_name: str = ""
handler_name: str = "" # 处理器名称
handler_description: str = ""
weight: int = 0 # 权重,数值越大优先级越高
intercept_message: bool = False # 是否拦截消息,默认为否
@@ -32,3 +32,17 @@ class BaseEventHandler(ABC):
Tuple[bool, Optional[str]]: (是否执行成功, 可选的返回消息)
"""
raise NotImplementedError("子类必须实现 execute 方法")
@classmethod
def get_handler_info(cls) -> "EventHandlerInfo":
"""获取事件处理器的信息"""
# 从类属性读取名称,如果没有定义则使用类名自动生成
name: str = getattr(cls, "handler_name", cls.__name__.lower().replace("handler", ""))
return EventHandlerInfo(
name=name,
component_type=ComponentType.LISTENER,
description=getattr(cls, "handler_description", "events处理器"),
event_type=cls.event_type,
weight=cls.weight,
intercept_message=cls.intercept_message,
)

View File

@@ -512,6 +512,12 @@ class PluginManager:
config_status = "" if self.plugin_paths.get(plugin_name) else ""
logger.info(f" ⚙️ 配置: {plugin_info.config_file} {config_status}")
root_path = Path(__file__)
# 查找项目根目录
while not (root_path / "pyproject.toml").exists() and root_path.parent != root_path:
root_path = root_path.parent
# 显示目录统计
logger.info("📂 加载目录统计:")
for directory in self.plugin_directories:
@@ -519,7 +525,11 @@ class PluginManager:
plugins_in_dir = []
for plugin_name in self.loaded_plugins.keys():
plugin_path = self.plugin_paths.get(plugin_name, "")
if plugin_path.startswith(directory):
if (
Path(plugin_path)
.resolve()
.is_relative_to(Path(os.path.join(str(root_path), directory)).resolve())
):
plugins_in_dir.append(plugin_name)
if plugins_in_dir: