Skip to content

Commit

Permalink
✨ 在发送搜图结果之前上锁,并增加最多 1 秒的时间间隔来避免刷屏,同时保证搜图结果的顺序
Browse files Browse the repository at this point in the history
  • Loading branch information
NekoAria committed May 18, 2022
1 parent ed73f2e commit 8d7a61a
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions YetAnotherPicSearch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import re
from typing import Any, Dict, List, Optional, Tuple, Union
from collections import defaultdict
from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union

import arrow
from nonebot.adapters.onebot.v11 import (
Expand All @@ -25,6 +26,10 @@
from .result import Result
from .saucenao import saucenao_search

sending_lock: DefaultDict[Tuple[Union[int, str], str], asyncio.Lock] = defaultdict(
asyncio.Lock
)


def has_images(event: MessageEvent) -> bool:
message = event.reply.message if event.reply else event.message
Expand Down Expand Up @@ -120,30 +125,51 @@ async def send_result_message(
) -> None:
if isinstance(event, PrivateMessageEvent):
for msg in msg_list:
await bot.send_private_msg(user_id=event.user_id, message=msg)
start_time = arrow.now()
async with sending_lock[(event.user_id, "private")]:
await bot.send_private_msg(user_id=event.user_id, message=msg)
await asyncio.sleep(
max(1 - (arrow.now() - start_time).total_seconds(), 0)
)
elif isinstance(event, GroupMessageEvent):
flag = config.group_forward_search_result and len(msg_list) > 1
if flag:
try:
await bot.send_group_forward_msg(
group_id=event.group_id,
messages=[
{
"type": "node",
"data": {
"name": "\u200b",
"uin": bot.self_id,
"content": msg,
},
}
for msg in msg_list
],
)
start_time = arrow.now()
async with sending_lock[(event.group_id, "group")]:
await send_group_forward_msg(bot, event, msg_list)
await asyncio.sleep(
max(1 - (arrow.now() - start_time).total_seconds(), 0)
)
except ActionFailed:
flag = False
if not flag:
for msg in msg_list:
await bot.send_group_msg(group_id=event.group_id, message=msg)
start_time = arrow.now()
async with sending_lock[(event.group_id, "group")]:
await bot.send_group_msg(group_id=event.group_id, message=msg)
await asyncio.sleep(
max(1 - (arrow.now() - start_time).total_seconds(), 0)
)


async def send_group_forward_msg(
bot: Bot, event: GroupMessageEvent, msg_list: List[str]
) -> None:
await bot.send_group_forward_msg(
group_id=event.group_id,
messages=[
{
"type": "node",
"data": {
"name": "\u200b",
"uin": bot.self_id,
"content": msg,
},
}
for msg in msg_list
],
)


@IMAGE_SEARCH.handle()
Expand All @@ -164,11 +190,10 @@ async def handle_image_search(bot: Bot, event: MessageEvent, matcher: Matcher) -
indent=4,
ensure_ascii=False,
)
await asyncio.gather(
*[
send_result_message(bot, event, await image_search(i, mode, purge, db))
for i in image_urls
]
search_results = await asyncio.gather(
*[image_search(i, mode, purge, db) for i in image_urls]
)
for i in search_results:
await send_result_message(bot, event, i)
clear_expired_cache(db)
db.close()

0 comments on commit 8d7a61a

Please sign in to comment.