Skip to content

Commit 7202d9a

Browse files
committed
refactor(__init__): 重构并精简触发搜图的相关逻辑
1 parent 2c18e5b commit 7202d9a

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

YetAnotherPicSearch/__init__.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
)
1919
from nonebot.log import logger
2020
from nonebot.matcher import Matcher
21-
from nonebot.params import CommandArg
22-
from nonebot.plugin.on import on_command, on_message, on_metaevent
21+
from nonebot.plugin.on import on_message, on_metaevent
2322
from nonebot.rule import Rule
2423
from PicImageSearch import Network
2524
from tenacity import AsyncRetrying, stop_after_attempt, stop_after_delay
@@ -37,10 +36,6 @@
3736
asyncio.Lock
3837
)
3938

40-
# issue #30 and #32 ?
41-
# if sys.version_info >= (3, 8) and sys.platform == "win32":
42-
# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
43-
4439

4540
def check_first_connect(_: LifecycleMetaEvent) -> bool:
4641
return True
@@ -65,32 +60,27 @@ def has_images(event: MessageEvent) -> bool:
6560

6661
def to_me_with_images(bot: Bot, event: MessageEvent) -> bool:
6762
plain_text = event.message.extract_plain_text()
68-
if plain_text.startswith("搜图"):
69-
return False
70-
has_image = has_images(event)
63+
has_command = "搜图" in plain_text
7164
if isinstance(event, PrivateMessageEvent):
72-
return has_image and config.search_immediately
65+
return config.search_immediately or has_command
7366
# 群里回复机器人发送的消息时,必须带上 "搜图" 才会搜图,否则会被无视
7467
if event.reply and event.reply.sender.user_id == int(bot.self_id):
75-
return has_image and "搜图" in plain_text
68+
return has_command
7669
at_me = bool(
7770
[i for i in event.message if i.type == "at" and i.data["qq"] == bot.self_id]
7871
)
79-
return has_image and (event.to_me or at_me or "搜图" in plain_text)
72+
return event.to_me or at_me or has_command
8073

8174

8275
IMAGE_SEARCH = on_message(rule=Rule(to_me_with_images), priority=5)
83-
IMAGE_SEARCH_MODE = on_command("搜图", priority=5)
8476

8577

86-
@IMAGE_SEARCH_MODE.handle()
87-
async def handle_first_receive(
88-
event: MessageEvent, matcher: Matcher, args: Message = CommandArg()
89-
) -> None:
90-
mode, purge = get_args(args)
78+
@IMAGE_SEARCH.handle()
79+
async def handle_first_receive(event: MessageEvent, matcher: Matcher) -> None:
80+
mode, purge = get_args(event.message)
9181
matcher.state["ARGS"] = (mode, purge)
9282
if has_images(event):
93-
matcher.set_arg("IMAGES", args)
83+
matcher.state["IMAGES"] = event
9484

9585

9686
async def image_search(
@@ -234,16 +224,12 @@ async def send_forward_msg(
234224
)
235225

236226

237-
@IMAGE_SEARCH.handle()
238-
@IMAGE_SEARCH_MODE.got("IMAGES", prompt="请发送图片")
227+
@IMAGE_SEARCH.got("IMAGES", prompt="请发送图片")
239228
async def handle_image_search(bot: Bot, event: MessageEvent, matcher: Matcher) -> None:
240229
image_urls_with_md5 = get_image_urls_with_md5(event)
241230
if not image_urls_with_md5:
242-
await IMAGE_SEARCH_MODE.reject()
243-
if "ARGS" in matcher.state:
244-
mode, purge = matcher.state["ARGS"]
245-
else:
246-
mode, purge = get_args(event.message)
231+
await IMAGE_SEARCH.reject()
232+
mode, purge = matcher.state["ARGS"]
247233
network = (
248234
Network(proxies=config.proxy, cookies=config.exhentai_cookies, timeout=60)
249235
if mode == "ex"

0 commit comments

Comments
 (0)