From 8f8658d6045c1b6e19a521112cab6ca3334f5167 Mon Sep 17 00:00:00 2001 From: Azide Date: Sat, 5 Mar 2022 23:05:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=A4=9A=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=97=B6=E5=90=88=E5=B9=B6=E8=BD=AC=E5=8F=91=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=92=8C=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE=EF=BC=88?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=BC=80=E5=90=AF=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 | 2 +- src/plugins/nonebot_bison/send.py | 48 ++++++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/plugins/nonebot_bison/plugin_config.py b/src/plugins/nonebot_bison/plugin_config.py index c16071b..4637280 100644 --- a/src/plugins/nonebot_bison/plugin_config.py +++ b/src/plugins/nonebot_bison/plugin_config.py @@ -12,7 +12,7 @@ class PlugConfig(BaseSettings): bison_filter_log: bool = False bison_to_me: bool = True bison_skip_browser_check: bool = False - bison_use_pic_merge: bool = True#多图片时启用图片合并转发 + bison_use_pic_merge: bool = True#多图片时启用图片合并转发(仅限群),当bison_use_queue为False时该配置不会生效, class Config: extra = "ignore" diff --git a/src/plugins/nonebot_bison/send.py b/src/plugins/nonebot_bison/send.py index 1518636..c1060e0 100644 --- a/src/plugins/nonebot_bison/send.py +++ b/src/plugins/nonebot_bison/send.py @@ -1,3 +1,4 @@ +from email import message import time from typing import Literal, Union @@ -7,9 +8,25 @@ from nonebot.log import logger from .plugin_config import plugin_config -QUEUE = [] +QUEUE = []#不开启图片合并转发时使用 LAST_SEND_TIME = time.time() +def generate_forward_msg( + msgs:list, self_id:str, nickname:str + ): + group_msg=[] + for msg in msgs: + sub_msg={ + "type":"node", + "data": { + "name": f"{nickname}", + "uin": f"{self_id}", + "content": f"{msg}" + } + } + group_msg.append(sub_msg) + + return group_msg async def _do_send( bot: "Bot", user: str, user_type: str, msg: Union[str, Message, MessageSegment] @@ -19,6 +36,26 @@ async def _do_send( elif user_type == "private": await bot.call_api("send_private_msg", user_id=user, message=msg) +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: + logger.error("向群{}发送消息序列首消息失败:{}".format(user,repr(e))) + else: + logger.info("成功向群{}发送消息序列中的首条消息".format(user)) + try: + 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: + logger.error("向群{}发送合并图片消息失败:{}".format(user,repr(e))) + else: + logger.info("成功向群{}发送合并图片转发消息".format(user)) async def do_send_msgs(): global LAST_SEND_TIME @@ -39,10 +76,15 @@ async def do_send_msgs(): 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)) + if plugin_config.bison_use_pic_merge and user_type == "group": + await _do_merge_send(bot, user, user_type, msgs) + else: + 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)