/
event.go
124 lines (111 loc) · 3.67 KB
/
event.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
/**
* Copyright (c) 2023 Rehtt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package cqhttp_bot
import (
jsoniter "github.com/json-iterator/go"
)
func (b *Event) event(postType string, data jsoniter.Any) {
switch postType {
case "message":
if len(b.onMessage) == 0 && len(b.onPrivateMessages) == 0 && len(b.onGroupMessages) == 0 {
break
}
b.MessageEvent.eventMessage(data)
case "meta_event":
// 暂时忽略
case "request":
b.RequestEvent.eventRequest(data)
}
}
func (b *MessageEvent) eventMessage(data jsoniter.Any) {
var (
senderQid = data.Get("user_id").ToInt64()
messageId = data.Get("message_id").ToInt32()
m = NewEventMessage()
ctx = NewEventMessageContext()
)
defer m.Close()
defer ctx.Close()
m.RawMessage = data.Get("raw_message").ToString()
m.Messages = ParseMessage(m.RawMessage)
ctx.MessageId = messageId
ctx.SenderId = senderQid
ctx.Sender = Sender{
Nickname: data.Get("sender", "nickname").ToString(),
}
ctx.Message = m
ctx.GroupId = 0
switch data.Get("message_type").ToString() {
case "group":
groupId := data.Get("group_id").ToInt64()
for i := range b.onGroupMessages {
b.onGroupMessages[i](messageId, senderQid, groupId, m)
}
ctx.MessageType = Group
ctx.GroupId = groupId
case "private":
for i := range b.onPrivateMessages {
b.onPrivateMessages[i](messageId, senderQid, m)
}
ctx.MessageType = Private
}
for i := range b.onMessage {
b.onMessage[i](ctx)
}
}
// OnMessage 接收所有消息
func (b *MessageEvent) OnMessage(f OnMessageFunc) {
b.onMessage = append(b.onMessage, f)
}
// OnGroupMessage 接收群消息
func (b *MessageEvent) OnGroupMessage(f OnGroupMessageFunc) {
b.onGroupMessages = append(b.onGroupMessages, f)
}
// OnPrivateMessage 接收私人消息
func (b *MessageEvent) OnPrivateMessage(f OnPrivateMessageFunc) {
b.onPrivateMessages = append(b.onPrivateMessages, f)
}
func (r *RequestEvent) eventRequest(data jsoniter.Any) {
var (
userId = data.Get("user_id").ToInt64()
comment = data.Get("comment").ToString()
flag = data.Get("flag").ToString()
)
switch data.Get("request_type").ToString() {
case "friend":
for i := range r.onFriendRequests {
r.onFriendRequests[i](userId, comment, flag)
}
case "group":
groupId := data.Get("group_id").ToInt64()
subType := data.Get("sub_type").ToString()
for i := range r.onGroupRequests {
r.onGroupRequests[i](userId, groupId, GroupRequestEventSubType(subType), comment, flag)
}
}
}
func (r *RequestEvent) OnFriendRequest(f OnFriendRequestFunc) {
r.onFriendRequests = append(r.onFriendRequests, f)
}
func (r *RequestEvent) OnGroupRequest(f OnGroupRequestFunc) {
r.onGroupRequests = append(r.onGroupRequests, f)
}