-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat.go
222 lines (216 loc) · 4.92 KB
/
at.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"
"NothinBot/TimeLayout"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
type whoAtMe struct {
groupId int
atId int
atList []*EasyBot.CQMessage
atListLen int
}
// 获取
func (w *whoAtMe) get() *whoAtMe {
var atList []*EasyBot.CQMessage
tables := func() []int {
if w.groupId == 0 {
var tables []int
for i := range bot.MessageTableGroup {
tables = append(tables, i)
}
return tables
}
return []int{w.groupId}
}()
for _, i := range tables {
table := bot.MessageTableGroup[i]
for _, msg := range table {
for _, at := range msg.Extra.AtWho {
if w.atId == at {
atList = append(atList, msg)
}
}
}
}
sort.Slice(
atList, func(i, j int) bool { //根据msg的时间戳由大到小排序
return atList[i].Event.Time > atList[j].Event.Time
},
)
w.atList = atList
w.atListLen = len(atList)
return w
}
func (w *whoAtMe) format() (forwardMsg EasyBot.CQForwardMsg) {
atList := w.atList
atListLen := len(atList)
if atListLen > 99 { //超过100条合并转发放不下, 标题占1条
atListLen = 99
}
forwardMsg = EasyBot.NewForwardMsg(
EasyBot.NewCustomForwardNodeOSR(
//标题
func() string {
if w.groupId != 0 {
return fmt.Sprintf(
"%s之后群%d中最近%d条at过%d的消息:", time.Unix(startTime, 0).Format(TimeLayout.M24C), w.groupId,
atListLen, w.atId,
)
} else {
return fmt.Sprintf(
"%s之后所有群中最近%d条at过%d的消息:", time.Unix(startTime, 0).Format(TimeLayout.M24C), atListLen,
w.atId,
)
}
}(),
),
)
for i := 0; i < atListLen; i++ {
atMsg := w.atList[i]
content := fmt.Sprintf(
"%s\n\n--[%s]%s%s%s",
strings.ReplaceAll(atMsg.RawMessage, "CQ:at,qq=", "@"),
atMsg.Extra.TimeFormat,
atMsg.GetCardOrNickname(),
func() string {
if w.groupId != 0 {
return ""
} else { //查看所有群的时候补充来源群
return fmt.Sprintf(" (%d)", atMsg.GroupID)
}
}(),
func() string {
if atMsg.Extra.Recalled {
if atMsg.Extra.OperatorID == atMsg.UserID {
return "(已撤回)"
} else {
return "(已被他人撤回)"
}
} else {
return ""
}
}(),
)
forwardMsg = EasyBot.AppendForwardMsg(
forwardMsg, EasyBot.NewCustomForwardNodeOSR(content),
)
}
return
}
// 格式化
func (w *whoAtMe) formatGocq() (forwardMsg EasyBot.CQForwardMsg) {
atList := w.atList
atListLen := len(atList)
if atListLen > 99 { //超过100条合并转发放不下, 标题占1条
atListLen = 99
}
forwardMsg = EasyBot.NewForwardMsg(
EasyBot.NewCustomForwardNode(
//标题
"NothingBot",
bot.GetSelfId(),
func() string {
if w.groupId != 0 {
return fmt.Sprintf(
"%s之后群%d中最近%d条at过%d的消息:", time.Unix(startTime, 0).Format(TimeLayout.M24C), w.groupId,
atListLen, w.atId,
)
} else {
return fmt.Sprintf(
"%s之后所有群中最近%d条at过%d的消息:", time.Unix(startTime, 0).Format(TimeLayout.M24C), atListLen,
w.atId,
)
}
}(),
0, 0,
),
)
for i := 0; i < atListLen; i++ {
atMsg := w.atList[i]
name := fmt.Sprintf(
`[%s]%s%s%s`,
atMsg.Extra.TimeFormat,
atMsg.GetCardOrNickname(),
func() string {
if w.groupId != 0 {
return ""
} else { //查看所有群的时候补充来源群
return fmt.Sprintf(" (%d)", atMsg.GroupID)
}
}(),
func() string {
if atMsg.Extra.Recalled {
if atMsg.Extra.OperatorID == atMsg.UserID {
return "(已撤回)"
} else {
return "(已被他人撤回)"
}
} else {
return ""
}
}(),
)
uin := func() (uin int) {
if atMsg.UserID != 0 {
return atMsg.UserID
}
return bot.GetSelfId()
}()
content := strings.ReplaceAll(atMsg.Extra.MessageWithReply, "CQ:at,", "CQ:at,") //插入零宽空格阻止CQ码解析
forwardMsg = EasyBot.AppendForwardMsg(
forwardMsg, EasyBot.NewCustomForwardNode(
name, uin, content, 0, 0,
),
)
}
return
}
// 谁at我
func checkWhoAtMe(ctx *EasyBot.CQMessage) {
match := ctx.RegFindAllStringSubmatch(regexp.MustCompile(`^谁(@|[aA艾][tT特])(我|(\s*\[CQ:at,qq=)?([0-9]{1,11})?(]\s*))$`))
if len(match) > 0 {
var atId int
if match[0][2] == "我" {
atId = ctx.UserID
} else {
var err error
atId, err = strconv.Atoi(match[0][4])
if err != nil {
return
}
}
w := whoAtMe{
atId: atId,
groupId: func() int {
if ctx.MessageType == "group" {
return ctx.GroupID
}
return 0
}(),
}
w.get()
if w.atListLen == 0 {
if w.groupId != 0 {
ctx.SendMsgReply(
fmt.Sprintf(
"%s之后群%d中没有人at过%d", time.Unix(startTime, 0).Format(TimeLayout.M24C), w.groupId, w.atId,
),
)
} else {
ctx.SendMsgReply(
fmt.Sprintf(
"%s之后所有群中没有人at过%d", time.Unix(startTime, 0).Format(TimeLayout.M24C), w.atId,
),
)
}
return
}
ctx.SendForwardMsg(w.format())
}
}