@@ -2,9 +2,13 @@ package setutime
2
2
3
3
import (
4
4
"fmt"
5
+ "strconv"
6
+ "strings"
7
+ "time"
5
8
6
9
utils "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime/utils"
7
10
zero "github.com/wdvxdr1123/ZeroBot"
11
+ "github.com/wdvxdr1123/ZeroBot/message"
8
12
)
9
13
10
14
func init () { // 插件主体
@@ -28,111 +32,97 @@ func init() { // 插件主体
28
32
ctx .Send (illust .DetailPic )
29
33
return
30
34
})
31
- // 通过回复以图搜图
32
- zero .OnRegex ( `\[CQ:reply,id=(.*?)\](.*)搜索图片` ).SetBlock (true ).SetPriority (32 ).
35
+ // 以图搜图
36
+ zero .OnMessage ( FullMatchText ( "以图搜图" , "搜索图片" , "以图识图" ), MustHasPicture () ).SetBlock (true ).SetPriority (999 ).
33
37
Handle (func (ctx * zero.Ctx ) {
34
- var pics []string // 图片搜索池子
35
- // 获取回复的上文图片链接
36
- id := utils .Str2Int (ctx .State ["regex_matched" ].([]string )[1 ])
37
- for _ , elem := range ctx .GetMessage (id ).Elements {
38
- if elem .Type == "image" {
39
- pics = append (pics , elem .Data ["url" ])
40
- }
41
- }
42
- // 没有收到图片则向用户索取
43
- if len (pics ) == 0 {
44
- ctx .Send ("请发送多张图片!" )
45
- next := ctx .FutureEvent ("message" , ctx .CheckSession ())
46
- recv , cancel := next .Repeat ()
47
- for e := range recv { // 循环获取channel发来的信息
48
- if len (e .Message ) == 1 && e .Message [0 ].Type == "text" {
49
- cancel () // 如果是纯文本则退出索取
50
- break
51
- }
52
- for _ , elem := range e .Message {
53
- if elem .Type == "image" { // 将信息中的图片添加到搜索池子
54
- pics = append (pics , elem .Data ["url" ])
55
- }
56
- }
57
- if len (pics ) >= 5 {
58
- cancel () // 如果是图片数量大于等于5则退出索取
59
- break
60
- }
61
- }
62
- }
63
- if len (pics ) == 0 {
64
- ctx .Send ("没有收到图片,搜图结束......" )
65
- return
66
- }
67
38
// 开始搜索图片
68
39
ctx .Send ("少女祈祷中......" )
69
- for _ , pic := range pics {
70
- if text , err := utils .SauceNaoSearch (pic ); err == nil {
71
- ctx .Send (text ) // 返回SauceNAO的结果
40
+ for _ , pic := range ctx .State ["image_url" ].([]string ) {
41
+ fmt .Println (pic )
42
+ if m , err := utils .SauceNaoSearch (pic ); err == nil {
43
+ ctx .SendChain (m ... ) // 返回SauceNAO的结果
72
44
continue
73
45
} else {
74
- ctx .Send ( fmt . Sprintf ("ERROR: %v " , err ))
46
+ ctx .SendChain ( message . Text ("ERROR: " , err ))
75
47
}
76
- if text , err := utils .Ascii2dSearch (pic ); err == nil {
77
- ctx .Send ( text ) // 返回Ascii2d的结果
48
+ if m , err := utils .Ascii2dSearch (pic ); err == nil {
49
+ ctx .SendChain ( m ... ) // 返回Ascii2d的结果
78
50
continue
79
51
} else {
80
- ctx .Send ( fmt . Sprintf ("ERROR: %v " , err ))
52
+ ctx .SendChain ( message . Text ("ERROR: " , err ))
81
53
}
82
54
}
83
55
return
84
56
})
85
- // 通过命令以图搜图
86
- zero .OnKeywordGroup ([]string {"以图识图" , "以图搜图" , "搜索图片" }).SetBlock (true ).SetPriority (33 ).
87
- Handle (func (ctx * zero.Ctx ) {
88
- var pics []string // 图片搜索池子
89
- // 获取信息中图片链接
90
- for _ , elem := range ctx .Event .Message {
91
- if elem .Type == "image" {
92
- pics = append (pics , elem .Data ["url" ])
93
- }
94
- }
95
- // 没有收到图片则向用户索取
96
- if len (pics ) == 0 {
97
- ctx .Send ("请发送多张图片!" )
98
- next := ctx .FutureEvent ("message" , zero .CheckUser (ctx .Event .UserID ))
99
- recv , cancel := next .Repeat ()
100
- for e := range recv { // 循环获取channel发来的信息
101
- if len (e .Message ) == 1 && e .Message [0 ].Type == "text" {
102
- cancel () // 如果是纯文本则退出索取
103
- break
104
- }
105
- for _ , elem := range e .Message {
106
- if elem .Type == "image" { // 将信息中的图片添加到搜索池子
107
- pics = append (pics , elem .Data ["url" ])
108
- }
109
- }
110
- if len (pics ) >= 5 {
111
- cancel () // 如果是图片数量大于等于5则退出索取
112
- break
57
+ }
58
+
59
+ // FullMatchText 如果信息中文本完全匹配则返回 true
60
+ func FullMatchText (src ... string ) zero.Rule {
61
+ return func (ctx * zero.Ctx ) bool {
62
+ msg := ctx .Event .Message
63
+ for _ , elem := range msg {
64
+ if elem .Type == "text" {
65
+ text := elem .Data ["text" ]
66
+ text = strings .ReplaceAll (text , " " , "" )
67
+ text = strings .ReplaceAll (text , "\r " , "" )
68
+ text = strings .ReplaceAll (text , "\n " , "" )
69
+ for _ , s := range src {
70
+ if text == s {
71
+ return true
113
72
}
114
73
}
115
74
}
116
- if len (pics ) == 0 {
117
- ctx .Send ("没有收到图片,搜图结束......" )
118
- return
75
+ }
76
+ return false
77
+ }
78
+ }
79
+
80
+ // HasPicture 消息含有图片返回 true
81
+ func HasPicture () zero.Rule {
82
+ return func (ctx * zero.Ctx ) bool {
83
+ msg := ctx .Event .Message
84
+ url := []string {}
85
+ // 如果是回复信息则将信息替换成被回复的那条
86
+ if msg [0 ].Type == "reply" {
87
+ id , _ := strconv .Atoi (msg [0 ].Data ["id" ])
88
+ msg = ctx .GetMessage (int64 (id )).Elements
89
+ }
90
+ // 遍历信息中所有图片
91
+ for _ , elem := range msg {
92
+ if elem .Type == "image" {
93
+ url = append (url , elem .Data ["url" ])
119
94
}
120
- // 开始搜索图片
121
- ctx .Send ("少女祈祷中......" )
122
- for _ , pic := range pics {
123
- if text , err := utils .SauceNaoSearch (pic ); err == nil {
124
- ctx .Send (text ) // 返回SauceNAO的结果
125
- continue
126
- } else {
127
- ctx .Send (fmt .Sprintf ("ERROR: %v" , err ))
128
- }
129
- if text , err := utils .Ascii2dSearch (pic ); err == nil {
130
- ctx .Send (text ) // 返回Ascii2d的结果
131
- continue
132
- } else {
133
- ctx .Send (fmt .Sprintf ("ERROR: %v" , err ))
134
- }
95
+ }
96
+ // 如果有图片就返回true
97
+ if len (url ) > 0 {
98
+ ctx .State ["image_url" ] = url
99
+ return true
100
+ }
101
+ return false
102
+ }
103
+ }
104
+
105
+ // MustHasPicture 消息不存在图片阻塞60秒至有图片,超时返回 false
106
+ func MustHasPicture () zero.Rule {
107
+ return func (ctx * zero.Ctx ) bool {
108
+ if HasPicture ()(ctx ) {
109
+ return true
110
+ }
111
+ // 没有图片就索取
112
+ ctx .Send ("请发送一张图片" )
113
+ next := zero .NewFutureEvent ("message" , 999 , false , zero .CheckUser (ctx .Event .UserID ), HasPicture ())
114
+ recv , cancel := next .Repeat ()
115
+ select {
116
+ case e := <- recv :
117
+ cancel ()
118
+ newCtx := & zero.Ctx {Event : e , State : zero.State {}}
119
+ if HasPicture ()(newCtx ) {
120
+ ctx .State ["image_url" ] = newCtx .State ["image_url" ]
121
+ return true
135
122
}
136
- return
137
- })
123
+ return false
124
+ case <- time .After (time .Second * 60 ):
125
+ return false
126
+ }
127
+ }
138
128
}
0 commit comments