🐛 修复msg_seq的问题

This commit is contained in:
‘KimigaiiWuyi’ 2023-11-23 11:09:02 +08:00
parent 72387440e4
commit 49609a2fea

View File

@ -6,7 +6,7 @@ import base64
import asyncio import asyncio
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Union, Optional from typing import Any, Dict, List, Union, Optional
from collections import OrderedDict
import websockets.client import websockets.client
from nonebot.log import logger from nonebot.log import logger
from nonebot.adapters import Bot from nonebot.adapters import Bot
@ -17,6 +17,7 @@ from websockets.exceptions import ConnectionClosedError
from .utils import download_image from .utils import download_image
from .models import MessageSend, MessageReceive from .models import MessageSend, MessageReceive
msg_id_seq = OrderedDict()
bots: Dict[str, str] = {} bots: Dict[str, str] = {}
driver = get_driver() driver = get_driver()
@ -825,26 +826,24 @@ async def group_send(
assert isinstance(bot, qqbot) assert isinstance(bot, qqbot)
assert isinstance(target_id, str) assert isinstance(target_id, str)
async def _send( async def _send(text: Optional[str], img: Optional[str], msg_seq: int):
content: Optional[str], image: Optional[str], msg_seq: int
):
message = Message() message = Message()
if image: if img:
if image.startswith('link://'): if img.startswith('link://'):
_image = image.replace('link://', '') _img = img.replace('link://', '')
else: else:
logger.warning('[gscore] qqgroup暂不支持发送本地图信息, 请转为URL发送') logger.warning('[gscore] qqgroup暂不支持发送本地图信息, 请转为URL发送')
return return
else: else:
_image = '' _img = ''
if content and image: if text and img:
data = f'{content}\n{_image}' data = f'{text}\n{_img}'
message.append(MessageSegment.markdown(data)) message.append(MessageSegment.markdown(data))
elif content: elif text:
message.append(MessageSegment.text(content)) message.append(MessageSegment.text(text))
elif _image: elif _img:
message.append(MessageSegment.image(_image)) message.append(MessageSegment.image(_img))
if markdown: if markdown:
_markdown = markdown.replace('link://', '') _markdown = markdown.replace('link://', '')
@ -858,6 +857,7 @@ async def group_send(
msg_id=msg_id, msg_id=msg_id,
event_id=msg_id, event_id=msg_id,
message=message, message=message,
msg_seq=msg_seq,
) )
else: else:
await bot.send_to_c2c( await bot.send_to_c2c(
@ -865,21 +865,25 @@ async def group_send(
msg_id=msg_id, msg_id=msg_id,
event_id=msg_id, event_id=msg_id,
message=message, message=message,
msg_seq=msg_seq,
) )
msg_seq = 1 msg_id_seq[msg_id] = 1
if len(msg_id_seq) >= 30:
oldest_key = next(iter(msg_id_seq))
del msg_id_seq[oldest_key]
if node: if node:
for _msg in node: for _msg in node:
if _msg['type'] == 'image': if _msg['type'] == 'image':
image = _msg['data'] await _send(None, _msg['data'], msg_id_seq[msg_id])
content = None msg_id_seq[msg_id] += 1
elif _msg['type'] == 'text': elif _msg['type'] == 'text':
image = None await _send(_msg['data'], None, msg_id_seq[msg_id])
content = _msg['data'] msg_id_seq[msg_id] += 1
await _send(content, image, msg_seq)
msg_seq += 1
else: else:
await _send(content, image, msg_seq) await _send(content, image, msg_id_seq[msg_id])
async def ntchat_send( async def ntchat_send(