Skip to content

Commit

Permalink
feat: 支持更多的api
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn committed Nov 28, 2024
1 parent f9217aa commit b2b98cc
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 123 deletions.
238 changes: 118 additions & 120 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"strconv"
"time"

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

// TODO 不支持的api,扔了先
// CQGetUnidirectionalFriendList 获取单向好友列表
//
// @route(get_unidirectional_friend_list)
//func (bot *CQBot) CQGetUnidirectionalFriendList() global.MSG {
// list, err := bot.Client.GetUnidirectionalFriendList()
// if err != nil {
// log.Warnf("获取单向好友列表时出现错误: %v", err)
// return Failed(100, "API_ERROR", err.Error())
// }
// fs := make([]global.MSG, 0, len(list))
// for _, f := range list {
// fs = append(fs, global.MSG{
// "nickname": f.Nickname,
// "user_id": f.Uin,
// "source": f.Source,
// })
// }
// return OK(fs)
//}
func (bot *CQBot) CQGetUnidirectionalFriendList() global.MSG {
list, err := bot.Client.GetUnidirectionalFriendList()
if err != nil {
log.Warnf("获取单向好友列表时出现错误: %v", err)
return Failed(100, "API_ERROR", err.Error())
}
fs := make([]global.MSG, 0, len(list))
for _, f := range list {
fs = append(fs, global.MSG{
"nickname": f.Nickname,
"user_id": f.Uin,
"source": f.Source,
})
}
return OK(fs)
}

// TODO 不支持的api,扔了先
// CQDeleteUnidirectionalFriend 删除单向好友
//
// @route(delete_unidirectional_friend)
// @rename(uin->user_id)
//func (bot *CQBot) CQDeleteUnidirectionalFriend(uin int64) global.MSG {
// list, err := bot.Client.GetUnidirectionalFriendList()
// if err != nil {
// log.Warnf("获取单向好友列表时出现错误: %v", err)
// return Failed(100, "API_ERROR", err.Error())
// }
// for _, f := range list {
// if f.Uin == uin {
// if err = bot.Client.DeleteUnidirectionalFriend(uin); err != nil {
// log.Warnf("删除单向好友时出现错误: %v", err)
// return Failed(100, "API_ERROR", err.Error())
// }
// return OK(nil)
// }
// }
// return Failed(100, "FRIEND_NOT_FOUND", "好友不存在")
//}
func (bot *CQBot) CQDeleteUnidirectionalFriend(uin int64) global.MSG {
list, err := bot.Client.GetUnidirectionalFriendList()
if err != nil {
log.Warnf("获取单向好友列表时出现错误: %v", err)
return Failed(100, "API_ERROR", err.Error())
}
for _, f := range list {
if f.Uin == uint32(uin) {
if err = bot.Client.DeleteUnidirectionalFriend(uint32(uin)); err != nil {
log.Warnf("删除单向好友时出现错误: %v", err)
return Failed(100, "API_ERROR", err.Error())
}
return OK(nil)
}
}
return Failed(100, "FRIEND_NOT_FOUND", "好友不存在")
}

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

