From 173f05a8ae93c6c2e460043de9cbc8fc5629ee6b Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sat, 25 Feb 2023 15:39:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=A4=9A=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=E6=94=AF=E6=8C=81=20(#178)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-template.py | 2 ++ pkg/qqbot/command.py | 17 ++++++++--------- pkg/qqbot/manager.py | 23 +++++++++++++++++------ pkg/qqbot/process.py | 13 +++++++++++-- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/config-template.py b/config-template.py index f0613aa6..4cd0acd3 100644 --- a/config-template.py +++ b/config-template.py @@ -38,6 +38,8 @@ openai_config = { } # [必需] 管理员QQ号,用于接收报错等通知及执行管理员级别指令 +# 支持多个管理员,可以使用list形式设置,例如: +# admin_qq = [12345678, 87654321] admin_qq = 0 # 情景预设(机器人人格) diff --git a/pkg/qqbot/command.py b/pkg/qqbot/command.py index d9c93a10..6aaceac6 100644 --- a/pkg/qqbot/command.py +++ b/pkg/qqbot/command.py @@ -158,7 +158,7 @@ def plugin_operation(cmd, params, is_admin): def process_command(session_name: str, text_message: str, mgr, config, - launcher_type: str, launcher_id: int, sender_id: int) -> list: + launcher_type: str, launcher_id: int, sender_id: int, is_admin: bool) -> list: reply = [] try: logging.info( @@ -280,9 +280,8 @@ def process_command(session_name: str, text_message: str, mgr, config, reply = [reply_str] elif cmd == 'plugin': - reply = plugin_operation(cmd, params, True - if (launcher_type == 'person' and launcher_id == config.admin_qq) - else False) + reply = plugin_operation(cmd, params, is_admin) + elif cmd == 'default': if len(params) == 0: # 输出目前所有情景预设 @@ -294,7 +293,7 @@ def process_command(session_name: str, text_message: str, mgr, config, reply_str += "\n当前默认情景预设:{}\n".format(dprompt.get_current()) reply_str += "请使用!default <情景预设>来设置默认情景预设" reply = [reply_str] - elif len(params) >0 and launcher_type == 'person' and launcher_id == config.admin_qq: + elif len(params) >0 and is_admin: # 设置默认情景 import pkg.openai.dprompt as dprompt try: @@ -304,12 +303,12 @@ def process_command(session_name: str, text_message: str, mgr, config, reply = ["[bot]err: 未找到情景预设:{}".format(params[0])] else: reply = ["[bot]err: 仅管理员可设置默认情景预设"] - elif cmd == 'reload' and launcher_type == 'person' and launcher_id == config.admin_qq: + elif cmd == 'reload' and is_admin: def reload_task(): pkg.utils.reloader.reload_all() threading.Thread(target=reload_task, daemon=True).start() - elif cmd == 'update' and launcher_type == 'person' and launcher_id == config.admin_qq: + elif cmd == 'update' and is_admin: def update_task(): try: if pkg.utils.updater.update_all(): @@ -324,10 +323,10 @@ def process_command(session_name: str, text_message: str, mgr, config, threading.Thread(target=update_task, daemon=True).start() reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."] - elif cmd == 'cfg' and launcher_type == 'person' and launcher_id == config.admin_qq: + elif cmd == 'cfg' and is_admin: reply = config_operation(cmd, params) else: - if cmd.startswith("~") and launcher_type == 'person' and launcher_id == config.admin_qq: + if cmd.startswith("~") and is_admin: config_item = cmd[1:] params = [config_item] + params reply = config_operation("cfg", params) diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 92380c94..46345398 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -303,14 +303,25 @@ class QQBotManager: # 通知系统管理员 def notify_admin(self, message: str): config = pkg.utils.context.get_config() - if hasattr(config, "admin_qq") and config.admin_qq != 0: + if hasattr(config, "admin_qq") and config.admin_qq != 0 and config.admin_qq != []: logging.info("通知管理员:{}".format(message)) - send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message)) - threading.Thread(target=asyncio.run, args=(send_task,)).start() + if type(config.admin_qq) == int: + send_task = self.bot.send_friend_message(config.admin_qq, "[bot]{}".format(message)) + threading.Thread(target=asyncio.run, args=(send_task,)).start() + else: + for adm in config.admin_qq: + send_task = self.bot.send_friend_message(adm, "[bot]{}".format(message)) + threading.Thread(target=asyncio.run, args=(send_task,)).start() + def notify_admin_message_chain(self, message): config = pkg.utils.context.get_config() - if hasattr(config, "admin_qq") and config.admin_qq != 0: + if hasattr(config, "admin_qq") and config.admin_qq != 0 and config.admin_qq != []: logging.info("通知管理员:{}".format(message)) - send_task = self.bot.send_friend_message(config.admin_qq, message) - threading.Thread(target=asyncio.run, args=(send_task,)).start() + if type(config.admin_qq) == int: + send_task = self.bot.send_friend_message(config.admin_qq, message) + threading.Thread(target=asyncio.run, args=(send_task,)).start() + else: + for adm in config.admin_qq: + send_task = self.bot.send_friend_message(adm, message) + threading.Thread(target=asyncio.run, args=(send_task,)).start() diff --git a/pkg/qqbot/process.py b/pkg/qqbot/process.py index 2b1aed5f..b87eaf3a 100644 --- a/pkg/qqbot/process.py +++ b/pkg/qqbot/process.py @@ -29,6 +29,15 @@ import pkg.qqbot.ignore as ignore processing = [] +def is_admin(qq: int) -> bool: + """兼容list和int类型的管理员判断""" + import config + if type(config.admin_qq) == list: + return qq in config.admin_qq + else: + return qq == config.admin_qq + + def process_message(launcher_type: str, launcher_id: int, text_message: str, message_chain: MessageChain, sender_id: int) -> MessageChain: global processing @@ -83,7 +92,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes 'command': text_message[1:].strip().split(' ')[0], 'params': text_message[1:].strip().split(' ')[1:], 'text_message': text_message, - 'is_admin': sender_id == config.admin_qq, + 'is_admin': is_admin(sender_id), } event = plugin_host.emit(plugin_models.PersonCommandSent if launcher_type == 'person' @@ -98,7 +107,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes if not event.is_prevented_default(): reply = pkg.qqbot.command.process_command(session_name, text_message, - mgr, config, launcher_type, launcher_id, sender_id) + mgr, config, launcher_type, launcher_id, sender_id, is_admin(sender_id)) else: # 消息 # 限速丢弃检查