mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
添加了多图片时合并转发功能和相关配置(默认开启)
This commit is contained in:
parent
65d0fd9ffc
commit
8f8658d604
@ -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"
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user