From 651469dad197fb2cb465efecb37d6a119f5ff68f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E7=BE=85=E7=8B=BC?= Date: Wed, 15 Jan 2025 01:00:58 +0800 Subject: [PATCH] update qqbot --- bots/qqbot/message.py | 156 ++++++++++++++++++++++++++---------------- 1 file changed, 97 insertions(+), 59 deletions(-) diff --git a/bots/qqbot/message.py b/bots/qqbot/message.py index a4ba17778c..79bdf81fb0 100644 --- a/bots/qqbot/message.py +++ b/bots/qqbot/message.py @@ -4,6 +4,7 @@ import filetype from botpy.message import C2CMessage, DirectMessage, GroupMessage, Message +from botpy.errors import ServerError from botpy.types.message import Reference from bots.qqbot.info import * @@ -24,6 +25,7 @@ from core.database import BotDBUtil from core.logger import Logger from core.utils.http import download, url_pattern +from core.utils.image import msgchain2image enable_analytics = Config("enable_analytics", False) enable_send_url = Config("qq_bot_enable_send_url", False, table_name="bot_qqbot") @@ -107,25 +109,34 @@ async def send_message( if images: image_1 = images[0] images.pop(0) - send_img = await image_1.get() if image_1 else None - msg_quote = ( - Reference( - message_id=self.session.message.id, - ignore_get_message_error=False, + send_img = await image_1.get() if image_1 else None + msg_quote = ( + Reference( + message_id=self.session.message.id, + ignore_get_message_error=False, + ) + if quote and not send_img + else None ) - if quote and not send_img - else None - ) - if not msg_quote and quote: - msg = f"<@{self.session.message.author.id}> \n" + msg - send = await self.session.message.reply( - content=msg, file_image=send_img, message_reference=msg_quote - ) - Logger.info(f"[Bot] -> [{self.target.target_id}]: {msg}") - if image_1: - Logger.info( - f"[Bot] -> [{self.target.target_id}]: Image: {str(image_1.__dict__)}" + if not msg_quote and quote: + msg = f"<@{self.session.message.author.id}> \n" + msg + try: + send = await self.session.message.reply( + content=msg, file_image=send_img, message_reference=msg_quote ) + Logger.info(f"[Bot] -> [{self.target.target_id}]: {msg}") + if image_1: + Logger.info( + f"[Bot] -> [{self.target.target_id}]: Image: {str(image_1.__dict__)}" + ) + except ServerError: + img_chain = filtered_msg + img_chain.insert(0, I18NContext("error.message.limited.msg2img")) + img_chain.append(image_1) if image_1 else None + imgs = await msgchain2image(img_chain, self) + if imgs: + imgs = [Image(img) for img in imgs] + images = imgs + images if images: for img in images: send_img = await img.get() @@ -140,24 +151,33 @@ async def send_message( if images: image_1 = images[0] images.pop(0) - send_img = await image_1.get() if image_1 else None - msg_quote = ( - Reference( - message_id=self.session.message.id, - ignore_get_message_error=False, + send_img = await image_1.get() if image_1 else None + msg_quote = ( + Reference( + message_id=self.session.message.id, + ignore_get_message_error=False, + ) + if quote and not send_img + else None ) - if quote and not send_img - else None - ) - send = await self.session.message.reply( - content=msg, file_image=send_img, message_reference=msg_quote - ) - sends.append(send) - Logger.info(f"[Bot] -> [{self.target.target_id}]: {msg}") - if image_1: - Logger.info( - f"[Bot] -> [{self.target.target_id}]: Image: {str(image_1.__dict__)}" + try: + send = await self.session.message.reply( + content=msg, file_image=send_img, message_reference=msg_quote ) + sends.append(send) + Logger.info(f"[Bot] -> [{self.target.target_id}]: {msg}") + if image_1: + Logger.info( + f"[Bot] -> [{self.target.target_id}]: Image: {str(image_1.__dict__)}" + ) + except ServerError: + img_chain = filtered_msg + img_chain.insert(0, I18NContext("error.message.limited.msg2img")) + img_chain.append(image_1) if image_1 else None + imgs = await msgchain2image(img_chain, self) + if imgs: + imgs = [Image(img) for img in imgs] + images = imgs + images if images: for img in images: send_img = await img.get() @@ -181,20 +201,29 @@ async def send_message( ) if msg and self.session.message.id: msg = "\n" + msg - send = await self.session.message.reply( - content=msg, - msg_type=7 if send_img else 0, - media=send_img, - msg_seq=seq, - ) - Logger.info(f"[Bot] -> [{self.target.target_id}]: {msg.strip()}") - if image_1: - Logger.info( - f"[Bot] -> [{self.target.target_id}]: Image: {str(image_1.__dict__)}" + try: + send = await self.session.message.reply( + content=msg, + msg_type=7 if send_img else 0, + media=send_img, + msg_seq=seq, ) - if send: - sends.append(send) - seq += 1 + Logger.info(f"[Bot] -> [{self.target.target_id}]: {msg.strip()}") + if image_1: + Logger.info( + f"[Bot] -> [{self.target.target_id}]: Image: {str(image_1.__dict__)}" + ) + if send: + sends.append(send) + seq += 1 + except ServerError: + img_chain = filtered_msg + img_chain.insert(0, I18NContext("error.message.limited.msg2img")) + img_chain.append(image_1) if image_1 else None + imgs = await msgchain2image(img_chain, self) + if imgs: + imgs = [Image(img) for img in imgs] + images = imgs + images if images: for img in images: send_img = await self.session.message._api.post_group_file( @@ -224,20 +253,29 @@ async def send_message( file_type=1, file_data=await image_1.get_base64(), ) - send = await self.session.message.reply( - content=msg, - msg_type=7 if send_img else 0, - media=send_img, - msg_seq=seq, - ) - Logger.info(f"[Bot] -> [{self.target.target_id}]: {msg.strip()}") - if image_1: - Logger.info( - f"[Bot] -> [{self.target.target_id}]: Image: {str(image_1.__dict__)}" + try: + send = await self.session.message.reply( + content=msg, + msg_type=7 if send_img else 0, + media=send_img, + msg_seq=seq, ) - if send: - sends.append(send) - seq += 1 + Logger.info(f"[Bot] -> [{self.target.target_id}]: {msg.strip()}") + if image_1: + Logger.info( + f"[Bot] -> [{self.target.target_id}]: Image: {str(image_1.__dict__)}" + ) + if send: + sends.append(send) + seq += 1 + except ServerError: + img_chain = filtered_msg + img_chain.insert(0, I18NContext("error.message.limited.msg2img")) + img_chain.append(image_1) if image_1 else None + imgs = await msgchain2image(img_chain, self) + if imgs: + imgs = [Image(img) for img in imgs] + images = imgs + images if images: for img in images: send_img = await self.session.message._api.post_c2c_file(