18
18
)
19
19
from nonebot .log import logger
20
20
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
23
22
from nonebot .rule import Rule
24
23
from PicImageSearch import Network
25
24
from tenacity import AsyncRetrying , stop_after_attempt , stop_after_delay
37
36
asyncio .Lock
38
37
)
39
38
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
-
44
39
45
40
def check_first_connect (_ : LifecycleMetaEvent ) -> bool :
46
41
return True
@@ -65,32 +60,27 @@ def has_images(event: MessageEvent) -> bool:
65
60
66
61
def to_me_with_images (bot : Bot , event : MessageEvent ) -> bool :
67
62
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
71
64
if isinstance (event , PrivateMessageEvent ):
72
- return has_image and config .search_immediately
65
+ return config .search_immediately or has_command
73
66
# 群里回复机器人发送的消息时,必须带上 "搜图" 才会搜图,否则会被无视
74
67
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
76
69
at_me = bool (
77
70
[i for i in event .message if i .type == "at" and i .data ["qq" ] == bot .self_id ]
78
71
)
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
80
73
81
74
82
75
IMAGE_SEARCH = on_message (rule = Rule (to_me_with_images ), priority = 5 )
83
- IMAGE_SEARCH_MODE = on_command ("搜图" , priority = 5 )
84
76
85
77
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 )
91
81
matcher .state ["ARGS" ] = (mode , purge )
92
82
if has_images (event ):
93
- matcher .set_arg ( "IMAGES" , args )
83
+ matcher .state [ "IMAGES" ] = event
94
84
95
85
96
86
async def image_search (
@@ -234,16 +224,12 @@ async def send_forward_msg(
234
224
)
235
225
236
226
237
- @IMAGE_SEARCH .handle ()
238
- @IMAGE_SEARCH_MODE .got ("IMAGES" , prompt = "请发送图片" )
227
+ @IMAGE_SEARCH .got ("IMAGES" , prompt = "请发送图片" )
239
228
async def handle_image_search (bot : Bot , event : MessageEvent , matcher : Matcher ) -> None :
240
229
image_urls_with_md5 = get_image_urls_with_md5 (event )
241
230
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" ]
247
233
network = (
248
234
Network (proxies = config .proxy , cookies = config .exhentai_cookies , timeout = 60 )
249
235
if mode == "ex"
0 commit comments