From 6901c5ba56050edff41ba1b1667d5fd5f7543b05 Mon Sep 17 00:00:00 2001 From: JS00000 Date: Tue, 4 Apr 2023 03:14:08 +0800 Subject: [PATCH] Plugins: register function add namecn --- plugins/banwords/banwords.py | 2 +- plugins/bdunit/bdunit.py | 2 +- plugins/dungeon/dungeon.py | 2 +- plugins/finish/finish.py | 2 +- plugins/godcmd/godcmd.py | 39 +++++++++++++++--------------------- plugins/hello/hello.py | 2 +- plugins/plugin_manager.py | 12 ++++++----- plugins/role/role.py | 2 +- 8 files changed, 29 insertions(+), 34 deletions(-) diff --git a/plugins/banwords/banwords.py b/plugins/banwords/banwords.py index 77042c1..ede2779 100644 --- a/plugins/banwords/banwords.py +++ b/plugins/banwords/banwords.py @@ -10,7 +10,7 @@ from common.log import logger from .WordsSearch import WordsSearch -@plugins.register(name="Banwords", desc="判断消息中是否有敏感词、决定是否回复。", version="1.0", author="lanvent", desire_priority= 100) +@plugins.register(name="Banwords", desire_priority=100, hidden=True, desc="判断消息中是否有敏感词、决定是否回复。", version="1.0", author="lanvent") class Banwords(Plugin): def __init__(self): super().__init__() diff --git a/plugins/bdunit/bdunit.py b/plugins/bdunit/bdunit.py index cb8ff05..d8d7386 100644 --- a/plugins/bdunit/bdunit.py +++ b/plugins/bdunit/bdunit.py @@ -16,7 +16,7 @@ from uuid import getnode as get_mac """ -@plugins.register(name="BDunit", desc="Baidu unit bot system", version="0.1", author="jackson", desire_priority=0) +@plugins.register(name="BDunit", desire_priority=0, desc="Baidu unit bot system", version="0.1", author="jackson") class BDunit(Plugin): def __init__(self): super().__init__() diff --git a/plugins/dungeon/dungeon.py b/plugins/dungeon/dungeon.py index 1e129dc..d521988 100644 --- a/plugins/dungeon/dungeon.py +++ b/plugins/dungeon/dungeon.py @@ -35,7 +35,7 @@ class StoryTeller(): return prompt -@plugins.register(name="文字冒险", desc="A plugin to play dungeon game", version="1.0", author="lanvent", desire_priority= 0) +@plugins.register(name="Dungeon", desire_priority=0, namecn="文字冒险", desc="A plugin to play dungeon game", version="1.0", author="lanvent") class Dungeon(Plugin): def __init__(self): super().__init__() diff --git a/plugins/finish/finish.py b/plugins/finish/finish.py index 88dc962..4ce86b8 100644 --- a/plugins/finish/finish.py +++ b/plugins/finish/finish.py @@ -7,7 +7,7 @@ from plugins import * from common.log import logger -@plugins.register(name="Finish", desc="A plugin that check unknow command", version="1.0", author="js00000", desire_priority= -999) +@plugins.register(name="Finish", desire_priority=-999, hidden=True, desc="A plugin that check unknow command", version="1.0", author="js00000") class Finish(Plugin): def __init__(self): super().__init__() diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index c60f937..951c9eb 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -103,7 +103,7 @@ ADMIN_COMMANDS = { def get_help_text(isadmin, isgroup): help_text = "通用指令:\n" for cmd, info in COMMANDS.items(): - if cmd=="auth": # 隐藏认证指令 + if cmd=="auth": #不提示认证指令 continue alias=["#"+a for a in info['alias']] @@ -116,10 +116,11 @@ def get_help_text(isadmin, isgroup): # 插件指令 plugins = PluginManager().list_plugins() for plugin in plugins: - if plugin != 'GODCMD' and plugin != 'BANWORDS' and plugin != 'FINISH' and plugins[plugin].enabled: - print(plugin) - help_text += "\n%s:\n"%plugin - help_text += "#帮助 %s: 关于%s的详细帮助\n"%(plugin,plugin) + if plugins[plugin].enabled and not plugins[plugin].hidden: + namecn = plugins[plugin].namecn + print(namecn) + help_text += "\n%s:\n"%namecn + help_text += "#帮助 %s: 关于%s的详细帮助\n"%(namecn,namecn) help_text += PluginManager().instances[plugin].get_help_text(verbose=False) if ADMIN_COMMANDS and isadmin: @@ -130,7 +131,7 @@ def get_help_text(isadmin, isgroup): help_text += f": {info['desc']}\n" return help_text -@plugins.register(name="Godcmd", desc="为你的机器人添加指令集,有用户和管理员两种角色,加载顺序请放在首位,初次运行后插件目录会生成配置文件, 填充管理员密码后即可认证", version="1.0", author="lanvent", desire_priority= 999) +@plugins.register(name="Godcmd", desire_priority=999, hidden=True, desc="为你的机器人添加指令集,有用户和管理员两种角色,加载顺序请放在首位,初次运行后插件目录会生成配置文件, 填充管理员密码后即可认证", version="1.0", author="lanvent") class Godcmd(Plugin): def __init__(self): @@ -188,12 +189,16 @@ class Godcmd(Plugin): if len(args) == 0: ok, result = True, get_help_text(isadmin, isgroup) elif len(args) == 1: + # This can replace the helpp command plugins = PluginManager().list_plugins() - name = args[0].upper() - if name in plugins and name != 'GODCMD' and name != 'BANWORDS' and plugins[name].enabled: - ok, result = True, PluginManager().instances[name].get_help_text(verbose=True) - else: - ok, result = False, "unknown args" + query_name = args[0].upper() + # search name and namecn + for name, plugincls in plugins.items(): + if query_name == name or query_name == plugincls.namecn: + ok, result = True, PluginManager().instances[name].get_help_text(verbose=True) + break + if not ok: + result = "unknown args" elif cmd == "set_openai_api_key": if len(args) == 1: user_data = conf().get_user_data(user) @@ -208,18 +213,6 @@ class Godcmd(Plugin): except Exception as e: ok, result = False, "你没有设置私有api_key" ok, result = True, "你的OpenAI私有api_key已清除" - # elif cmd == "helpp": - # if len(args) != 1: - # ok, result = False, "请提供插件名" - # else: - # plugins = PluginManager().list_plugins() - # name = args[0].upper() - # if name in plugins and plugins[name].enabled: - # ok, result = True, PluginManager().instances[name].get_help_text(isgroup=isgroup, isadmin=isadmin) - # else: - # ok, result= False, "插件不存在或未启用" - # elif cmd == "id": - # ok, result = True, f"用户id=\n{user}" elif cmd == "reset": if bottype in (const.CHATGPT, const.OPEN_AI): bot.sessions.clear_session(session_id) diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index dc91f16..9413183 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -8,7 +8,7 @@ from plugins import * from common.log import logger -@plugins.register(name="Hello", desc="A simple plugin that says hello", version="0.1", author="lanvent", desire_priority= -1) +@plugins.register(name="Hello", desire_priority=-1, hidden=True, desc="A simple plugin that says hello", version="0.1", author="lanvent") class Hello(Plugin): def __init__(self): super().__init__() diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index a425c6e..c7d7ddf 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -18,16 +18,18 @@ class PluginManager: self.instances = {} self.pconf = {} - def register(self, name: str, desc: str, version: str, author: str, desire_priority: int = 0): + def register(self, name: str, desire_priority: int = 0, **kwargs): def wrapper(plugincls): plugincls.name = name - plugincls.desc = desc - plugincls.version = version - plugincls.author = author plugincls.priority = desire_priority + plugincls.desc = kwargs.get('desc') + plugincls.author = kwargs.get('author') + plugincls.version = kwargs.get('version') if kwargs.get('version') != None else "1.0" + plugincls.namecn = kwargs.get('namecn') if kwargs.get('namecn') != None else name + plugincls.hidden = kwargs.get('hidden') if kwargs.get('hidden') != None else False plugincls.enabled = True self.plugins[name.upper()] = plugincls - logger.info("Plugin %s_v%s registered" % (name, version)) + logger.info("Plugin %s_v%s registered" % (name, plugincls.version)) return plugincls return wrapper diff --git a/plugins/role/role.py b/plugins/role/role.py index 929cc53..0bc828e 100644 --- a/plugins/role/role.py +++ b/plugins/role/role.py @@ -29,7 +29,7 @@ class RolePlay(): prompt = self.wrapper % user_action return prompt -@plugins.register(name="角色扮演", desc="为你的Bot设置预设角色", version="1.0", author="lanvent", desire_priority= 0) +@plugins.register(name="Role", desire_priority=0, namecn="角色扮演", desc="为你的Bot设置预设角色", version="1.0", author="lanvent") class Role(Plugin): def __init__(self): super().__init__()