模块管理优化
This commit is contained in:
@@ -140,7 +140,9 @@ class PluginManagerPlugin(MessagePluginInterface):
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, "未知命令"
|
||||
except Exception as e:
|
||||
self.LOG.error(f"处理插件管理请求出错: {e}")
|
||||
import traceback
|
||||
error_trace = traceback.format_exc()
|
||||
self.LOG.error(f"处理插件管理请求出错: {e}\n{error_trace}")
|
||||
wcf.send_text(f"❌操作失败: {str(e)}",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"处理出错: {e}"
|
||||
@@ -235,7 +237,11 @@ class PluginManagerPlugin(MessagePluginInterface):
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {actual_plugin_name} 已经是启用状态"
|
||||
|
||||
if self.plugin_manager.start_plugin(actual_plugin_name):
|
||||
# 获取插件的模块名,这才是插件管理器需要的名称
|
||||
module_name = plugin.__class__.__module__.split('.')[-2]
|
||||
|
||||
# 使用模块名启用插件
|
||||
if self.plugin_manager.start_plugin(module_name):
|
||||
wcf.send_text(f"✅插件 {actual_plugin_name} 启用成功",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {actual_plugin_name} 启用成功"
|
||||
@@ -272,8 +278,11 @@ class PluginManagerPlugin(MessagePluginInterface):
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {actual_plugin_name} 已经是禁用状态"
|
||||
|
||||
# 使用插件管理器停止插件
|
||||
success = self.plugin_manager.stop_plugin(actual_plugin_name)
|
||||
# 获取插件的模块名,这才是插件管理器需要的名称
|
||||
module_name = plugin.__class__.__module__.split('.')[-2]
|
||||
|
||||
# 使用插件管理器停止插件 - 使用模块名而不是显示名称
|
||||
success = self.plugin_manager.stop_plugin(module_name)
|
||||
if success:
|
||||
wcf.send_text(f"✅插件 {actual_plugin_name} 禁用成功",
|
||||
(roomid if roomid else sender), sender)
|
||||
@@ -329,7 +338,7 @@ class PluginManagerPlugin(MessagePluginInterface):
|
||||
# 如果之前是启用状态,则重新启用
|
||||
if was_running:
|
||||
self.LOG.info(f"正在启用插件 {actual_plugin_name}")
|
||||
if not self.plugin_manager.start_plugin(actual_plugin_name):
|
||||
if not self.plugin_manager.start_plugin(module_name): # 使用模块名而不是显示名称
|
||||
wcf.send_text(f"⚠️插件 {actual_plugin_name} 重载成功,但启用失败",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {actual_plugin_name} 重载成功,但启用失败"
|
||||
@@ -382,33 +391,32 @@ class PluginManagerPlugin(MessagePluginInterface):
|
||||
|
||||
def _load_plugin(self, plugin_name: str, wcf, sender: str, roomid: str, silent: bool = False) -> Tuple[bool, str]:
|
||||
"""加载插件"""
|
||||
# 对于加载操作,我们不需要查找匹配的插件名称,因为插件可能尚未加载
|
||||
# 但我们可以检查是否有同名插件已加载
|
||||
|
||||
# 检查插件是否已加载
|
||||
existing_plugin = self.plugin_registry.get_plugin(plugin_name)
|
||||
if existing_plugin:
|
||||
# 对于加载操作,我们直接使用目录名作为模块名
|
||||
# 检查插件目录是否存在
|
||||
plugin_dir = os.path.join("plugins", plugin_name)
|
||||
if not os.path.exists(plugin_dir):
|
||||
if not silent:
|
||||
wcf.send_text(f"⚠️插件 {plugin_name} 已经加载",
|
||||
wcf.send_text(f"❌插件目录 {plugin_dir} 不存在",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {plugin_name} 已经加载"
|
||||
return False, f"插件目录 {plugin_dir} 不存在"
|
||||
|
||||
# 检查插件是否已加载 - 遍历所有插件查找模块名匹配的
|
||||
for existing_plugin in self.plugin_registry.get_all_plugins().values():
|
||||
existing_module_name = existing_plugin.__class__.__module__.split('.')[-2]
|
||||
if existing_module_name == plugin_name:
|
||||
if not silent:
|
||||
wcf.send_text(f"⚠️插件 {existing_plugin.name} (模块名: {plugin_name}) 已经加载",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {existing_plugin.name} 已经加载"
|
||||
|
||||
try:
|
||||
# 检查插件目录是否存在
|
||||
plugin_dir = os.path.join("plugins", plugin_name)
|
||||
if not os.path.exists(plugin_dir):
|
||||
if not silent:
|
||||
wcf.send_text(f"❌插件目录 {plugin_dir} 不存在",
|
||||
(roomid if roomid else sender), sender)
|
||||
return False, f"插件目录 {plugin_dir} 不存在"
|
||||
|
||||
# 使用插件管理器加载插件
|
||||
plugin = self.plugin_manager.load_plugin(plugin_name)
|
||||
if plugin:
|
||||
if not silent:
|
||||
wcf.send_text(f"✅插件 {plugin_name} 加载成功",
|
||||
wcf.send_text(f"✅插件 {plugin.name} 加载成功",
|
||||
(roomid if roomid else sender), sender)
|
||||
return True, f"插件 {plugin_name} 加载成功"
|
||||
return True, f"插件 {plugin.name} 加载成功"
|
||||
else:
|
||||
if not silent:
|
||||
wcf.send_text(f"❌插件 {plugin_name} 加载失败",
|
||||
|
||||
Reference in New Issue
Block a user