From 48210675f761d01afd1fe58d26bef9b86ba0474e Mon Sep 17 00:00:00 2001 From: Azide Date: Sun, 6 Mar 2022 01:19:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86bison=5Fuse=5Fpic=5F?= =?UTF-8?q?merge=E9=85=8D=E7=BD=AE=E9=A1=B9=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=95=B0=E9=87=8F=EF=BC=8C=E7=8E=B0=E5=9C=A8=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E6=98=AF=E5=90=A6=E5=85=A8=E9=83=A8=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E8=BD=AC=E5=8F=91=EF=BC=88=E6=8E=A9=E7=9B=96=E9=A6=96?= =?UTF-8?q?=E6=9D=A1=E6=B6=88=E6=81=AF=E5=92=8C=E5=9B=BE=E7=89=87=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=88=86=E5=BC=80=E8=BD=AC=E5=8F=91=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E5=BB=B6=E8=BF=9F=E9=97=AE=E9=A2=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/nonebot_bison/plugin_config.py | 4 +- src/plugins/nonebot_bison/send.py | 30 ++++++++------ src/plugins/nonebot_bison/send.py.bak | 48 ---------------------- 3 files changed, 19 insertions(+), 63 deletions(-) delete mode 100644 src/plugins/nonebot_bison/send.py.bak diff --git a/src/plugins/nonebot_bison/plugin_config.py b/src/plugins/nonebot_bison/plugin_config.py index 4a50175..35e2b36 100644 --- a/src/plugins/nonebot_bison/plugin_config.py +++ b/src/plugins/nonebot_bison/plugin_config.py @@ -12,8 +12,8 @@ class PlugConfig(BaseSettings): bison_filter_log: bool = False bison_to_me: bool = True bison_skip_browser_check: bool = False - bison_use_pic_merge: bool = False#多图片时启用图片合并转发(仅限群),当bison_use_queue为False时该配置不会生效, - + bison_use_pic_merge: int = 0#多图片时启用图片合并转发(仅限群),当bison_use_queue为False时该配置不会生效 + #0:不启用;1:首条消息单独发送,剩余照片合并转发;3:所有消息全部合并转发 class Config: extra = "ignore" diff --git a/src/plugins/nonebot_bison/send.py b/src/plugins/nonebot_bison/send.py index 7f196ac..2283adc 100644 --- a/src/plugins/nonebot_bison/send.py +++ b/src/plugins/nonebot_bison/send.py @@ -39,22 +39,26 @@ async def _do_send( async def _do_merge_send( bot: Bot, user, user_type: Literal["private", "group"], msgs: list ): - try: - await _do_send(bot, user, user_type, msgs.pop(0))#弹出第一条消息,剩下的消息合并 - except Exception as e_f:#first_msg_exception - logger.error("向群{}发送消息序列首消息失败:{}".format(user,repr(e_f))) - else: - logger.info("成功向群{}发送消息序列中的首条消息".format(user)) + if plugin_config.bison_use_pic_merge == 1: + try: + await _do_send(bot, user, user_type, msgs.pop(0))#弹出第一条消息,剩下的消息合并 + except Exception as e_f:#first_msg_exception + logger.error("向群{}发送消息序列首消息失败:{}".format(user,repr(e_f))) + else: + logger.info("成功向群{}发送消息序列中的首条消息".format(user)) try: if msgs: - group_bot_info = await bot.get_group_member_info(group_id=user,user_id=bot.self_id,no_cache=True)#调用api获取群内bot的相关参数 - forward_msg = generate_forward_msg(msgs = msgs, - self_id = group_bot_info["user_id"], - nickname = group_bot_info["card"] - )#生成合并转发内容 - await bot.send_group_forward_msg(group_id=user,messages=forward_msg) + if len(msgs) == 1:#只有一条消息序列就不合并转发 + await _do_send(bot, user, user_type, msgs.pop(0)) + else: + group_bot_info = await bot.get_group_member_info(group_id=user,user_id=bot.self_id,no_cache=True)#调用api获取群内bot的相关参数 + forward_msg = generate_forward_msg(msgs = msgs, + self_id = group_bot_info["user_id"], + nickname = group_bot_info["card"] + )#生成合并转发内容 + await bot.send_group_forward_msg(group_id=user,messages=forward_msg) except Exception as e_b:#behind_msg_exception - logger.warning("向群{}发送合并图片消息超时或者可能失败:{}".format(user,repr(e_b))) + logger.warning("向群{}发送合并图片消息超时或者可能失败:{}\n可能是因为图片太大或者太多".format(user,repr(e_b))) else: logger.info("成功向群{}发送合并图片转发消息".format(user)) diff --git a/src/plugins/nonebot_bison/send.py.bak b/src/plugins/nonebot_bison/send.py.bak deleted file mode 100644 index 1518636..0000000 --- a/src/plugins/nonebot_bison/send.py.bak +++ /dev/null @@ -1,48 +0,0 @@ -import time -from typing import Literal, Union - -from nonebot.adapters import Message, MessageSegment -from nonebot.adapters.onebot.v11.bot import Bot -from nonebot.log import logger - -from .plugin_config import plugin_config - -QUEUE = [] -LAST_SEND_TIME = time.time() - - -async def _do_send( - bot: "Bot", user: str, user_type: str, msg: Union[str, Message, MessageSegment] -): - if user_type == "group": - await bot.call_api("send_group_msg", group_id=user, message=msg) - elif user_type == "private": - await bot.call_api("send_private_msg", user_id=user, message=msg) - - -async def do_send_msgs(): - global LAST_SEND_TIME - if time.time() - LAST_SEND_TIME < 1.5: - return - if QUEUE: - bot, user, user_type, msg, retry_time = QUEUE.pop(0) - try: - await _do_send(bot, user, user_type, msg) - except Exception as e: - if retry_time > 0: - QUEUE.insert(0, (bot, user, user_type, msg, retry_time - 1)) - else: - msg_str = str(msg) - if len(msg_str) > 50: - msg_str = msg_str[:50] + "..." - logger.warning(f"send msg err {e} {msg_str}") - LAST_SEND_TIME = time.time() - - -async def send_msgs(bot: Bot, user, user_type: Literal["private", "group"], msgs: list): - if plugin_config.bison_use_queue: - for msg in msgs: - QUEUE.append((bot, user, user_type, msg, 2)) - else: - for msg in msgs: - await _do_send(bot, user, user_type, msg)