重构整个插件系统,尝试恢复可启动性,新增插件系统maibot-plugin-sdk依赖
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"license": "GPL-v3.0-or-later",
|
||||
"host_application": {
|
||||
"min_version": "0.10.3"
|
||||
"min_version": "1.0.0"
|
||||
},
|
||||
"homepage_url": "https://github.com/SengokuCola/BetterFrequency",
|
||||
"repository_url": "https://github.com/SengokuCola/BetterFrequency",
|
||||
|
||||
@@ -1,144 +1,87 @@
|
||||
from typing import List, Tuple, Type, Optional
|
||||
from src.plugin_system import BasePlugin, register_plugin, BaseCommand, ComponentInfo, ConfigField
|
||||
from src.plugin_system.apis import send_api, frequency_api
|
||||
"""发言频率控制插件 — 新 SDK 版本
|
||||
|
||||
通过 /chat 命令设置和查看聊天频率。
|
||||
"""
|
||||
|
||||
from maibot_sdk import MaiBotPlugin, Command
|
||||
|
||||
|
||||
class SetTalkFrequencyCommand(BaseCommand):
|
||||
"""设置当前聊天的talk_frequency值"""
|
||||
class BetterFrequencyPlugin(MaiBotPlugin):
|
||||
"""聊天频率控制插件"""
|
||||
|
||||
command_name = "set_talk_frequency"
|
||||
command_description = "设置当前聊天的talk_frequency值:/chat talk_frequency <数字> 或 /chat t <数字>"
|
||||
command_pattern = r"^/chat\s+(?:talk_frequency|t)\s+(?P<value>[+-]?\d*\.?\d+)$"
|
||||
@Command(
|
||||
"set_talk_frequency",
|
||||
description="设置当前聊天的talk_frequency值:/chat talk_frequency <数字> 或 /chat t <数字>",
|
||||
pattern=r"^/chat\s+(?:talk_frequency|t)\s+(?P<value>[+-]?\d*\.?\d+)$",
|
||||
)
|
||||
async def handle_set_talk_frequency(
|
||||
self, stream_id: str = "", matched_groups: dict | None = None, **kwargs
|
||||
):
|
||||
"""设置当前聊天的 talk_frequency"""
|
||||
if not matched_groups or "value" not in matched_groups:
|
||||
return False, "命令格式错误", False
|
||||
|
||||
value_str = matched_groups["value"]
|
||||
if not value_str:
|
||||
return False, "无法获取数值参数", False
|
||||
|
||||
async def execute(self) -> Tuple[bool, Optional[str], bool]:
|
||||
try:
|
||||
# 获取命令参数 - 使用命名捕获组
|
||||
if not self.matched_groups or "value" not in self.matched_groups:
|
||||
return False, "命令格式错误", False
|
||||
|
||||
value_str = self.matched_groups["value"]
|
||||
if not value_str:
|
||||
return False, "无法获取数值参数", False
|
||||
|
||||
value = float(value_str)
|
||||
|
||||
# 获取聊天流ID
|
||||
if not self.message.chat_stream or not hasattr(self.message.chat_stream, "stream_id"):
|
||||
return False, "无法获取聊天流信息", False
|
||||
|
||||
chat_id = self.message.chat_stream.stream_id
|
||||
|
||||
# 设置talk_frequency
|
||||
frequency_api.set_talk_frequency_adjust(chat_id, value)
|
||||
|
||||
final_value = frequency_api.get_current_talk_value(chat_id)
|
||||
adjust_value = frequency_api.get_talk_frequency_adjust(chat_id)
|
||||
base_value = final_value / adjust_value
|
||||
|
||||
# 发送反馈消息(不保存到数据库)
|
||||
await send_api.text_to_stream(
|
||||
f"已设置当前聊天的talk_frequency调整值为: {value}\n当前talk_value: {final_value:.2f}\n发言频率调整: {adjust_value:.2f}\n基础值: {base_value:.2f}",
|
||||
chat_id,
|
||||
storage_message=False,
|
||||
)
|
||||
|
||||
return True, None, False
|
||||
|
||||
except ValueError:
|
||||
error_msg = "数值格式错误,请输入有效的数字"
|
||||
await self.send_text(error_msg, storage_message=False)
|
||||
return False, error_msg, False
|
||||
except Exception as e:
|
||||
error_msg = f"设置talk_frequency失败: {str(e)}"
|
||||
await self.send_text(error_msg, storage_message=False)
|
||||
return False, error_msg, False
|
||||
await self.ctx.send.text("数值格式错误,请输入有效的数字", stream_id)
|
||||
return False, "数值格式错误", False
|
||||
|
||||
if not stream_id:
|
||||
return False, "无法获取聊天流信息", False
|
||||
|
||||
# 设置 talk_frequency
|
||||
await self.ctx.frequency.set_adjust(stream_id, value)
|
||||
|
||||
# 获取当前状态
|
||||
current = await self.ctx.frequency.get_current_talk_value(stream_id)
|
||||
current_val = current if isinstance(current, (int, float)) else 0
|
||||
adjust = await self.ctx.frequency.get_adjust(stream_id)
|
||||
adjust_val = adjust if isinstance(adjust, (int, float)) else 1
|
||||
base_val = current_val / adjust_val if adjust_val else 0
|
||||
|
||||
msg = (
|
||||
f"已设置当前聊天的talk_frequency调整值为: {value}\n"
|
||||
f"当前talk_value: {current_val:.2f}\n"
|
||||
f"发言频率调整: {adjust_val:.2f}\n"
|
||||
f"基础值: {base_val:.2f}"
|
||||
)
|
||||
await self.ctx.send.text(msg, stream_id)
|
||||
return True, None, False
|
||||
|
||||
@Command(
|
||||
"show_frequency",
|
||||
description="显示当前聊天的频率控制状态:/chat show 或 /chat s",
|
||||
pattern=r"^/chat\s+(?:show|s)$",
|
||||
)
|
||||
async def handle_show_frequency(self, stream_id: str = "", **kwargs):
|
||||
"""显示当前频率控制状态"""
|
||||
if not stream_id:
|
||||
return False, "无法获取聊天流信息", False
|
||||
|
||||
current = await self.ctx.frequency.get_current_talk_value(stream_id)
|
||||
current_val = current if isinstance(current, (int, float)) else 0
|
||||
adjust = await self.ctx.frequency.get_adjust(stream_id)
|
||||
adjust_val = adjust if isinstance(adjust, (int, float)) else 1
|
||||
base_val = current_val / adjust_val if adjust_val else 0
|
||||
|
||||
status_msg = (
|
||||
"当前聊天频率控制状态\n"
|
||||
"Talk Value (发言频率):\n\n"
|
||||
f" • 基础值: {base_val:.2f}\n"
|
||||
f" • 发言频率调整: {adjust_val:.2f}\n"
|
||||
f" • 当前值: {current_val:.2f}\n\n"
|
||||
"使用命令:\n"
|
||||
" • /chat talk_frequency <数字> 或 /chat t <数字> - 设置发言频率调整\n"
|
||||
" • /chat show 或 /chat s - 显示当前状态"
|
||||
)
|
||||
await self.ctx.send.text(status_msg, stream_id)
|
||||
return True, None, False
|
||||
|
||||
|
||||
class ShowFrequencyCommand(BaseCommand):
|
||||
"""显示当前聊天的频率控制状态"""
|
||||
|
||||
command_name = "show_frequency"
|
||||
command_description = "显示当前聊天的频率控制状态:/chat show 或 /chat s"
|
||||
command_pattern = r"^/chat\s+(?:show|s)$"
|
||||
|
||||
async def execute(self) -> Tuple[bool, Optional[str], bool]:
|
||||
try:
|
||||
# 获取聊天流ID
|
||||
if not self.message.chat_stream or not hasattr(self.message.chat_stream, "stream_id"):
|
||||
return False, "无法获取聊天流信息", False
|
||||
|
||||
chat_id = self.message.chat_stream.stream_id
|
||||
|
||||
# 获取当前频率控制状态
|
||||
current_talk_frequency = frequency_api.get_current_talk_value(chat_id)
|
||||
talk_frequency_adjust = frequency_api.get_talk_frequency_adjust(chat_id)
|
||||
base_value = current_talk_frequency / talk_frequency_adjust
|
||||
|
||||
# 构建显示消息
|
||||
status_msg = f"""当前聊天频率控制状态
|
||||
Talk Value (发言频率):
|
||||
|
||||
• 基础值: {base_value:.2f}
|
||||
• 发言频率调整: {talk_frequency_adjust:.2f}
|
||||
• 当前值: {current_talk_frequency:.2f}
|
||||
|
||||
使用命令:
|
||||
• /chat talk_frequency <数字> 或 /chat t <数字> - 设置发言频率调整
|
||||
• /chat show 或 /chat s - 显示当前状态"""
|
||||
|
||||
# 发送状态消息(不保存到数据库)
|
||||
await send_api.text_to_stream(status_msg, chat_id, storage_message=False)
|
||||
|
||||
return True, None, False
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"获取频率控制状态失败: {str(e)}"
|
||||
# 使用内置的send_text方法发送错误消息
|
||||
await self.send_text(error_msg, storage_message=False)
|
||||
return False, error_msg, False
|
||||
|
||||
|
||||
# ===== 插件注册 =====
|
||||
|
||||
|
||||
@register_plugin
|
||||
class BetterFrequencyPlugin(BasePlugin):
|
||||
"""BetterFrequency插件 - 控制聊天频率的插件"""
|
||||
|
||||
# 插件基本信息
|
||||
plugin_name: str = "better_frequency_plugin"
|
||||
enable_plugin: bool = True
|
||||
dependencies: List[str] = []
|
||||
python_dependencies: List[str] = []
|
||||
config_file_name: str = "config.toml"
|
||||
|
||||
# 配置节描述
|
||||
config_section_descriptions = {"plugin": "插件基本信息", "frequency": "频率控制配置", "features": "功能开关配置"}
|
||||
|
||||
# 配置Schema定义
|
||||
config_schema: dict = {
|
||||
"plugin": {
|
||||
"name": ConfigField(type=str, default="better_frequency_plugin", description="插件名称"),
|
||||
"version": ConfigField(type=str, default="1.0.2", description="插件版本"),
|
||||
"enabled": ConfigField(type=bool, default=True, description="是否启用插件"),
|
||||
},
|
||||
"frequency": {
|
||||
"default_talk_adjust": ConfigField(type=float, default=1.0, description="默认talk_frequency调整值"),
|
||||
"max_adjust_value": ConfigField(type=float, default=1.0, description="最大调整值"),
|
||||
"min_adjust_value": ConfigField(type=float, default=0.0, description="最小调整值"),
|
||||
},
|
||||
}
|
||||
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
components = []
|
||||
|
||||
# 根据配置决定是否注册命令组件
|
||||
if self.config.get("features", {}).get("enable_commands", True):
|
||||
components.extend(
|
||||
[
|
||||
(SetTalkFrequencyCommand.get_command_info(), SetTalkFrequencyCommand),
|
||||
(ShowFrequencyCommand.get_command_info(), ShowFrequencyCommand),
|
||||
]
|
||||
)
|
||||
|
||||
return components
|
||||
def create_plugin():
|
||||
return BetterFrequencyPlugin()
|
||||
|
||||
Reference in New Issue
Block a user