-
Notifications
You must be signed in to change notification settings - Fork 0
/
corpus.go
222 lines (201 loc) · 5.59 KB
/
corpus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"NothinBot/EasyBot"
"fmt"
"strconv"
"time"
"regexp"
log "github.com/sirupsen/logrus"
)
type corpus struct {
regstr string
regexp *regexp.Regexp
reply string
replyForwardMsg EasyBot.CQForwardMsg
scene string
delay time.Duration
}
var allCorpuses []corpus
// 初始化语料库
func initCorpus() {
allCorpuses = []corpus{}
configsRaw := v.Get("corpus")
if configsRaw == nil {
log.Info("[Init] 未找到语料库")
return
}
configs, isArr := configsRaw.([]any)
if !isArr {
log.Error("[Init] corpus 内容应为数组")
return
}
for i, config := range configs {
configMap, isMap := config.(map[string]any)
if !isMap {
log.Error("[Init] corpus.", i, " 内容数据类型错误")
continue
}
corpus := corpus{}
if regStr := fmt.Sprint(configMap["regexp"]); regStr != "" {
corpus.regstr = regStr
} else {
log.Error("[Init] corpus.", i, ".regexp 正则表达式非法, 禁止为空")
continue
}
if regExp, err := regexp.Compile(corpus.regstr); err == nil {
corpus.regexp = regExp
} else {
log.Error("[Init] corpus.", i, ".regexp 正则表达式非法, err: ", err)
continue
}
if sceneStr := fmt.Sprint(configMap["scene"]); sceneStr == "a" || sceneStr == "all" || sceneStr == "g" || sceneStr == "group" || sceneStr == "p" || sceneStr == "private" {
corpus.scene = sceneStr
} else {
log.Error("[Init] corpus.", i, ".scene 需要在\"a\",\"g\",\"p\"之间指定一个触发场景")
continue
}
corpus.delay = func() (delay time.Duration) {
delayRaw := v.Get(fmt.Sprint("corpus.", i, ".delay"))
delayInt, isInt := delayRaw.(int)
delayFloat, isFloat := delayRaw.(float64)
delayString, isString := delayRaw.(string)
delayParseFloat, err := strconv.ParseFloat(delayString, 64)
switch {
case delayRaw == nil:
return 0
case (!isInt && !isFloat && !isString) || (isString && err != nil):
log.Error(
"[Init] corpus.", i, ".scene 延迟格式错误 isInt: ", isInt, " isFloat: ", isFloat, " isString: ",
isString, " err: ", err,
)
return 0
default: // (isInt || isFloat || isString) && (!isString || err == nil)
switch {
case isInt:
return time.Millisecond * 1000 * time.Duration(delayInt)
case isFloat:
return time.Millisecond * 1000 * time.Duration(delayFloat)
case isString:
return time.Millisecond * 1000 * time.Duration(delayParseFloat)
}
}
return 0
}()
corpus.reply, corpus.replyForwardMsg = func() (string, EasyBot.CQForwardMsg) {
replySlice, isSlice := configMap["reply"].([]any)
switch {
default: //corpus.reply
return fmt.Sprint(configMap["reply"]), nil
case isSlice: //corpus.replyForwardMsg
return "", func() (forwardMsg EasyBot.CQForwardMsg) {
for _, eachReply := range replySlice {
if eachReplyMap, isMap := eachReply.(map[string]any); isMap { //自定义名字、头像
//使用自定义信息
name := func() (name string) {
if nameAny := eachReplyMap["name"]; nameAny != nil {
return fmt.Sprint(nameAny)
}
return "NothingBot"
}()
uin := func() (uin int) {
if uinAny := eachReplyMap["uin"]; uinAny != nil {
if uinInt, isInt := uinAny.(int); isInt {
return uinInt
}
}
return bot.GetSelfId()
}()
timestamp := func() (timestamp int64) {
if tsAny := eachReplyMap["time"]; tsAny != nil {
if tsInt, isInt := tsAny.(int); isInt {
return int64(tsInt)
}
}
return 0
}()
seq := func() (seq int64) {
if seqAny := eachReplyMap["time"]; seqAny != nil {
if seqInt, isInt := seqAny.(int); isInt {
return int64(seqInt)
}
}
return 0
}()
eachReplyMapContentSlice, isSlice := eachReplyMap["content"].([]any)
if !isSlice {
forwardMsg = EasyBot.AppendForwardMsg(
forwardMsg,
EasyBot.NewCustomForwardNode(
name,
uin,
fmt.Sprint(eachReplyMap["content"]),
timestamp, seq,
),
)
}
if isSlice {
for _, eachEachReplyMapContentSlice := range eachReplyMapContentSlice {
forwardMsg = EasyBot.AppendForwardMsg(
forwardMsg,
EasyBot.NewCustomForwardNode(
name,
uin,
fmt.Sprint(eachEachReplyMapContentSlice),
timestamp, seq,
),
)
}
}
} else { //未自定义名字、头像
//使用bot信息
forwardMsg = EasyBot.AppendForwardMsg(
forwardMsg,
EasyBot.NewCustomForwardNode(
"NothingBot",
bot.GetSelfId(),
fmt.Sprint(eachReply),
0, 0,
),
)
}
}
return
}()
}
}()
allCorpuses = append(allCorpuses, corpus)
}
}
// 语料库
func checkCorpus(ctx *EasyBot.CQMessage) {
for i, c := range allCorpuses {
log.Trace("[corpus] 匹配语料库: ", i, " 正则: ", c.regstr)
matches := c.regexp.FindAllStringSubmatch(ctx.RawMessage, -1)
if len(matches) > 0 {
var ok bool
switch c.scene {
case "a", "all":
ok = true
case "p", "private":
if ctx.MessageType == "private" {
ok = true
}
case "g", "group":
if ctx.MessageType == "group" {
ok = true
}
}
if ok {
log.Info("[corpus] 成功匹配语料库 ", i, " 正则: ", c.regstr)
go func(c corpus) {
time.Sleep(c.delay)
if c.reply != "" {
ctx.SendMsg(c.reply)
} else {
ctx.SendForwardMsg(c.replyForwardMsg)
}
}(c)
}
}
}
}