feat: QQ平台支持自定义昵称指令。使用格式: nick 新昵称。默认是ai
This commit is contained in:
+27
-9
@@ -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")
|
||||
])
|
||||
@@ -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"
|
||||
|
||||
@@ -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", "历史"):
|
||||
|
||||
@@ -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"):
|
||||
|
||||
@@ -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"):
|
||||
|
||||
Reference in New Issue
Block a user