插件化项目优化,支持将代码改造为插件,支持自动加载

This commit is contained in:
liuwei
2025-03-18 13:57:39 +08:00
parent 3c757161c4
commit bcca2dab28
13 changed files with 1033 additions and 388 deletions

View File

@@ -0,0 +1,47 @@
from typing import Dict, Any, Tuple, Optional, List
from plugin_common.plugin_interface import PluginInterface
class MessagePluginInterface(PluginInterface):
"""消息处理插件接口"""
@property
def command_prefix(self) -> Optional[str]:
"""命令前缀,如 '/'"""
return None
@property
def commands(self) -> List[str]:
"""支持的命令列表"""
return []
def can_process(self, message: Dict[str, Any]) -> bool:
"""
检查插件是否可以处理该消息
Args:
message: 消息字典,包含消息的各种属性
Returns:
是否可以处理
"""
# 默认实现:检查是否是命令
if self.command_prefix and self.commands:
content = message.get("content", "")
if content.startswith(self.command_prefix):
command = content[len(self.command_prefix):].split()[0]
return command in self.commands
return False
def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""
处理消息
Args:
message: 消息字典,包含消息的各种属性,以及发送消息所需的对象
- wcf: WcfAPI对象可用于发送消息
- message_util: 消息工具类,提供更高级的消息处理功能
Returns:
(是否已处理, 处理结果)
"""
raise NotImplementedError("子类必须实现此方法")