feat: Enhance plugin configuration management and SDK integration

- Add support for configuration reload scopes in the plugin runtime.
- Implement validation for SDK plugins to ensure required lifecycle methods are overridden.
- Update the configuration update handling to include scope information.
- Introduce tests for expression auto-check task and NapCat adapter SDK integration.
- Refactor configuration management to support callbacks with variable arguments.
- Improve plugin loading and error handling for configuration updates.
- Ensure that plugins can manage their own configuration updates effectively.
This commit is contained in:
DrSmoothl
2026-03-23 20:06:12 +08:00
parent 9dea6b0e6f
commit d13767ee21
16 changed files with 907 additions and 71 deletions

View File

@@ -3,11 +3,11 @@
根据聊天上下文的情感,使用 LLM 选择并发送合适的表情包。
"""
import random
from maibot_sdk import MaiBotPlugin, Action
from maibot_sdk import Action, MaiBotPlugin
from maibot_sdk.types import ActivationType
import random
class EmojiPlugin(MaiBotPlugin):
"""表情包插件"""
@@ -95,10 +95,35 @@ class EmojiPlugin(MaiBotPlugin):
return True, f"成功发送表情包:[表情包:{chosen_emotion}]"
return False, "发送表情包失败"
async def on_load(self):
async def on_load(self) -> None:
"""处理插件加载。"""
# 从插件配置读取 emoji_chance 来覆盖默认概率
await self.ctx.config.get("emoji.emoji_chance")
async def on_unload(self) -> None:
"""处理插件卸载。"""
async def on_config_update(self, scope: str, config_data: dict[str, object], version: str) -> None:
"""处理配置热重载事件。
Args:
scope: 配置变更范围。
config_data: 最新配置数据。
version: 配置版本号。
"""
del config_data
del version
if scope == "self":
await self.ctx.config.get("emoji.emoji_chance")
def create_plugin() -> EmojiPlugin:
"""创建 Emoji 插件实例。
Returns:
EmojiPlugin: 新的 Emoji 插件实例。
"""
def create_plugin():
return EmojiPlugin()

View File

@@ -3,7 +3,7 @@
通过 /pm 命令管理插件和组件的生命周期。
"""
from maibot_sdk import MaiBotPlugin, Command
from maibot_sdk import Command, MaiBotPlugin
_VALID_COMPONENT_TYPES = ("action", "command", "event_handler")
@@ -44,6 +44,12 @@ HELP_COMPONENT = (
class PluginManagementPlugin(MaiBotPlugin):
"""插件和组件管理插件"""
async def on_load(self) -> None:
"""处理插件加载。"""
async def on_unload(self) -> None:
"""处理插件卸载。"""
@Command(
"management",
description="管理插件和组件的生命周期",
@@ -268,6 +274,25 @@ class PluginManagementPlugin(MaiBotPlugin):
return components
return []
async def on_config_update(self, scope: str, config_data: dict[str, object], version: str) -> None:
"""处理配置热重载事件。
Args:
scope: 配置变更范围。
config_data: 最新配置数据。
version: 配置版本号。
"""
del scope
del config_data
del version
def create_plugin() -> PluginManagementPlugin:
"""创建插件管理插件实例。
Returns:
PluginManagementPlugin: 新的插件管理插件实例。
"""
def create_plugin():
return PluginManagementPlugin()