模块管理优化
This commit is contained in:
@@ -159,37 +159,102 @@ class PluginManagerPlugin(MessagePluginInterface):
|
||||
message = "📋 插件列表:\n"
|
||||
for plugin in plugins:
|
||||
status = "✅ 已启用" if plugin.status == PluginStatus.RUNNING else "❌ 已禁用"
|
||||
message += f"{status}-{plugin.name}\n"
|
||||
# 获取插件模块名
|
||||
module_name = plugin.__class__.__module__.split('.')[-2]
|
||||
message += f"{status}-{plugin.name} [模块: {module_name}]\n"
|
||||
|
||||
wcf.send_text(message, (roomid if roomid else sender), sender)
|
||||
return True, "列出插件成功"
|
||||
|
||||
def _find_plugin_by_name(self, input_name: str) -> Optional[str]:
|
||||
"""
|
||||
根据用户输入的名称查找匹配的插件
|
||||
支持模糊匹配和不区分大小写
|
||||
|
||||
Args:
|
||||
input_name: 用户输入的插件名称
|
||||
|
||||
Returns:
|
||||
匹配到的插件名称,未找到返回None
|
||||
"""
|
||||
if not input_name:
|
||||
return None
|
||||
|
||||
# 获取所有插件
|
||||
all_plugins = self.plugin_registry.get_all_plugins()
|
||||
|
||||
# 精确匹配
|
||||
if input_name in all_plugins:
|
||||
return input_name
|
||||
|
||||
# 不区分大小写的精确匹配
|
||||
for plugin_name in all_plugins:
|
||||
if plugin_name.lower() == input_name.lower():
|
||||
return plugin_name
|
||||
|
||||
# 模块名匹配(例如输入music匹配音乐点播)
|
||||
for plugin_name, plugin in all_plugins.items():
|
||||
module_name = plugin.__class__.__module__.split('.')[-2] # 获取模块名
|
||||
if module_name.lower() == input_name.lower():
|
||||
return plugin_name
|
||||
|
||||
# 部分匹配(包含关系)
|
||||
matched_plugins = []
|
||||
for plugin_name in all_plugins:
|
||||
if input_name.lower() in plugin_name.lower() or plugin_name.lower() in input_name.lower():
|
||||
matched_plugins.append(plugin_name)
|
||||
|
||||
# 如果只有一个匹配项,返回它
|
||||
if len(matched_plugins) == 1:
|
||||
return matched_plugins[0]
|
||||
# 如果有多个匹配项,返回None(需要用户明确指定)
|
||||
elif len(matched_plugins) > 1:
|
||||
return None
|
||||
|
||||
return None
|
||||
|
||||
# 修改现有的插件操作方法,使用_find_plugin_by_name函数
|
||||
def _enable_plugin(self, plugin_name: str, wcf, sender: str, roomid: str) -> Tuple[bool, str]:
|
||||
"""启用插件"""
|
||||
plugin = self.plugin_registry.get_plugin(plugin_name)
|
||||
if not plugin:
|
||||
wcf.send_text(f"❌插件 {plugin_name} 不存在",
|
||||
# 查找匹配的插件名称
|
||||
actual_plugin_name = self._find_plugin_by_name(plugin_name)
|
||||
|
||||
if not actual_plugin_name:
|
||||
wcf.send_text(f"❌未找到插件 {plugin_name},请检查名称是否正确",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {plugin_name} 不存在"
|
||||
return True, f"未找到插件 {plugin_name}"
|
||||
|
||||
plugin = self.plugin_registry.get_plugin(actual_plugin_name)
|
||||
if not plugin:
|
||||
wcf.send_text(f"❌插件 {actual_plugin_name} 不存在",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {actual_plugin_name} 不存在"
|
||||
|
||||
if plugin.status == PluginStatus.RUNNING:
|
||||
wcf.send_text(f"⚠️插件 {plugin_name} 已经是启用状态",
|
||||
wcf.send_text(f"插件 {actual_plugin_name} 已经是启用状态",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {plugin_name} 已经是启用状态"
|
||||
return True, f"插件 {actual_plugin_name} 已经是启用状态"
|
||||
|
||||
# 使用插件管理器启动插件
|
||||
success = self.plugin_manager.start_plugin(plugin_name)
|
||||
if success:
|
||||
wcf.send_text(f"✅插件 {plugin_name} 启用成功",
|
||||
if self.plugin_manager.start_plugin(actual_plugin_name):
|
||||
wcf.send_text(f"✅插件 {actual_plugin_name} 启用成功",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {plugin_name} 启用成功"
|
||||
return True, f"插件 {actual_plugin_name} 启用成功"
|
||||
else:
|
||||
wcf.send_text(f"❌插件 {plugin_name} 启用失败",
|
||||
wcf.send_text(f"❌插件 {actual_plugin_name} 启用失败",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {plugin_name} 启用失败"
|
||||
|
||||
return True, f"插件 {actual_plugin_name} 启用失败"
|
||||
|
||||
# 同样修改其他插件操作方法
|
||||
def _disable_plugin(self, plugin_name: str, wcf, sender: str, roomid: str) -> Tuple[bool, str]:
|
||||
"""禁用插件"""
|
||||
# 查找匹配的插件名称
|
||||
actual_plugin_name = self._find_plugin_by_name(plugin_name)
|
||||
|
||||
if not actual_plugin_name:
|
||||
wcf.send_text(f"❌未找到插件 {plugin_name},请检查名称是否正确",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"未找到插件 {plugin_name}"
|
||||
|
||||
# 不允许禁用自身
|
||||
if plugin_name == self.name:
|
||||
wcf.send_text(f"⚠️不能禁用插件管理插件自身",
|
||||
@@ -306,11 +371,22 @@ class PluginManagerPlugin(MessagePluginInterface):
|
||||
|
||||
def _plugin_info(self, plugin_name: str, wcf, sender: str, roomid: str) -> Tuple[bool, str]:
|
||||
"""查看插件详情"""
|
||||
plugin = self.plugin_registry.get_plugin(plugin_name)
|
||||
if not plugin:
|
||||
wcf.send_text(f"❌插件 {plugin_name} 不存在",
|
||||
# 查找匹配的插件名称
|
||||
actual_plugin_name = self._find_plugin_by_name(plugin_name)
|
||||
|
||||
if not actual_plugin_name:
|
||||
wcf.send_text(f"❌未找到插件 {plugin_name},请检查名称是否正确",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {plugin_name} 不存在"
|
||||
return True, f"未找到插件 {plugin_name}"
|
||||
|
||||
plugin = self.plugin_registry.get_plugin(actual_plugin_name)
|
||||
if not plugin:
|
||||
wcf.send_text(f"❌插件 {actual_plugin_name} 不存在",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {actual_plugin_name} 不存在"
|
||||
|
||||
# 获取插件模块名
|
||||
module_name = plugin.__class__.__module__.split('.')[-2]
|
||||
|
||||
# 构建插件详情消息
|
||||
status_text = "✅ 已启用" if plugin.status == PluginStatus.RUNNING else "❌ 已禁用"
|
||||
@@ -319,6 +395,7 @@ class PluginManagerPlugin(MessagePluginInterface):
|
||||
📝 描述:{plugin.description}
|
||||
🔢 版本:{plugin.version}
|
||||
👤 作者:{plugin.author}
|
||||
📂 模块名:{module_name}
|
||||
⚙️ 状态:{status_text}
|
||||
🔑 命令:{', '.join(plugin.commands) if hasattr(plugin, 'commands') else '无'}
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user