mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-05 11:26:43 +08:00
增加了bison_use_pic_merge配置项的功能数量,现在可以选择是否全部合并转发(掩盖首条消息和图片消息分开转发时的延迟问题)
This commit is contained in:
parent
8ab7328611
commit
48210675f7
@ -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"
|
||||
|
||||
|
@ -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))
|
||||
|
||||
|
@ -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)
|
Loading…
x
Reference in New Issue
Block a user