async def AddQA(groupMsg: GroupMessage, group: Group) -> MessageChain: global app isFirstRun = temp_talk[groupMsg.sender.id]['isFirstRun'] Question = temp_talk[groupMsg.sender.id]['Q'] sendMsg = None session = Msg(groupMsg) if isFirstRun: if session.user_id in BlackUser: sendMsg = session.msg_chain.create( [At(session.user_id), Plain("你已经被拉入小黑屋")]) temp_talk.pop(session.user_id) await app.sendGroupMessage(group, sendMsg) return None if not GroupQA.get(group.id): GroupQA[group.id] = dict() t_QA: dict = GroupQA[group.id] if Question is not None: if Question in t_QA.keys(): reply = "问题已存在,当前回答为:" sendMsg = groupMsg.messageChain.create( [Plain(reply)]).plusWith(t_QA[Question].get_msg_list()) temp_talk.pop(session.user_id) else: sendMsg = session.msg_chain.create([Plain("问题已被录入,请问如何回答?")]) else: t_QA = GroupQA[group.id] answer = Msg(groupMsg) t_QA[Question] = answer sendMsg = session.msg_chain.create([Plain("录入成功")]) list_refresh(group.id) await saveQA() del session return sendMsg
async def FQA(message: GroupMessage, group: Group) -> bool: if not message.messageChain.has(Plain): return False msg = Msg(message) msg_chain = message.messageChain # 首先对消息进行问答解析 Question = msg.txt.strip() if Question == '列表': await app.sendGroupMessage(group, FQA_list(message, group)) del msg return True at = msg_chain.get(At)[0].target if msg_chain.has(At) else 0 tempQ = search(Question, group) if tempQ is not None: send_msg = tempQ.get_msg_graia(msg_chain) else: if at == BOTQQ: send_msg = msg_chain.create( [Plain("没有找到这个问题,请等待学长学姐来回答或回复“列表”查看已有问题")]) else: send_msg = None if send_msg is not None: await app.sendGroupMessage(group, send_msg) del msg return True del msg return False
def get_change(Q: str, group: GroupQA, GM: GroupMessage) -> bool: if group.id in GroupQA: t_QA: dict = GroupQA[group.id] if Q in t_QA: t_QA[Q] = Msg(GM) return True return False
async def group_message_handler(app: GraiaMiraiApplication, message: GroupMessage, group: Group): if await FQA(message, group): return msg = Msg(message) Question = message.messageChain.get( Plain)[0].text if message.messageChain.has(Plain) else None if Question is None: return hasSession = temp_talk.get(msg.user_id) if hasSession: if await session_manager(message, group): return if parser(message, "添加问题 "): # 创建添加问题的新会话 Question = Question.replace("添加问题", "").strip() if not hasSession: add_temp_talk(msg.user_id, 'Add', True, Question) await AddQA(message, group) del msg return if parser(message, "修改问题 "): # 创建修改问题的新会话 Question = Question.replace("修改问题", "").strip() if not hasSession: add_temp_talk(msg.user_id, 'Change', True, Question) await change(group=group, GM=message) del msg return if parser(message, "删除问题 "): # 删除问题 Question = Question.replace("删除问题", "").strip() isdeleteOK = "删除成功" if deleteQA(Question, group) else "不存在这个问题" await app.sendGroupMessage( group, message.messageChain.create([Plain(isdeleteOK)])) await saveQA() del msg return
async def group_message_handler(message: GroupMessage, group: Group): if group_is_in_list(message, group, shutdown_all_group): return if parser(message, "百度 ") \ or parser(message, "萌娘 "): return msg = Msg(message) question = message.messageChain.get( Plain)[0].text if message.messageChain.has(Plain) else None has_session = temp_talk.get(msg.user_id) if has_session is not None: await session_manager(message, group) return if await FQA(message, group): return if parser(message, '#'): await indexes(message, group) return if group.id in mast_manager_group: if not is_manager(message): return if parser(message, "添加问题 "): # 创建添加问题的新会话 question = question.replace("添加问题 ", "").strip() if has_session is None: add_temp_talk(msg.user_id, 'Add', True, question) sendMsg = await AddQA(message, group) if sendMsg is not None: await app.sendGroupMessage(group, sendMsg) del msg return if parser(message, "修改问题 "): # 创建修改问题的新会话 question = question.replace("修改问题", "").strip() question = question if not re.search("#", question) \ else quick_find_question_list[group.id][int(question.replace('#', ''))] if has_session is None: add_temp_talk(msg.user_id, 'Change', True, question) sendMsg = await change(group=group, GM=message) if sendMsg is not None: await app.sendGroupMessage(group, sendMsg) del msg return if parser(message, "删除问题 "): # 删除问题 question = question.replace("删除问题", "").strip() question = question if not re.search("#", question) \ else quick_find_question_list[group.id][int(question.replace('#', ''))] isdeleteOK: str = f"删除问题{question}成功" if deleteQA(question, group) else "不存在这个问题" await app.sendGroupMessage( group, message.messageChain.create([Plain(isdeleteOK)])) await saveQA() del msg gc.collect() return
async def ReadQA(): try: with open('QAindex.json', 'r') as f: tempDict = json.loads(f.read()) for i in tempDict.keys(): GroupQA[int(i)] = tempDict[i] for key in GroupQA[int(i)].keys(): t = tempDict[i][key] GroupQA[int(i)][key] = Msg() GroupQA[int(i)][key].set_dict_from_json(t) f.close() print("读取结束") except: with open('QAIndex.json', 'w+') as f: f.close()
async def AddQA(groupMsg: GroupMessage, group: Group): isFirstRun = temp_talk[groupMsg.sender.id]['isFirstRun'] Question = temp_talk[groupMsg.sender.id]['Q'] sendMsg=None if isFirstRun: session = Msg(groupMsg) if session.user_id in BlackUser: sendMsg = asSendable_creat(list=[ At(session.user_id), Plain("你已经被拉入小黑屋") ], MC=session.msgChain) await app.sendGroupMessage(group, sendMsg) return if not GroupQA.get(group.id): GroupQA[group.id] = dict() t_QA: dict = GroupQA[group.id] if Question is not None: if Question in t_QA.keys(): reply="问题已存在,当前回答为:" sendMsg = t_QA[Question].get_msg_graia(session.msgChain).plusWith([ Plain(reply) ]) temp_talk.pop(groupMsg.sender.id) else: sendMsg = asSendable_creat(list=[ Plain("问题已被录入,请问如何回答?") ], MC=session.msgChain) else: t_QA = GroupQA[group.id] answer = Msg(groupMsg) t_QA[Question] = answer sendMsg = asSendable_creat(list=[ Plain("录入成功") ], MC=answer.msgChain) await saveQA() if sendMsg is not None: await app.sendGroupMessage(group, sendMsg)
async def Compatible_old_index(): # 对旧有数据的转化 with open('QAindex.json', 'r') as f: tempDick = json.loads(f.read()) for i in tempDick.keys(): tempDick[i] = json.loads(tempDick[i]) GroupQA[int(i)] = tempDick[i].copy() for key in tempDick[i]: txt = tempDick[i][key] GroupQA[int(i)][key] = Msg() TransformDict = { 'user_id': None, 'at': None, 'msg_txt': txt, 'img_url': None } GroupQA[int(i)][key].set_dict(TransformDict) f.close() await saveQA()
async def ReadQA(): try: with open('QAindex.json', 'r') as f: tempDict = json.loads(f.read()) for i in tempDict.keys(): GroupQA[int(i)] = tempDict[i] for key in GroupQA[int(i)].keys(): t = tempDict[i][key] GroupQA[int(i)][key] = Msg() GroupQA[int(i)][key].set_dict_from_json(t) quick_find_question_list[int(i)] = sorted( GroupQA[int(i)].keys(), key=lambda i: len(i), reverse=False) f.close() print("读取结束") except: with open('QAIndex.json', 'w+') as f: f.close()