// CQGetGroupList 获取群列表
//
Expand Down Expand Up @@ -278,7 +276,7 @@ func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) gl
log.Warnf("上传群文件 %v 失败: 文件不存在", file)
return Failed(100, "FILE_NOT_FOUND", "文件不存在")
}
if err := bot.Client.UploadGroupFile(uint32(groupID), file, name, utils.Ternary(folder == "", "/", folder)); err != nil {
if err := bot.Client.SendGroupFile(uint32(groupID), file, name, utils.Ternary(folder == "", "/", folder)); err != nil {
log.Warnf("上传群 %v 文件 %v 失败: %v", groupID, file, err)
return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error())
}
Expand All @@ -293,7 +291,7 @@ func (bot *CQBot) CQUploadPrivateFile(userID int64, file, name string) global.MS
log.Warnf("上传群文件 %v 失败: 文件不存在", file)
return Failed(100, "FILE_NOT_FOUND", "文件不存在")
}
if err := bot.Client.UploadPrivateFile(uint32(userID), file, name); err != nil {
if err := bot.Client.SendPrivateFile(uint32(userID), file, name); err != nil {
log.Warnf("上传私聊 %v 文件 %v 失败: %+v", userID, file, err)
return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error())
}
Expand Down Expand Up @@ -618,7 +616,7 @@ func (bot *CQBot) CQSendPrivateMessage(userID int64, groupID int64, m gjson.Resu
func (bot *CQBot) CQSetGroupCard(groupID, userID int64, card string) global.MSG {
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
if m := bot.Client.GetCachedMemberInfo(uint32(userID), uint32(groupID)); m != nil {
if err := bot.Client.GroupRenameMember(uint32(groupID), uint32(userID), card); err != nil {
if err := bot.Client.SetGroupMemberName(uint32(groupID), uint32(userID), card); err != nil {
return Failed(100, "SET_CARD_FAILED", err.Error())

}
Expand All @@ -636,7 +634,7 @@ func (bot *CQBot) CQSetGroupCard(groupID, userID int64, card string) global.MSG
func (bot *CQBot) CQSetGroupSpecialTitle(groupID, userID int64, title string) global.MSG {
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
if m := bot.Client.GetCachedMemberInfo(uint32(userID), uint32(groupID)); m != nil {
if err := bot.Client.GroupSetSpecialTitle(uint32(groupID), uint32(userID), title); err != nil {
if err := bot.Client.SetGroupMemberSpecialTitle(uint32(groupID), uint32(userID), title); err != nil {
return Failed(100, "SET_Title_FAILED", err.Error())
}
return OK(nil)
Expand All @@ -652,7 +650,7 @@ func (bot *CQBot) CQSetGroupSpecialTitle(groupID, userID int64, title string) gl
// @rename(name->group_name)
func (bot *CQBot) CQSetGroupName(groupID int64, name string) global.MSG {
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
if err := bot.Client.GroupRename(uint32(groupID), name); err != nil {
if err := bot.Client.SetGroupName(uint32(groupID), name); err != nil {
return Failed(100, "SET_NAME_FAILED", err.Error())
}
return OK(nil)
Expand Down Expand Up @@ -729,7 +727,7 @@ func (bot *CQBot) CQSetGroupKick(groupID int64, userID int64, block bool) global
if m == nil {
return Failed(100, "MEMBER_NOT_FOUND", "人员不存在")
}
if err := bot.Client.GroupKickMember(uint32(groupID), uint32(userID), block); err != nil {
if err := bot.Client.KickGroupMember(uint32(groupID), uint32(userID), block); err != nil {
return Failed(100, "NOT_MANAGEABLE", "机器人权限不足")
}
return OK(nil)
Expand All @@ -748,7 +746,7 @@ func (bot *CQBot) CQSetGroupBan(groupID, userID int64, duration uint32) global.M
if duration >= 2592000 {
return Failed(100, "DURATION_IS_NOT_IN_RANGE", "非法的禁言时长")
}
if err := bot.Client.GroupMuteMember(uint32(groupID), uint32(userID), duration); err != nil {
if err := bot.Client.SetGroupMemberMute(uint32(groupID), uint32(userID), duration); err != nil {
return Failed(100, "NOT_MANAGEABLE", "机器人权限不足")
}
return OK(nil)
Expand All @@ -764,7 +762,7 @@ func (bot *CQBot) CQSetGroupBan(groupID, userID int64, duration uint32) global.M
// @default(enable=true)
func (bot *CQBot) CQSetGroupWholeBan(groupID int64, enable bool) global.MSG {
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
if err := bot.Client.GroupMuteGlobal(uint32(groupID), enable); err != nil {
if err := bot.Client.SetGroupGlobalMute(uint32(groupID), enable); err != nil {
return Failed(100, "NOT_MANAGEABLE", "机器人权限不足")
}
return OK(nil)
Expand All @@ -778,7 +776,7 @@ func (bot *CQBot) CQSetGroupWholeBan(groupID int64, enable bool) global.MSG {
// @route(set_group_leave)
func (bot *CQBot) CQSetGroupLeave(groupID int64) global.MSG {
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g != nil {
if err := bot.Client.GroupLeave(uint32(groupID)); err != nil {
if err := bot.Client.SetGroupLeave(uint32(groupID)); err != nil {
return Failed(100, "反正是失败了.png", err.Error())
}
return OK(nil)
Expand Down Expand Up @@ -839,7 +837,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
if subType == "add" {
for _, req := range append(msgs.JoinRequests, filteredmsgs.JoinRequests...) {
if strconv.FormatInt(int64(req.Sequence), 10) == flag {
if req.Checked() {
if req.Checked {
log.Warnf("处理群系统消息失败: 无法操作已处理的消息.")
return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
}
Expand All @@ -854,7 +852,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
} else {
for _, req := range append(msgs.InvitedRequests, filteredmsgs.InvitedRequests...) {
if strconv.FormatInt(int64(req.Sequence), 10) == flag {
if req.Checked() {
if req.Checked {
log.Warnf("处理群系统消息失败: 无法操作已处理的消息.")
return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
}
Expand Down Expand Up @@ -918,8 +916,8 @@ func (bot *CQBot) CQSetGroupAdmin(groupID, userID int64, enable bool) global.MSG
if m.Permission != entity.Owner {
return Failed(100, "PERMISSION_DENIED", "或权限不足")
}
if err := bot.Client.GroupSetAdmin(uint32(groupID), uint32(userID), enable); err != nil {
return Failed(100, "反正是失败了.png", err.Error())
if err := bot.Client.SetGroupAdmin(uint32(groupID), uint32(userID), enable); err != nil {
return Failed(100, "UNKNOWN_ERROR", err.Error())
}
return OK(nil)
}
Expand Down Expand Up @@ -1284,56 +1282,53 @@ func (bot *CQBot) CQGetMessage(messageID int32) global.MSG {
return OK(m)
}

// CQGetGroupSystemMessages 扩展API-获取群文件系统消息
// CQGetGroupSystemMessages 扩展API-获取群系统消息
//
// 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
// @route(get_group_system_msg)
//func (bot *CQBot) CQGetGroupSystemMessages() global.MSG {
// msg, err := bot.Client.GetGroupSystemMessages()
// if err != nil {
// log.Warnf("获取群系统消息失败: %v", err)
// return Failed(100, "SYSTEM_MSG_API_ERROR", err.Error())
// }
// return OK(msg)
//}
func (bot *CQBot) CQGetGroupSystemMessages() global.MSG {
msgs, err := bot.Client.GetGroupSystemMessages(false, 20)
if err != nil {
log.Warnf("获取群系统消息失败: %v", err)
return Failed(100, "SYSTEM_MSG_API_ERROR", err.Error())
}
return OK(msgs)
}

// CQGetGroupMessageHistory 获取群消息历史记录
//
// 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
// @route(get_group_msg_history)
// @rename(seq->message_seq)
//func (bot *CQBot) CQGetGroupMessageHistory(groupID int64, seq int64) global.MSG {
// if g, _ := bot.Client.GetCachedGroupInfo(uint32(groupID)); g == nil {
// return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
// }
// if seq == 0 {
// g, err := bot.Client.GetCachedGroupInfo(uint32(groupID))
// if err != nil {
// return Failed(100, "GROUP_INFO_API_ERROR", err.Error())
// }
// seq = g.LastMsgSeq
// }
// msg, err := bot.Client.GetGroupMessages(groupID, int64(math.Max(float64(seq-19), 1)), seq)
// if err != nil {
// log.Warnf("获取群历史消息失败: %v", err)
// return Failed(100, "MESSAGES_API_ERROR", err.Error())
// }
// source := message.Source{
// SourceType: message.SourcePrivate,
// PrimaryID: 0,
// }
// ms := make([]*event, 0, len(msg))
// for _, m := range msg {
// bot.checkMedia(m.Elements, groupID)
// id := bot.InsertGroupMessage(m, source)
// t := bot.formatGroupMessage(m)
// t.Others["message_id"] = id
// ms = append(ms, t)
// }
// return OK(global.MSG{
// "messages": ms,
// })
//}
func (bot *CQBot) CQGetGroupMessageHistory(groupID int64, seq int64) global.MSG {
if g := bot.Client.GetCachedGroupInfo(uint32(groupID)); g == nil {
return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
}
if seq == 0 {
g := bot.Client.GetCachedGroupInfo(uint32(groupID))
seq = int64(g.LastMsgSeq)
}
msg, err := bot.Client.GetGroupMessages(uint32(groupID), uint32(math.Max(float64(seq-19), 1)), uint32(seq))
if err != nil {
log.Warnf("获取群历史消息失败: %v", err)
return Failed(100, "MESSAGES_API_ERROR", err.Error())
}
source := message.Source{
SourceType: message.SourcePrivate,
PrimaryID: 0,
}
ms := make([]*event, 0, len(msg))
for _, m := range msg {
bot.checkMedia(m.Elements, source)
id := bot.InsertGroupMessage(m, source)
t := bot.formatGroupMessage(m)
t.Others["message_id"] = id
ms = append(ms, t)
}
return OK(global.MSG{
"messages": ms,
})
}

// CQGetOnlineClients 扩展API-获取当前账号在线客户端列表
//
Expand Down Expand Up @@ -1537,11 +1532,12 @@ func (bot *CQBot) CQGetEssenceMessageList(groupID int64) global.MSG {
//
// 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
// @route(check_url_safely)
//func (bot *CQBot) CQCheckURLSafely(url string) global.MSG {
// return OK(global.MSG{
// "level": bot.Client.CheckUrlSafely(url),
// })
//}
func (bot *CQBot) CQCheckURLSafely(url string) global.MSG {
level, _ := bot.Client.CheckURLSafely(url)
return OK(global.MSG{
"level": level,
})
}

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

// TODO 计划实现的api
// CQMarkMessageAsRead 标记消息已读
// @route(mark_msg_as_read)
// @rename(msg_id->message_id)
//func (bot *CQBot) CQMarkMessageAsRead(msgID int32) global.MSG {
// m, err := db.GetMessageByGlobalID(msgID)
// if err != nil {
// return Failed(100, "MSG_NOT_FOUND", "消息不存在")
// }
// switch o := m.(type) {
// case *db.StoredGroupMessage:
// bot.Client.MarkGroupMessageReaded(o.GroupCode, int64(o.Attribute.MessageSeq))
// return OK(nil)
// case *db.StoredPrivateMessage:
// bot.Client.MarkPrivateMessageReaded(o.SessionUin, o.Attribute.Timestamp)
// }
// return OK(nil)
//}
func (bot *CQBot) CQMarkMessageAsRead(msgID int32) global.MSG {
m, err := db.GetMessageByGlobalID(msgID)
if err != nil {
return Failed(100, "MSG_NOT_FOUND", "消息不存在")
}
switch o := m.(type) {
case *db.StoredGroupMessage:
if bot.Client.MarkGroupMessageReaded(uint32(o.GroupCode), uint32(o.Attribute.MessageSeq)) != nil {
return Failed(100, "ACTION_FAILED", "标记群消息已读失败")
}
case *db.StoredPrivateMessage:
if bot.Client.MarkPrivateMessageReaded(uint32(o.SessionUin), uint32(o.Attribute.Timestamp), uint32(o.Attribute.MessageSeq)) != nil {
return Failed(100, "ACTION_FAILED", "标记私聊消息已读失败")
}
}
return OK(nil)
}

// CQSetQQProfile 设置 QQ 资料
//
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/FloatTech/sqlite v1.6.3
github.com/LagrangeDev/LagrangeGo v0.1.2-0.20241127060254-2b65455dd776
github.com/LagrangeDev/LagrangeGo v0.1.2
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
Expand Down
Loading

0 comments on commit b2b98cc

Please sign in to comment.