Skip to content

Commit b2b98cc

Browse files
committed
feat: 支持更多的api
1 parent f9217aa commit b2b98cc

File tree

5 files changed

+156
-123
lines changed

5 files changed

+156
-123
lines changed

coolq/api.go

+118-120
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10+
"math"
1011
"strconv"
1112
"time"
1213

@@ -64,64 +65,61 @@ func (bot *CQBot) CQGetFriendList(spec *onebot.Spec) global.MSG {
6465
return OK(fs)
6566
}
6667

67-
// TODO 不支持的api,扔了先
6868
// CQGetUnidirectionalFriendList 获取单向好友列表
6969
//
7070
// @route(get_unidirectional_friend_list)
71-
//func (bot *CQBot) CQGetUnidirectionalFriendList() global.MSG {
72-
// list, err := bot.Client.GetUnidirectionalFriendList()
73-
// if err != nil {
74-
// log.Warnf("获取单向好友列表时出现错误: %v", err)
75-
// return Failed(100, "API_ERROR", err.Error())
76-
// }
77-
// fs := make([]global.MSG, 0, len(list))
78-
// for _, f := range list {
79-
// fs = append(fs, global.MSG{
80-
// "nickname": f.Nickname,
81-
// "user_id": f.Uin,
82-
// "source": f.Source,
83-
// })
84-
// }
85-
// return OK(fs)
86-
//}
71+
func (bot *CQBot) CQGetUnidirectionalFriendList() global.MSG {
72+
list, err := bot.Client.GetUnidirectionalFriendList()
73+
if err != nil {
74+
log.Warnf("获取单向好友列表时出现错误: %v", err)
75+
return Failed(100, "API_ERROR", err.Error())
76+
}
77+
fs := make([]global.MSG, 0, len(list))
78+
for _, f := range list {
79+
fs = append(fs, global.MSG{
80+
"nickname": f.Nickname,
81+
"user_id": f.Uin,
82+
"source": f.Source,
83+
})
84+
}
85+
return OK(fs)
86+
}
8787

88-
// TODO 不支持的api,扔了先
8988
// CQDeleteUnidirectionalFriend 删除单向好友
9089
//
9190
// @route(delete_unidirectional_friend)
9291
// @rename(uin->user_id)
93-
//func (bot *CQBot) CQDeleteUnidirectionalFriend(uin int64) global.MSG {
94-
// list, err := bot.Client.GetUnidirectionalFriendList()
95-
// if err != nil {
96-
// log.Warnf("获取单向好友列表时出现错误: %v", err)
97-
// return Failed(100, "API_ERROR", err.Error())
98-
// }
99-
// for _, f := range list {
100-
// if f.Uin == uin {
101-
// if err = bot.Client.DeleteUnidirectionalFriend(uin); err != nil {
102-
// log.Warnf("删除单向好友时出现错误: %v", err)
103-
// return Failed(100, "API_ERROR", err.Error())
104-
// }
105-
// return OK(nil)
106-
// }
107-
// }
108-
// return Failed(100, "FRIEND_NOT_FOUND", "好友不存在")
109-
//}
92+
func (bot *CQBot) CQDeleteUnidirectionalFriend(uin int64) global.MSG {
93+
list, err := bot.Client.GetUnidirectionalFriendList()
94+
if err != nil {
95+
log.Warnf("获取单向好友列表时出现错误: %v", err)
96+
return Failed(100, "API_ERROR", err.Error())
97+
}
98+
for _, f := range list {
99+
if f.Uin == uint32(uin) {
100+
if err = bot.Client.DeleteUnidirectionalFriend(uint32(uin)); err != nil {
101+
log.Warnf("删除单向好友时出现错误: %v", err)
102+
return Failed(100, "API_ERROR", err.Error())
103+
}
104+
return OK(nil)
105+
}
106+
}
107+
return Failed(100, "FRIEND_NOT_FOUND", "好友不存在")
108+
}
110109

