加入bot,自动注入内容,在项目启动完成之后,给每个插件注入bot

This commit is contained in:
liuwei
2025-05-20 15:10:26 +08:00
parent 322297a69c
commit da89eea4f1
6 changed files with 69 additions and 53 deletions

View File

@@ -8,55 +8,55 @@ from typing import Dict, Any, List, Optional, Tuple
class PluginStatus(Enum):
"""插件状态枚举"""
UNLOADED = 0 # 未加载
LOADED = 1 # 已加载但未启动
RUNNING = 2 # 运行中
STOPPED = 3 # 已停止
ERROR = 4 # 错误状态
UNLOADED = 0 # 未加载
LOADED = 1 # 已加载但未启动
RUNNING = 2 # 运行中
STOPPED = 3 # 已停止
ERROR = 4 # 错误状态
class PluginInterface(ABC):
"""插件基础接口,所有插件必须实现此接口"""
@property
@abstractmethod
def name(self) -> str:
"""插件名称"""
pass
@property
@abstractmethod
def version(self) -> str:
"""插件版本"""
pass
@property
@abstractmethod
def description(self) -> str:
"""插件描述"""
pass
@property
@abstractmethod
def author(self) -> str:
"""插件作者"""
pass
@property
def dependencies(self) -> List[str]:
"""插件依赖,返回依赖的其他插件名称列表"""
return []
@property
def status(self) -> PluginStatus:
"""获取插件当前状态"""
return self._status
@status.setter
def status(self, value: PluginStatus):
"""设置插件状态"""
self._status = value
def __init__(self):
"""初始化插件"""
self._status = PluginStatus.UNLOADED
@@ -64,7 +64,8 @@ class PluginInterface(ABC):
self._plugin_path = ""
# 初始化日志记录器
self.LOG = logger
self.bot = None
def load_config(self) -> bool:
"""
从插件目录下的config.toml加载配置
@@ -87,7 +88,7 @@ class PluginInterface(ABC):
except Exception as e:
self.LOG.info(f"加载插件配置失败: {e}")
return False
def set_plugin_path(self, path: str) -> None:
"""
设置插件路径
@@ -96,7 +97,7 @@ class PluginInterface(ABC):
path: 插件路径
"""
self._plugin_path = path
def get_plugin_path(self) -> str:
"""
获取插件路径
@@ -105,7 +106,7 @@ class PluginInterface(ABC):
插件路径
"""
return self._plugin_path
@abstractmethod
def initialize(self, context: Dict[str, Any]) -> bool:
"""
@@ -118,7 +119,7 @@ class PluginInterface(ABC):
初始化是否成功
"""
pass
@abstractmethod
def start(self) -> bool:
"""
@@ -128,7 +129,7 @@ class PluginInterface(ABC):
启动是否成功
"""
pass
@abstractmethod
def stop(self) -> bool:
"""
@@ -138,7 +139,7 @@ class PluginInterface(ABC):
停止是否成功
"""
pass
def configure(self, config: Dict[str, Any]) -> bool:
"""
配置插件
@@ -151,7 +152,7 @@ class PluginInterface(ABC):
"""
self._config.update(config)
return True
def get_config(self) -> Dict[str, Any]:
"""
获取插件配置
@@ -160,7 +161,7 @@ class PluginInterface(ABC):
插件配置
"""
return self._config
def cleanup(self) -> bool:
"""
清理插件资源,在卸载前调用
@@ -169,10 +170,10 @@ class PluginInterface(ABC):
清理是否成功
"""
return True
def get_config_path(self) -> str:
return os.path.join(self._plugin_path, 'config.toml')
def can_process(self, data: Any) -> bool:
"""检查插件是否可以处理给定的数据
@@ -199,4 +200,4 @@ class PluginInterface(ABC):
Returns:
(是否已处理, 处理结果)
"""
raise NotImplementedError("子类必须实现此方法")
raise NotImplementedError("子类必须实现此方法")