From 9c3c8ff2c4834e2001e98f763fed8937c5823cde Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 20 Nov 2023 23:45:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E9=A2=91=E9=81=93?= =?UTF-8?q?=E4=B8=BB=E5=8A=A8=E6=B6=88=E6=81=AF=E5=9B=9E=E5=A4=8D=20fix:?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/qqbot/global_object.py | 2 +- model/platform/qqchan.py | 60 ++++++++++++++++++++++++++++++------ model/platform/qqgroup.py | 11 +++---- 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/cores/qqbot/global_object.py b/cores/qqbot/global_object.py index 2cbdffea8..ace53fe2c 100644 --- a/cores/qqbot/global_object.py +++ b/cores/qqbot/global_object.py @@ -25,7 +25,7 @@ class GlobalObject: uniqueSession: bool cnt_total: int platform_qq: QQ - platform_qqchan: QQ + platform_qqchan: QQChan def __init__(self): self.nick = None # gocq 的昵称 diff --git a/model/platform/qqchan.py b/model/platform/qqchan.py index ec7a520c4..217598ae4 100644 --- a/model/platform/qqchan.py +++ b/model/platform/qqchan.py @@ -9,6 +9,7 @@ from cores.qqbot.personality import personalities from util import general_utils as gu from nakuru.entities.components import Plain, At, Image from botpy.types.message import Reference +from botpy import Client class NakuruGuildMember(): tiny_id: int # 发送者识别号 @@ -44,7 +45,7 @@ class QQChan(): def set_cnt(self, cnt): self.qqchan_cnt = cnt - def run_bot(self, botclient, appid, token): + def run_bot(self, botclient: Client, appid, token): intents = botpy.Intents(public_guild_messages=True, direct_message=True) self.client = botclient self.client.run(appid=appid, token=token) @@ -100,7 +101,12 @@ class QQChan(): ngm.raw_message = message return ngm - def send_qq_msg(self, message: NakuruGuildMessage, res): + def send_qq_msg(self, + message: NakuruGuildMessage, + res: list): + ''' + 回复频道消息 + ''' gu.log("回复QQ频道消息: "+str(res), level=gu.LEVEL_INFO, tag="QQ频道", max_len=500) self.qqchan_cnt += 1 plain_text = "" @@ -112,7 +118,9 @@ class QQChan(): plain_text = res # print(plain_text, image_path) - msg_ref = Reference(message_id=message.raw_message.id, ignore_get_message_error=False) + msg_ref = None + if message.raw_message is not None: + msg_ref = Reference(message_id=message.raw_message.id, ignore_get_message_error=False) if image_path is not None: msg_ref = None if image_path.startswith("http"): @@ -121,8 +129,15 @@ class QQChan(): image = PILImage.open(io.BytesIO(pic_res.content)) image_path = gu.save_temp_img(image) + + try: - reply_res = asyncio.run_coroutine_threadsafe(message.raw_message.reply(content=str(plain_text), message_reference = msg_ref, file_image=image_path), self.client.loop) + # reply_res = asyncio.run_coroutine_threadsafe(message.raw_message.reply(content=str(plain_text), message_reference = msg_ref, file_image=image_path), self.client.loop) + reply_res = asyncio.run_coroutine_threadsafe(self.client.api.post_message(channel_id=message.channel_id, + content=str(plain_text), + msg_id=message.message_id, + file_image=image_path, + message_reference=msg_ref), self.client.loop) reply_res.result() except BaseException as e: # 分割过长的消息 @@ -131,21 +146,46 @@ class QQChan(): split_res.append(plain_text[:len(plain_text)//2]) split_res.append(plain_text[len(plain_text)//2:]) for i in split_res: - reply_res = asyncio.run_coroutine_threadsafe(message.raw_message.reply(content=str(i), message_reference = msg_ref, file_image=image_path), self.client.loop) + reply_res = asyncio.run_coroutine_threadsafe(self.client.api.post_message(channel_id=message.channel_id, + content=str(i), + msg_id=message.message_id, + file_image=image_path, + message_reference=msg_ref), self.client.loop) reply_res.result() else: # 发送qq信息 try: # 防止被qq频道过滤消息 plain_text = plain_text.replace(".", " . ") - reply_res = asyncio.run_coroutine_threadsafe(message.raw_message.reply(content=str(plain_text), message_reference = msg_ref, file_image=image_path), self.client.loop) - # 发送信息 + reply_res = asyncio.run_coroutine_threadsafe(self.client.api.post_message(channel_id=message.channel_id, + content=str(plain_text), + msg_id=message.message_id, + file_image=image_path, + message_reference=msg_ref), self.client.loop).result() # 发送信息 except BaseException as e: print("QQ频道API错误: \n"+str(e)) try: - reply_res = asyncio.run_coroutine_threadsafe(message.raw_message.reply(content=str(str.join(" ", plain_text)), message_reference = msg_ref, file_image=image_path), self.client.loop) + # reply_res = asyncio.run_coroutine_threadsafe(message.raw_message.reply(content=str(str.join(" ", plain_text)), message_reference = msg_ref, file_image=image_path), self.client.loop) + reply_res = asyncio.run_coroutine_threadsafe(self.client.api.post_message(channel_id=message.channel_id, + content=str(str.join(" ", plain_text)), + msg_id=message.message_id, + file_image=image_path, + message_reference=msg_ref), self.client.loop).result() except BaseException as e: plain_text = re.sub(r'(https|http)?:\/\/(\w|\.|\/|\?|\=|\&|\%)*\b', '[被隐藏的链接]', str(e), flags=re.MULTILINE) plain_text = plain_text.replace(".", "·") - asyncio.run_coroutine_threadsafe(message.raw_message.reply(content=plain_text), self.client.loop).result() - # send(message, f"QQ频道API错误:{str(e)}\n下面是格式化后的回答:\n{f_res}") \ No newline at end of file + reply_res = asyncio.run_coroutine_threadsafe(self.client.api.post_message(channel_id=message.channel_id, + content=plain_text, + msg_id=message.message_id, + file_image=image_path, + message_reference=msg_ref), self.client.loop).result() + # send(message, f"QQ频道API错误:{str(e)}\n下面是格式化后的回答:\n{f_res}") + + def push_message(self, channel_id: int, message_chain: list): + ''' + 推送消息 + ''' + _n = NakuruGuildMessage() + _n.channel_id = channel_id + self.send_qq_msg(_n, message_chain) + \ No newline at end of file diff --git a/model/platform/qqgroup.py b/model/platform/qqgroup.py index 81410e8d8..bcbcacf66 100644 --- a/model/platform/qqgroup.py +++ b/model/platform/qqgroup.py @@ -15,16 +15,13 @@ class UnofficialQQBotSDK: self.client_secret = client_secret self.get_access_token() self.get_wss_endpoint() - # self.ws_connect() - # print(self.ws_recv()) asyncio.get_event_loop().run_until_complete(self.ws_client()) + + + def get_access_token(self) -> None: - # self.access_token = requests.post(self.GET_APP_ACCESS_TOKEN_URL, data={ - # "appid": self.appid, - # "clientSecret": self.client_secret - # }).json()['access_token'] res = requests.post(self.GET_APP_ACCESS_TOKEN_URL, json={ "appId": self.appid, "clientSecret": self.client_secret @@ -112,4 +109,4 @@ class UnofficialQQBotSDK: if __name__ == "__main__": - UnofficialQQBotSDK("102041113", "b9VebKp7B2g7KK7f") \ No newline at end of file + UnofficialQQBotSDK("", "") \ No newline at end of file