111-
// TODO 不支持的api,扔了先
112110
// CQDeleteFriend 删除好友
113111
// @route(delete_friend)
114112
// @rename(uin->"[user_id\x2Cid].0")
115-
//func (bot *CQBot) CQDeleteFriend(uin int64) global.MSG {
116-
// if bot.Client.FindFriend(uin) == nil {
117-
// return Failed(100, "FRIEND_NOT_FOUND", "好友不存在")
118-
// }
119-
// if err := bot.Client.DeleteFriend(uin); err != nil {
120-
// log.Warnf("删除好友时出现错误: %v", err)
121-
// return Failed(100, "DELETE_API_ERROR", err.Error())
122-
// }
123-
// return OK(nil)
124-
//}
113+
func (bot *CQBot) CQDeleteFriend(uin int64, block bool) global.MSG {
114+
if bot.Client.GetCachedFriendInfo(uint32(uin)) == nil {
115+
return Failed(100, "FRIEND_NOT_FOUND", "好友不存在")
116+
}
117+
if err := bot.Client.DeleteFriend(uint32(uin), block); err != nil {
118+
log.Warnf("删除好友时出现错误: %v", err)
119+
return Failed(100, "DELETE_API_ERROR", err.Error())
120+
}
121+
return OK(nil)
122+
}
125123

