-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_msg.go
167 lines (156 loc) · 4.85 KB
/
send_msg.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
package handlers
import (
"fmt"
"strconv"
"github.com/hoshinonyaruko/gensokyo-kook/callapi"
"github.com/hoshinonyaruko/gensokyo-kook/config"
"github.com/hoshinonyaruko/gensokyo-kook/echo"
"github.com/hoshinonyaruko/gensokyo-kook/idmap"
"github.com/hoshinonyaruko/gensokyo-kook/mylog"
)
func init() {
callapi.RegisterHandler("send_msg", HandleSendMsg)
}
func HandleSendMsg(client callapi.Client, Token string, BaseUrl string, message callapi.ActionMessage) (string, error) {
// 使用 message.Echo 作为key来获取消息类型
var msgType string
var retmsg string
if echoStr, ok := message.Echo.(string); ok {
// 当 message.Echo 是字符串类型时执行此块
msgType = echo.GetMsgTypeByKey(echoStr)
}
if msgType == "" {
msgType = GetMessageTypeByGroupidV2(message.Params.GroupID)
}
if msgType == "" {
msgType = GetMessageTypeByUseridV2(message.Params.UserID)
}
var err error
switch msgType {
case "group":
//复用处理逻辑
retmsg, _ = HandleSendGroupMsg(client, Token, BaseUrl, message)
case "guild":
//用GroupID给ChannelID赋值,因为我们是把频道虚拟成了群
message.Params.ChannelID = message.Params.GroupID.(string)
var RChannelID string
if config.GetIdmapPro() {
// 使用RetrieveRowByIDv2还原真实的ChannelID
RChannelID, _, err = idmap.RetrieveRowByIDv2Pro(message.Params.ChannelID, message.Params.UserID.(string))
if err != nil {
mylog.Printf("error retrieving real RChannelID: %v", err)
}
} else {
// 使用RetrieveRowByIDv2还原真实的ChannelID
RChannelID, err = idmap.RetrieveRowByIDv2(message.Params.ChannelID)
if err != nil {
mylog.Printf("error retrieving real RChannelID: %v", err)
}
}
message.Params.ChannelID = RChannelID
retmsg, _ = HandleSendGuildChannelMsg(client, Token, BaseUrl, message)
case "guild_private":
//send_msg比具体的send_xxx少一层,其包含的字段类型在虚拟化场景已经失去作用
//根据userid绑定得到的具体真实事件类型,这里也有多种可能性
//1,私聊(但虚拟成了群),这里用群号取得需要的id
//2,频道私聊(但虚拟成了私聊)这里传递2个nil,用user_id去推测channel_id和guild_id
retmsg, _ = HandleSendGuildChannelPrivateMsg(client, Token, BaseUrl, message, nil, nil)
case "group_private":
//私聊信息
retmsg, _ = HandleSendPrivateMsg(client, Token, BaseUrl, message)
default:
mylog.Printf("1Unknown message type: %s", msgType)
}
return retmsg, nil
}
// 通过user_id获取messageID
func GetMessageIDByUseridOrGroupid(appID string, userID interface{}) string {
// 从appID和userID生成key
var userIDStr string
switch u := userID.(type) {
case int:
userIDStr = strconv.Itoa(u)
case int64:
userIDStr = strconv.FormatInt(u, 10)
case float64:
userIDStr = strconv.FormatFloat(u, 'f', 0, 64)
case string:
userIDStr = u
default:
// 可能需要处理其他类型或报错
return ""
}
//将真实id转为int
userid64, err := idmap.StoreIDv2(userIDStr)
if err != nil {
mylog.Printf("Error storing ID 241: %v", err)
return ""
}
key := appID + "_" + fmt.Sprint(userid64)
mylog.Printf("GetMessageIDByUseridOrGroupid_key:%v", key)
messageid := echo.GetMsgIDByKey(key)
if messageid == "" {
key := appID + "_" + userIDStr
mylog.Printf("GetMessageIDByUseridOrGroupid_key_2:%v", key)
messageid = echo.GetMsgIDByKey(key)
}
return messageid
}
// 通过user_id获取messageID
func GetMessageIDByUseridAndGroupid(appID string, userID interface{}, groupID interface{}) string {
// 从appID和userID生成key
var userIDStr string
switch u := userID.(type) {
case int:
userIDStr = strconv.Itoa(u)
case int64:
userIDStr = strconv.FormatInt(u, 10)
case float64:
userIDStr = strconv.FormatFloat(u, 'f', 0, 64)
case string:
userIDStr = u
default:
// 可能需要处理其他类型或报错
return ""
}
// 从appID和userID生成key
var GroupIDStr string
switch u := groupID.(type) {
case int:
GroupIDStr = strconv.Itoa(u)
case int64:
GroupIDStr = strconv.FormatInt(u, 10)
case float64:
GroupIDStr = strconv.FormatFloat(u, 'f', 0, 64)
case string:
GroupIDStr = u
default:
// 可能需要处理其他类型或报错
return ""
}
var userid64, groupid64 int64
var err error
if config.GetIdmapPro() {
//将真实id转为int userid64
groupid64, userid64, err = idmap.StoreIDv2Pro(GroupIDStr, userIDStr)
if err != nil {
mylog.Fatalf("Error storing ID 210: %v", err)
}
} else {
//将真实id转为int
userid64, err = idmap.StoreIDv2(userIDStr)
if err != nil {
mylog.Fatalf("Error storing ID 241: %v", err)
return ""
}
//将真实id转为int
groupid64, err = idmap.StoreIDv2(GroupIDStr)
if err != nil {
mylog.Fatalf("Error storing ID 256: %v", err)
return ""
}
}
key := appID + "_" + fmt.Sprint(groupid64) + "_" + fmt.Sprint(userid64)
mylog.Printf("GetMessageIDByUseridAndGroupid_key:%v", key)
return echo.GetMsgIDByKey(key)
}