From c1d1e923cddb62ba6633f96609542eacb321c94e Mon Sep 17 00:00:00 2001 From: zhayujie Date: Fri, 24 Mar 2023 00:22:09 +0800 Subject: [PATCH 1/2] feat: add plugins config --- channel/wechat/wechat_channel.py | 4 ++-- config-template.json | 3 ++- plugins/plugin_manager.py | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index eff788d..a57957d 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -57,8 +57,8 @@ class WechatChannel(Channel): # handle_* 系列函数处理收到的消息后构造context,然后调用handle函数处理context # context是一个字典,包含了消息的所有信息,包括以下key - # type: 消息类型,包括TEXT、VOICE、IMAGE_CREATE - # content: 消息内容,如果是TEXT类型,content就是文本内容,如果是VOICE类型,content就是语音文件名,如果是IMAGE_CREATE类型,content就是图片生成命令 + # type 消息类型, 包括TEXT、VOICE、IMAGE_CREATE + # content 消息内容,如果是TEXT类型,content就是文本内容,如果是VOICE类型,content就是语音文件名,如果是IMAGE_CREATE类型,content就是图片生成命令 # session_id: 会话id # isgroup: 是否是群聊 # msg: 原始消息对象 diff --git a/config-template.json b/config-template.json index 9ad9f5d..4d35e44 100644 --- a/config-template.json +++ b/config-template.json @@ -9,5 +9,6 @@ "conversation_max_tokens": 1000, "speech_recognition": false, "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", - "expires_in_seconds": 3600 + "expires_in_seconds": 3600, + "plugins": ["role", "hello", "sdwebui", "godcmd", "dungeon", "banwords"] } diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index d946786..4b1a977 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -6,8 +6,8 @@ import os from common.singleton import singleton from common.sorted_dict import SortedDict from .event import * -from .plugin import * from common.log import logger +from config import conf @singleton @@ -59,7 +59,7 @@ class PluginManager: if os.path.isdir(plugin_path): # 判断插件是否包含同名.py文件 main_module_path = os.path.join(plugin_path, plugin_name+".py") - if os.path.isfile(main_module_path): + if os.path.isfile(main_module_path) and conf().get("plugins") and plugin_name in conf().get("plugins"): # 导入插件 import_path = "{}.{}.{}".format(plugins_dir, plugin_name, plugin_name) main_module = importlib.import_module(import_path) From 8c4a62b9c67a5811af57d6e7c4131e7e1c38c904 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Fri, 24 Mar 2023 00:59:26 +0800 Subject: [PATCH 2/2] fix: use try catch instead of config --- config-template.json | 3 +-- plugins/banwords/banwords.py | 2 +- plugins/plugin_manager.py | 8 ++++++-- plugins/role/role.py | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/config-template.json b/config-template.json index 4d35e44..9ad9f5d 100644 --- a/config-template.json +++ b/config-template.json @@ -9,6 +9,5 @@ "conversation_max_tokens": 1000, "speech_recognition": false, "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", - "expires_in_seconds": 3600, - "plugins": ["role", "hello", "sdwebui", "godcmd", "dungeon", "banwords"] + "expires_in_seconds": 3600 } diff --git a/plugins/banwords/banwords.py b/plugins/banwords/banwords.py index 2b4a711..9488b5a 100644 --- a/plugins/banwords/banwords.py +++ b/plugins/banwords/banwords.py @@ -38,7 +38,7 @@ class Banwords(Plugin): self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context logger.info("[Banwords] inited") except Exception as e: - logger.error("Banwords init failed: %s" % e) + logger.warn("Banwords init failed: %s" % e) diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index 4b1a977..8906e6e 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -59,10 +59,14 @@ class PluginManager: if os.path.isdir(plugin_path): # 判断插件是否包含同名.py文件 main_module_path = os.path.join(plugin_path, plugin_name+".py") - if os.path.isfile(main_module_path) and conf().get("plugins") and plugin_name in conf().get("plugins"): + if os.path.isfile(main_module_path): # 导入插件 import_path = "{}.{}.{}".format(plugins_dir, plugin_name, plugin_name) - main_module = importlib.import_module(import_path) + try: + main_module = importlib.import_module(import_path) + except Exception as e: + logger.warn("Failed to import plugin %s: %s" % (plugin_name, e)) + continue pconf = self.pconf new_plugins = [] modified = False diff --git a/plugins/role/role.py b/plugins/role/role.py index 91c09be..ea58819 100644 --- a/plugins/role/role.py +++ b/plugins/role/role.py @@ -116,7 +116,7 @@ class Role(Plugin): e_context.action = EventAction.CONTINUE def get_help_text(self): - help_text = "输入\"$角色 (角色名)\"或\"$role (角色名)\"为我设定角色吧,#reset 可以清除设定的角色。\n目前可用角色列表:\n" + help_text = "输入\"$角色 (角色名)\"或\"$role (角色名)\"为我设定角色吧,#reset 可以清除设定的角色。\n\n目前可用角色列表:\n" for role in self.roles: help_text += f"[{role}]: {self.roles[role]['remark']}\n" return help_text