126124
// CQGetGroupList 获取群列表
127125
//
@@ -278,7 +276,7 @@ func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) gl
278276
log.Warnf("上传群文件 %v 失败: 文件不存在", file)
279277
return Failed(100, "FILE_NOT_FOUND", "文件不存在")
280278
}
281-
if err := bot.Client.UploadGroupFile(uint32(groupID), file, name, utils.Ternary(folder == "", "/", folder)); err != nil {
279+
if err := bot.Client.SendGroupFile(uint32(groupID), file, name, utils.Ternary(folder == "", "/", folder)); err != nil {
282280
log.Warnf("上传群 %v 文件 %v 失败: %v", groupID, file, err)
283281
return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error())
284282
}
@@ -293,7 +291,7 @@ func (bot *CQBot) CQUploadPrivateFile(userID int64, file, name string) global.MS
293291
log.Warnf("上传群文件 %v 失败: 文件不存在", file)
294292
return Failed(100, "FILE_NOT_FOUND", "文件不存在")
295293
}
296-
if err := bot.Client.UploadPrivateFile(uint32(userID), file, name); err != nil {
294+
if err := bot.Client.SendPrivateFile(uint32(userID), file, name); err != nil {
297295
log.Warnf("上传私聊 %v 文件 %v 失败: %+v", userID, file, err)
298296
return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error())
299297
}
@@ -618,7 +616,7 @@ func (bot *CQBot) CQSendPrivateMessage(userID int64, groupID int64, m gjson.Resu
618616
func (bot *CQBot) CQSetGroupCard(groupID, userID int64, card string) global.MSG {
619617
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
620618
if m := bot.Client.GetCachedMemberInfo(uint32(userID), uint32(groupID)); m != nil {
621-
if err := bot.Client.GroupRenameMember(uint32(groupID), uint32(userID), card); err != nil {
619+
if err := bot.Client.SetGroupMemberName(uint32(groupID), uint32(userID), card); err != nil {
622620
return Failed(100, "SET_CARD_FAILED", err.Error())
623621

624622
}
@@ -636,7 +634,7 @@ func (bot *CQBot) CQSetGroupCard(groupID, userID int64, card string) global.MSG
636634
func (bot *CQBot) CQSetGroupSpecialTitle(groupID, userID int64, title string) global.MSG {
637635
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
638636
if m := bot.Client.GetCachedMemberInfo(uint32(userID), uint32(groupID)); m != nil {
639-
if err := bot.Client.GroupSetSpecialTitle(uint32(groupID), uint32(userID), title); err != nil {
637+
if err := bot.Client.SetGroupMemberSpecialTitle(uint32(groupID), uint32(userID), title); err != nil {
640638
return Failed(100, "SET_Title_FAILED", err.Error())
641639
}
642640
return OK(nil)
@@ -652,7 +650,7 @@ func (bot *CQBot) CQSetGroupSpecialTitle(groupID, userID int64, title string) gl
652650
// @rename(name->group_name)
653651
func (bot *CQBot) CQSetGroupName(groupID int64, name string) global.MSG {
654652
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
655-
if err := bot.Client.GroupRename(uint32(groupID), name); err != nil {
653+
if err := bot.Client.SetGroupName(uint32(groupID), name); err != nil {
656654
return Failed(100, "SET_NAME_FAILED", err.Error())
657655
}
658656
return OK(nil)
@@ -729,7 +727,7 @@ func (bot *CQBot) CQSetGroupKick(groupID int64, userID int64, block bool) global
729727
if m == nil {
730728
return Failed(100, "MEMBER_NOT_FOUND", "人员不存在")
731729
}
732-
if err := bot.Client.GroupKickMember(uint32(groupID), uint32(userID), block); err != nil {
730+
if err := bot.Client.KickGroupMember(uint32(groupID), uint32(userID), block); err != nil {
733731
return Failed(100, "NOT_MANAGEABLE", "机器人权限不足")
734732
}
735733
return OK(nil)
@@ -748,7 +746,7 @@ func (bot *CQBot) CQSetGroupBan(groupID, userID int64, duration uint32) global.M
748746
if duration >= 2592000 {
749747
return Failed(100, "DURATION_IS_NOT_IN_RANGE", "非法的禁言时长")
750748
}
751-
if err := bot.Client.GroupMuteMember(uint32(groupID), uint32(userID), duration); err != nil {
749+
if err := bot.Client.SetGroupMemberMute(uint32(groupID), uint32(userID), duration); err != nil {
752750
return Failed(100, "NOT_MANAGEABLE", "机器人权限不足")
753751
}
754752
return OK(nil)
@@ -764,7 +762,7 @@ func (bot *CQBot) CQSetGroupBan(groupID, userID int64, duration uint32) global.M
764762
// @default(enable=true)
765763
func (bot *CQBot) CQSetGroupWholeBan(groupID int64, enable bool) global.MSG {
766764
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
767-
if err := bot.Client.GroupMuteGlobal(uint32(groupID), enable); err != nil {
765+
if err := bot.Client.SetGroupGlobalMute(uint32(groupID), enable); err != nil {
768766
return Failed(100, "NOT_MANAGEABLE", "机器人权限不足")
769767
}
770768
return OK(nil)
@@ -778,7 +776,7 @@ func (bot *CQBot) CQSetGroupWholeBan(groupID int64, enable bool) global.MSG {
778776
// @route(set_group_leave)
779777
func (bot *CQBot) CQSetGroupLeave(groupID int64) global.MSG {
780778
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
781-
if err := bot.Client.GroupLeave(uint32(groupID)); err != nil {
779+
if err := bot.Client.SetGroupLeave(uint32(groupID)); err != nil {
782780
return Failed(100, "反正是失败了.png", err.Error())
783781
}
784782
return OK(nil)
@@ -839,7 +837,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
839837
if subType == "add" {
840838
for _, req := range append(msgs.JoinRequests, filteredmsgs.JoinRequests...) {
841839
if strconv.FormatInt(int64(req.Sequence), 10) == flag {
842-
if req.Checked() {
840+
if req.Checked {
843841
log.Warnf("处理群系统消息失败: 无法操作已处理的消息.")
844842
return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
845843
}
@@ -854,7 +852,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
854852
} else {
855853
for _, req := range append(msgs.InvitedRequests, filteredmsgs.InvitedRequests...) {
856854
if strconv.FormatInt(int64(req.Sequence), 10) == flag {
857-
if req.Checked() {
855+
if req.Checked {
858856
log.Warnf("处理群系统消息失败: 无法操作已处理的消息.")
859857
return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
860858
}
@@ -918,8 +916,8 @@ func (bot *CQBot) CQSetGroupAdmin(groupID, userID int64, enable bool) global.MSG
918916
if m.Permission != entity.Owner {
919917
return Failed(100, "PERMISSION_DENIED", "或权限不足")
920918
}
921-
if err := bot.Client.GroupSetAdmin(uint32(groupID), uint32(userID), enable); err != nil {
922-
return Failed(100, "反正是失败了.png", err.Error())
919+
if err := bot.Client.SetGroupAdmin(uint32(groupID), uint32(userID), enable); err != nil {
920+
return Failed(100, "UNKNOWN_ERROR", err.Error())
923921
}
924922
return OK(nil)
925923
}
@@ -1284,56 +1282,53 @@ func (bot *CQBot) CQGetMessage(messageID int32) global.MSG {
12841282
return OK(m)
12851283
}
12861284

1287-
// CQGetGroupSystemMessages 扩展API-获取群文件系统消息
1285+
// CQGetGroupSystemMessages 扩展API-获取群系统消息
12881286
//
12891287
// https://docs.go-cqhttp.org/api/#%E8%8E%B7%E5%8F%96%E7%BE%A4%E7%B3%BB%E7%BB%9F%E6%B6%88%E6%81%AF
12901288
// @route(get_group_system_msg)
1291-
//func (bot *CQBot) CQGetGroupSystemMessages() global.MSG {
1292-
// msg, err := bot.Client.GetGroupSystemMessages()
1293-
// if err != nil {
1294-
// log.Warnf("获取群系统消息失败: %v", err)
1295-
// return Failed(100, "SYSTEM_MSG_API_ERROR", err.Error())
1296-
// }
1297-
// return OK(msg)
1298-
//}
1289+
func (bot *CQBot) CQGetGroupSystemMessages() global.MSG {
1290+
msgs, err := bot.Client.GetGroupSystemMessages(false, 20)
1291+
if err != nil {
1292+
log.Warnf("获取群系统消息失败: %v", err)
1293+
return Failed(100, "SYSTEM_MSG_API_ERROR", err.Error())
1294+
}
1295+
return OK(msgs)
1296+
}
12991297

13001298
// CQGetGroupMessageHistory 获取群消息历史记录
13011299
//
13021300
// https://docs.go-cqhttp.org/api/#%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%B6%88%E6%81%AF%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95
13031301
// @route(get_group_msg_history)
13041302
// @rename(seq->message_seq)
1305-
//func (bot *CQBot) CQGetGroupMessageHistory(groupID int64, seq int64) global.MSG {
1306-
// if g, _ := bot.Client.GetCachedGroupInfo(uint32(groupID)); g == nil {
1307-
// return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
1308-
// }
1309-
// if seq == 0 {
1310-
// g, err := bot.Client.GetCachedGroupInfo(uint32(groupID))
1311-
// if err != nil {
1312-
// return Failed(100, "GROUP_INFO_API_ERROR", err.Error())
1313-
// }
1314-
// seq = g.LastMsgSeq
1315-
// }
1316-
// msg, err := bot.Client.GetGroupMessages(groupID, int64(math.Max(float64(seq-19), 1)), seq)
1317-
// if err != nil {
1318-
// log.Warnf("获取群历史消息失败: %v", err)
1319-
// return Failed(100, "MESSAGES_API_ERROR", err.Error())
1320-
// }
1321-
// source := message.Source{
1322-
// SourceType: message.SourcePrivate,
1323-
// PrimaryID: 0,
1324-
// }
1325-
// ms := make([]*event, 0, len(msg))
1326-
// for _, m := range msg {
1327-
// bot.checkMedia(m.Elements, groupID)
1328-
// id := bot.InsertGroupMessage(m, source)
1329-
// t := bot.formatGroupMessage(m)
1330-
// t.Others["message_id"] = id
1331-
// ms = append(ms, t)
1332-
// }
1333-
// return OK(global.MSG{
1334-
// "messages": ms,
1335-
// })
1336-
//}
1303+
func (bot *CQBot) CQGetGroupMessageHistory(groupID int64, seq int64) global.MSG {
1304+
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g == nil {
1305+
return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
1306+
}
1307+
if seq == 0 {
1308+
g := bot.Client.GetCachedGroupInfo(uint32(groupID))
1309+
seq = int64(g.LastMsgSeq)
1310+
}
1311+
msg, err := bot.Client.GetGroupMessages(uint32(groupID), uint32(math.Max(float64(seq-19), 1)), uint32(seq))
1312+
if err != nil {
1313+
log.Warnf("获取群历史消息失败: %v", err)
1314+
return Failed(100, "MESSAGES_API_ERROR", err.Error())
1315+
}
1316+
source := message.Source{
1317+
SourceType: message.SourcePrivate,
1318+
PrimaryID: 0,
1319+
}
1320+
ms := make([]*event, 0, len(msg))
1321+
for _, m := range msg {
1322+
bot.checkMedia(m.Elements, source)
1323+
id := bot.InsertGroupMessage(m, source)
1324+
t := bot.formatGroupMessage(m)
1325+
t.Others["message_id"] = id
1326+
ms = append(ms, t)
1327+
}
1328+
return OK(global.MSG{
1329+
"messages": ms,
1330+
})
1331+
}
13371332

13381333
// CQGetOnlineClients 扩展API-获取当前账号在线客户端列表
13391334
//
@@ -1537,11 +1532,12 @@ func (bot *CQBot) CQGetEssenceMessageList(groupID int64) global.MSG {
15371532
//
15381533
// https://docs.go-cqhttp.org/api/#%E6%A3%80%E6%9F%A5%E9%93%BE%E6%8E%A5%E5%AE%89%E5%85%A8%E6%80%A7
15391534
// @route(check_url_safely)
1540-
//func (bot *CQBot) CQCheckURLSafely(url string) global.MSG {
1541-
// return OK(global.MSG{
1542-
// "level": bot.Client.CheckUrlSafely(url),
1543-
// })
1544-
//}
1535+
func (bot *CQBot) CQCheckURLSafely(url string) global.MSG {
1536+
level, _ := bot.Client.CheckURLSafely(url)
1537+
return OK(global.MSG{
1538+
"level": level,
1539+
})
1540+
}
15451541

15461542
// CQGetVersionInfo 获取版本信息
15471543
//
@@ -1613,24 +1609,26 @@ func (bot *CQBot) CQSendGroupSign(groupID int64) global.MSG {
16131609
// return OK(nil)
16141610
//}
16151611

1616-
// TODO 计划实现的api
16171612
// CQMarkMessageAsRead 标记消息已读
16181613
// @route(mark_msg_as_read)
16191614
// @rename(msg_id->message_id)
1620-
//func (bot *CQBot) CQMarkMessageAsRead(msgID int32) global.MSG {
1621-
// m, err := db.GetMessageByGlobalID(msgID)
1622-
// if err != nil {
1623-
// return Failed(100, "MSG_NOT_FOUND", "消息不存在")
1624-
// }
1625-
// switch o := m.(type) {
1626-
// case *db.StoredGroupMessage:
1627-
// bot.Client.MarkGroupMessageReaded(o.GroupCode, int64(o.Attribute.MessageSeq))
1628-
// return OK(nil)
1629-
// case *db.StoredPrivateMessage:
1630-
// bot.Client.MarkPrivateMessageReaded(o.SessionUin, o.Attribute.Timestamp)
1631-
// }
1632-
// return OK(nil)
1633-
//}
1615+
func (bot *CQBot) CQMarkMessageAsRead(msgID int32) global.MSG {
1616+
m, err := db.GetMessageByGlobalID(msgID)
1617+
if err != nil {
1618+
return Failed(100, "MSG_NOT_FOUND", "消息不存在")
1619+
}
1620+
switch o := m.(type) {
1621+
case *db.StoredGroupMessage:
1622+
if bot.Client.MarkGroupMessageReaded(uint32(o.GroupCode), uint32(o.Attribute.MessageSeq)) != nil {
1623+
return Failed(100, "ACTION_FAILED", "标记群消息已读失败")
1624+
}
1625+
case *db.StoredPrivateMessage:
1626+
if bot.Client.MarkPrivateMessageReaded(uint32(o.SessionUin), uint32(o.Attribute.Timestamp), uint32(o.Attribute.MessageSeq)) != nil {
1627+
return Failed(100, "ACTION_FAILED", "标记私聊消息已读失败")
1628+
}
1629+
}
1630+
return OK(nil)
1631+
}
16341632

16351633
// CQSetQQProfile 设置 QQ 资料
16361634
//

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/FloatTech/sqlite v1.6.3
7-
github.com/LagrangeDev/LagrangeGo v0.1.2-0.20241127060254-2b65455dd776
7+
github.com/LagrangeDev/LagrangeGo v0.1.2
88
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a
99
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
1010
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5

0 commit comments

Comments
 (0)