From e7b9d7cd546f03f57f88a44b0c2885b25bf4742c Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Fri, 14 Apr 2023 21:54:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20QQ=E5=B9=B3=E5=8F=B0=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=98=B5=E7=A7=B0=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=E3=80=82=E4=BD=BF=E7=94=A8=E6=A0=BC=E5=BC=8F:=20nick=20?= =?UTF-8?q?=E6=96=B0=E6=98=B5=E7=A7=B0=E3=80=82=E9=BB=98=E8=AE=A4=E6=98=AF?= =?UTF-8?q?ai?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/qqbot/core.py | 36 +++++++++++++++++------ model/command/command.py | 37 ++++++++++++++++++++++-- model/command/command_openai_official.py | 5 +++- model/command/command_rev_chatgpt.py | 5 +++- model/command/command_rev_edgegpt.py | 5 +++- 5 files changed, 73 insertions(+), 15 deletions(-) diff --git a/cores/qqbot/core.py b/cores/qqbot/core.py index 1042f0ace..54ab0719e 100644 --- a/cores/qqbot/core.py +++ b/cores/qqbot/core.py @@ -98,6 +98,7 @@ gocq_app = CQHTTP( http_port=5700, ) gocq_loop = None +nick_qq = "ai " bing_cache_loop = None @@ -265,6 +266,16 @@ def initBot(cfg, prov): print("\n[System] 如果有任何问题, 请在 https://github.com/Soulter/QQChannelChatGPT 上提交issue说明问题!或者添加QQ:905617992") print("[System] 请给 https://github.com/Soulter/QQChannelChatGPT 点个star!") + # 得到指令设置(cmd_config.json) + if os.path.exists("cmd_config.json"): + with open("cmd_config.json", 'r', encoding='utf-8') as f: + cmd_config = json.load(f) + # QQ机器人昵称 + if 'nick_qq' in cmd_config: + global nick_qq + nick_qq = cmd_config['nick_qq'] + + thread_inst = None # QQ频道 @@ -471,8 +482,7 @@ def oper_msg(message, group=False, msg_ref = None, platform = None): chatgpt_res = "" if chosen_provider == OPENAI_OFFICIAL: - hit, command_result = command_openai_official.check_command(qq_msg, session_id, user_name, role) - print(f"{hit} {command_result}") + hit, command_result = command_openai_official.check_command(qq_msg, session_id, user_name, role, platform=platform) # hit: 是否触发了指令. if not hit: # 请求ChatGPT获得结果 @@ -485,7 +495,7 @@ def oper_msg(message, group=False, msg_ref = None, platform = None): send_message(platform, message, f"OpenAI API错误。原因如下:\n{str(e)} \n前往官方频道反馈~", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot) elif chosen_provider == REV_CHATGPT: - hit, command_result = command_rev_chatgpt.check_command(qq_msg, role) + hit, command_result = command_rev_chatgpt.check_command(qq_msg, role, platform=platform) if not hit: try: chatgpt_res = str(rev_chatgpt.text_chat(qq_msg)) @@ -501,7 +511,7 @@ def oper_msg(message, group=False, msg_ref = None, platform = None): bing_cache_loop = gocq_loop elif platform == PLATFORM_QQCHAN: bing_cache_loop = qqchan_loop - hit, command_result = command_rev_edgegpt.check_command(qq_msg, bing_cache_loop, role) + hit, command_result = command_rev_edgegpt.check_command(qq_msg, bing_cache_loop, role, platform=platform) if not hit: try: while rev_edgegpt.is_busy(): @@ -535,6 +545,12 @@ def oper_msg(message, group=False, msg_ref = None, platform = None): with open("keyword.json", "r", encoding="utf-8") as f: keywords = json.load(f) + # QQ昵称 + if command == "nick": + with open("cmd_config.json", "r", encoding="utf-8") as f: + global nick_qq + nick_qq = json.load(f)["nick_qq"] + if command_result[0]: # 是否是画图指令 if len(command_result) == 3 and command_result[2] == 'draw': @@ -625,14 +641,15 @@ class gocqClient(): # 收到群聊消息 @gocq_app.receiver("GroupMessage") async def _(app: CQHTTP, source: GroupMessage): + global nick_qq if isinstance(source.message[0], Plain): - if source.message[0].text.startswith('ai '): - source.message[0].text = source.message[0].text[3:] + if source.message[0].text.startswith(nick_qq): + source.message[0].text = source.message[0].text[len(nick_qq):] new_sub_thread(oper_msg, (source, True, None, PLATFORM_GOCQ)) if isinstance(source.message[0], At): if source.message[0].qq == source.self_id: - if source.message[1].text.startswith('ai '): - source.message[1].text = source.message[0].text[3:] + if source.message[1].text.startswith(nick_qq): + source.message[1].text = source.message[0].text[len(nick_qq):] new_sub_thread(oper_msg, (source, True, None, PLATFORM_GOCQ)) else: return @@ -646,6 +663,7 @@ class gocqClient(): @gocq_app.receiver("GroupMemberIncrease") async def _(app: CQHTTP, source: GroupMemberIncrease): + global nick_qq await app.sendGroupMessage(source.group_id, [ - Plain(text=f"欢迎加入本群!\n欢迎给https://github.com/Soulter/QQChannelChatGPT项目一个Star😊~\n@我输入help查看帮助~\n") + Plain(text=f"欢迎加入本群!\n欢迎给https://github.com/Soulter/QQChannelChatGPT项目一个Star😊~\n@我输入help查看帮助~\n我叫{nick_qq}, 你也可以以【{nick_qq}+问题】的格式来提醒我并问我问题哦~\n") ]) \ No newline at end of file diff --git a/model/command/command.py b/model/command/command.py index 7dfa98afb..74b45c026 100644 --- a/model/command/command.py +++ b/model/command/command.py @@ -8,15 +8,45 @@ import requests from model.provider.provider import Provider import json +PLATFORM_QQCHAN = 'qqchan' +PLATFORM_GOCQ = 'gocq' + class Command: def __init__(self, provider: Provider): self.provider = Provider @abc.abstractmethod - def check_command(self, message): - if message.startswith("help") or message.startswith("帮助"): - return True, self.help() + def check_command(self, message, role, platform): + if self.command_start_with(message, "nick"): + return True, self.set_nick(message, platform) return False, None + + ''' + 存储机器人的昵称 + ''' + def set_nick(self, message: str, platform: str): + if platform == PLATFORM_GOCQ: + nick = message.split(" ")[1] + self.general_command_storer("nick_qq", nick) + return True, f"设置成功!现在你可以叫我{nick}来提问我啦~", "nick" + elif platform == PLATFORM_QQCHAN: + nick = message.split(" ")[2] + return False, "QQ频道平台不支持为机器人设置昵称。", "nick" + + """ + 存储指令结果到cmd_config.json + """ + def general_command_storer(self, key, value): + if not os.path.exists("cmd_config.json"): + config = {} + else: + with open("cmd_config.json", "r", encoding="utf-8") as f: + config = json.load(f) + config[key] = value + with open("cmd_config.json", "w", encoding="utf-8") as f: + json.dump(config, f, indent=4, ensure_ascii=False) + f.flush() + def general_commands(self): return { @@ -69,6 +99,7 @@ class Command: keyword = {l[1]: l[2]} with open("keyword.json", "w", encoding="utf-8") as f: json.dump(keyword, f, ensure_ascii=False, indent=4) + f.flush() return True, "设置成功: "+l[1]+" -> "+l[2], "keyword" except BaseException as e: return False, "设置失败: "+str(e), "keyword" diff --git a/model/command/command_openai_official.py b/model/command/command_openai_official.py index 7f24d577a..df1bd3698 100644 --- a/model/command/command_openai_official.py +++ b/model/command/command_openai_official.py @@ -6,7 +6,10 @@ class CommandOpenAIOfficial(Command): def __init__(self, provider: ProviderOpenAIOfficial): self.provider = provider - def check_command(self, message: str, session_id: str, user_name: str, role): + def check_command(self, message: str, session_id: str, user_name: str, role, platform: str): + hit, res = super().check_command(message, role, platform) + if hit: + return True, res if self.command_start_with(message, "reset", "重置"): return True, self.reset(session_id) elif self.command_start_with(message, "his", "历史"): diff --git a/model/command/command_rev_chatgpt.py b/model/command/command_rev_chatgpt.py index 557abc65c..ce545da1e 100644 --- a/model/command/command_rev_chatgpt.py +++ b/model/command/command_rev_chatgpt.py @@ -5,7 +5,10 @@ class CommandRevChatGPT(Command): def __init__(self, provider: ProviderRevChatGPT): self.provider = provider - def check_command(self, message: str, role): + def check_command(self, message: str, role, platform: str): + hit, res = super().check_command(message, role, platform) + if hit: + return True, res if self.command_start_with(message, "help", "帮助"): return True, self.help() elif self.command_start_with(message, "reset"): diff --git a/model/command/command_rev_edgegpt.py b/model/command/command_rev_edgegpt.py index 961323b96..5135ea68a 100644 --- a/model/command/command_rev_edgegpt.py +++ b/model/command/command_rev_edgegpt.py @@ -5,7 +5,10 @@ class CommandRevEdgeGPT(Command): def __init__(self, provider: ProviderRevEdgeGPT): self.provider = provider - def check_command(self, message: str, loop, role): + def check_command(self, message: str, loop, role, platform: str): + hit, res = super().check_command(message, role, platform) + if hit: + return True, res if self.command_start_with(message, "reset"): return True, self.reset(loop) elif self.command_start_with(message, "help"):