1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

more error message.

This commit is contained in:
Mrs4s 2020-12-01 21:14:48 +08:00
parent 09eeff3ce6
commit 03518b4823

View File

@ -58,7 +58,7 @@ func (bot *CQBot) CQGetGroupList(noCache bool) MSG {
func (bot *CQBot) CQGetGroupInfo(groupId int64) MSG { func (bot *CQBot) CQGetGroupInfo(groupId int64) MSG {
group := bot.Client.FindGroup(groupId) group := bot.Client.FindGroup(groupId)
if group == nil { if group == nil {
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
return OK(MSG{ return OK(MSG{
"group_id": group.Code, "group_id": group.Code,
@ -72,13 +72,13 @@ func (bot *CQBot) CQGetGroupInfo(groupId int64) MSG {
func (bot *CQBot) CQGetGroupMemberList(groupId int64, noCache bool) MSG { func (bot *CQBot) CQGetGroupMemberList(groupId int64, noCache bool) MSG {
group := bot.Client.FindGroup(groupId) group := bot.Client.FindGroup(groupId)
if group == nil { if group == nil {
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
if noCache { if noCache {
t, err := bot.Client.GetGroupMembers(group) t, err := bot.Client.GetGroupMembers(group)
if err != nil { if err != nil {
log.Warnf("刷新群 %v 成员列表失败: %v", groupId, err) log.Warnf("刷新群 %v 成员列表失败: %v", groupId, err)
return Failed(100) return Failed(100, "GET_MEMBERS_API_ERROR", err.Error())
} }
group.Members = t group.Members = t
} }
@ -93,11 +93,11 @@ func (bot *CQBot) CQGetGroupMemberList(groupId int64, noCache bool) MSG {
func (bot *CQBot) CQGetGroupMemberInfo(groupId, userId int64) MSG { func (bot *CQBot) CQGetGroupMemberInfo(groupId, userId int64) MSG {
group := bot.Client.FindGroup(groupId) group := bot.Client.FindGroup(groupId)
if group == nil { if group == nil {
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
member := group.FindMember(userId) member := group.FindMember(userId)
if member == nil { if member == nil {
return Failed(102) return Failed(100, "MEMBER_NOT_FOUND", "群员不存在")
} }
return OK(convertGroupMemberInfo(groupId, member)) return OK(convertGroupMemberInfo(groupId, member))
} }
@ -106,7 +106,7 @@ func (bot *CQBot) CQGetGroupFileSystemInfo(groupId int64) MSG {
fs, err := bot.Client.GetGroupFileSystem(groupId) fs, err := bot.Client.GetGroupFileSystem(groupId)
if err != nil { if err != nil {
log.Errorf("获取群 %v 文件系统信息失败: %v", groupId, err) log.Errorf("获取群 %v 文件系统信息失败: %v", groupId, err)
return Failed(100) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error())
} }
return OK(fs) return OK(fs)
} }
@ -115,12 +115,12 @@ func (bot *CQBot) CQGetGroupRootFiles(groupId int64) MSG {
fs, err := bot.Client.GetGroupFileSystem(groupId) fs, err := bot.Client.GetGroupFileSystem(groupId)
if err != nil { if err != nil {
log.Errorf("获取群 %v 文件系统信息失败: %v", groupId, err) log.Errorf("获取群 %v 文件系统信息失败: %v", groupId, err)
return Failed(100) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error())
} }
files, folders, err := fs.Root() files, folders, err := fs.Root()
if err != nil { if err != nil {
log.Errorf("获取群 %v 根目录文件失败: %v", groupId, err) log.Errorf("获取群 %v 根目录文件失败: %v", groupId, err)
return Failed(100) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error())
} }
return OK(MSG{ return OK(MSG{
"files": files, "files": files,
@ -132,12 +132,12 @@ func (bot *CQBot) CQGetGroupFilesByFolderId(groupId int64, folderId string) MSG
fs, err := bot.Client.GetGroupFileSystem(groupId) fs, err := bot.Client.GetGroupFileSystem(groupId)
if err != nil { if err != nil {
log.Errorf("获取群 %v 文件系统信息失败: %v", groupId, err) log.Errorf("获取群 %v 文件系统信息失败: %v", groupId, err)
return Failed(100) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error())
} }
files, folders, err := fs.GetFilesByFolder(folderId) files, folders, err := fs.GetFilesByFolder(folderId)
if err != nil { if err != nil {
log.Errorf("获取群 %v 根目录 %v 子文件失败: %v", groupId, folderId, err) log.Errorf("获取群 %v 根目录 %v 子文件失败: %v", groupId, folderId, err)
return Failed(100) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error())
} }
return OK(MSG{ return OK(MSG{
"files": files, "files": files,
@ -148,7 +148,7 @@ func (bot *CQBot) CQGetGroupFilesByFolderId(groupId int64, folderId string) MSG
func (bot *CQBot) CQGetGroupFileUrl(groupId int64, fileId string, busId int32) MSG { func (bot *CQBot) CQGetGroupFileUrl(groupId int64, fileId string, busId int32) MSG {
url := bot.Client.GetGroupFileUrl(groupId, fileId, busId) url := bot.Client.GetGroupFileUrl(groupId, fileId, busId)
if url == "" { if url == "" {
return Failed(100) return Failed(100, "FILE_SYSTEM_API_ERROR")
} }
return OK(MSG{ return OK(MSG{
"url": url, "url": url,
@ -158,7 +158,7 @@ func (bot *CQBot) CQGetGroupFileUrl(groupId int64, fileId string, busId int32) M
func (bot *CQBot) CQGetWordSlices(content string) MSG { func (bot *CQBot) CQGetWordSlices(content string) MSG {
slices, err := bot.Client.GetWordSegmentation(content) slices, err := bot.Client.GetWordSegmentation(content)
if err != nil { if err != nil {
return Failed(100) return Failed(100, "WORD_SEGMENTATION_API_ERROR", err.Error())
} }
for i := 0; i < len(slices); i++ { for i := 0; i < len(slices); i++ {
slices[i] = strings.ReplaceAll(slices[i], "\u0000", "") slices[i] = strings.ReplaceAll(slices[i], "\u0000", "")
@ -188,7 +188,7 @@ func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}, autoEscape bo
fixAt(elem) fixAt(elem)
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 { if mid == -1 {
return Failed(100) return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
} }
log.Infof("发送群 %v(%v) 的消息: %v (%v)", groupId, groupId, limitedString(m.String()), mid) log.Infof("发送群 %v(%v) 的消息: %v (%v)", groupId, groupId, limitedString(m.String()), mid)
return OK(MSG{"message_id": mid}) return OK(MSG{"message_id": mid})
@ -204,7 +204,7 @@ func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}, autoEscape bo
} }
if str == "" { if str == "" {
log.Warnf("群消息发送失败: 信息为空. MSG: %v", i) log.Warnf("群消息发送失败: 信息为空. MSG: %v", i)
return Failed(100) return Failed(100, "EMPTY_MSG_ERROR", "消息为空")
} }
var elem []message.IMessageElement var elem []message.IMessageElement
if autoEscape { if autoEscape {
@ -215,7 +215,7 @@ func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}, autoEscape bo
fixAt(elem) fixAt(elem)
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 { if mid == -1 {
return Failed(100) return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
} }
log.Infof("发送群 %v(%v) 的消息: %v (%v)", groupId, groupId, limitedString(str), mid) log.Infof("发送群 %v(%v) 的消息: %v (%v)", groupId, groupId, limitedString(str), mid)
return OK(MSG{"message_id": mid}) return OK(MSG{"message_id": mid})
@ -312,7 +312,7 @@ func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}, autoEscape b
elem := bot.ConvertObjectMessage(m, true) elem := bot.ConvertObjectMessage(m, true)
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
if mid == -1 { if mid == -1 {
return Failed(100) return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
} }
log.Infof("发送好友 %v(%v) 的消息: %v (%v)", userId, userId, limitedString(m.String()), mid) log.Infof("发送好友 %v(%v) 的消息: %v (%v)", userId, userId, limitedString(m.String()), mid)
return OK(MSG{"message_id": mid}) return OK(MSG{"message_id": mid})
@ -327,7 +327,7 @@ func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}, autoEscape b
str = s str = s
} }
if str == "" { if str == "" {
return Failed(100) return Failed(100, "EMPTY_MSG_ERROR", "消息为空")
} }
var elem []message.IMessageElement var elem []message.IMessageElement
if autoEscape { if autoEscape {
@ -337,7 +337,7 @@ func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}, autoEscape b
} }
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
if mid == -1 { if mid == -1 {
return Failed(100) return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
} }
log.Infof("发送好友 %v(%v) 的消息: %v (%v)", userId, userId, limitedString(str), mid) log.Infof("发送好友 %v(%v) 的消息: %v (%v)", userId, userId, limitedString(str), mid)
return OK(MSG{"message_id": mid}) return OK(MSG{"message_id": mid})
@ -351,7 +351,7 @@ func (bot *CQBot) CQSetGroupCard(groupId, userId int64, card string) MSG {
return OK(nil) return OK(nil)
} }
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_special_title-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E7%BB%84%E4%B8%93%E5%B1%9E%E5%A4%B4%E8%A1%94 // https://cqhttp.cc/docs/4.15/#/API?id=set_group_special_title-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E7%BB%84%E4%B8%93%E5%B1%9E%E5%A4%B4%E8%A1%94
@ -362,7 +362,7 @@ func (bot *CQBot) CQSetGroupSpecialTitle(groupId, userId int64, title string) MS
return OK(nil) return OK(nil)
} }
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
func (bot *CQBot) CQSetGroupName(groupId int64, name string) MSG { func (bot *CQBot) CQSetGroupName(groupId int64, name string) MSG {
@ -370,7 +370,7 @@ func (bot *CQBot) CQSetGroupName(groupId int64, name string) MSG {
g.UpdateName(name) g.UpdateName(name)
return OK(nil) return OK(nil)
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
func (bot *CQBot) CQSetGroupMemo(groupId int64, msg string) MSG { func (bot *CQBot) CQSetGroupMemo(groupId int64, msg string) MSG {
@ -378,7 +378,7 @@ func (bot *CQBot) CQSetGroupMemo(groupId int64, msg string) MSG {
g.UpdateMemo(msg) g.UpdateMemo(msg)
return OK(nil) return OK(nil)
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_kick-%E7%BE%A4%E7%BB%84%E8%B8%A2%E4%BA%BA // https://cqhttp.cc/docs/4.15/#/API?id=set_group_kick-%E7%BE%A4%E7%BB%84%E8%B8%A2%E4%BA%BA
@ -389,7 +389,7 @@ func (bot *CQBot) CQSetGroupKick(groupId, userId int64, msg string, block bool)
return OK(nil) return OK(nil)
} }
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_ban-%E7%BE%A4%E7%BB%84%E5%8D%95%E4%BA%BA%E7%A6%81%E8%A8%80 // https://cqhttp.cc/docs/4.15/#/API?id=set_group_ban-%E7%BE%A4%E7%BB%84%E5%8D%95%E4%BA%BA%E7%A6%81%E8%A8%80
@ -400,7 +400,7 @@ func (bot *CQBot) CQSetGroupBan(groupId, userId int64, duration uint32) MSG {
return OK(nil) return OK(nil)
} }
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_whole_ban-%E7%BE%A4%E7%BB%84%E5%85%A8%E5%91%98%E7%A6%81%E8%A8%80 // https://cqhttp.cc/docs/4.15/#/API?id=set_group_whole_ban-%E7%BE%A4%E7%BB%84%E5%85%A8%E5%91%98%E7%A6%81%E8%A8%80
@ -409,7 +409,7 @@ func (bot *CQBot) CQSetGroupWholeBan(groupId int64, enable bool) MSG {
g.MuteAll(enable) g.MuteAll(enable)
return OK(nil) return OK(nil)
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_leave-%E9%80%80%E5%87%BA%E7%BE%A4%E7%BB%84 // https://cqhttp.cc/docs/4.15/#/API?id=set_group_leave-%E9%80%80%E5%87%BA%E7%BE%A4%E7%BB%84
@ -418,14 +418,14 @@ func (bot *CQBot) CQSetGroupLeave(groupId int64) MSG {
g.Quit() g.Quit()
return OK(nil) return OK(nil)
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
// https://cqhttp.cc/docs/4.15/#/API?id=set_friend_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E5%A5%BD%E5%8F%8B%E8%AF%B7%E6%B1%82 // https://cqhttp.cc/docs/4.15/#/API?id=set_friend_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E5%A5%BD%E5%8F%8B%E8%AF%B7%E6%B1%82
func (bot *CQBot) CQProcessFriendRequest(flag string, approve bool) MSG { func (bot *CQBot) CQProcessFriendRequest(flag string, approve bool) MSG {
req, ok := bot.friendReqCache.Load(flag) req, ok := bot.friendReqCache.Load(flag)
if !ok { if !ok {
return Failed(100) return Failed(100, "FLAG_NOT_FOUND", "FLAG不存在")
} }
if approve { if approve {
req.(*client.NewFriendRequest).Accept() req.(*client.NewFriendRequest).Accept()
@ -440,14 +440,14 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
msgs, err := bot.Client.GetGroupSystemMessages() msgs, err := bot.Client.GetGroupSystemMessages()
if err != nil { if err != nil {
log.Errorf("获取群系统消息失败: %v", err) log.Errorf("获取群系统消息失败: %v", err)
return Failed(100) return Failed(100, "SYSTEM_MSG_API_ERROR", err.Error())
} }
if subType == "add" { if subType == "add" {
for _, req := range msgs.JoinRequests { for _, req := range msgs.JoinRequests {
if strconv.FormatInt(req.RequestId, 10) == flag { if strconv.FormatInt(req.RequestId, 10) == flag {
if req.Checked { if req.Checked {
log.Errorf("处理群系统消息失败: 无法操作已处理的消息.") log.Errorf("处理群系统消息失败: 无法操作已处理的消息.")
return Failed(100) return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
} }
if approve { if approve {
req.Accept() req.Accept()
@ -462,7 +462,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
if strconv.FormatInt(req.RequestId, 10) == flag { if strconv.FormatInt(req.RequestId, 10) == flag {
if req.Checked { if req.Checked {
log.Errorf("处理群系统消息失败: 无法操作已处理的消息.") log.Errorf("处理群系统消息失败: 无法操作已处理的消息.")
return Failed(100) return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
} }
if approve { if approve {
req.Accept() req.Accept()
@ -474,7 +474,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
} }
} }
log.Errorf("处理群系统消息失败: 消息 %v 不存在.", flag) log.Errorf("处理群系统消息失败: 消息 %v 不存在.", flag)
return Failed(100) return Failed(100, "FLAG_NOT_FOUND", "FLAG不存在")
} }
// https://cqhttp.cc/docs/4.15/#/API?id=delete_msg-%E6%92%A4%E5%9B%9E%E6%B6%88%E6%81%AF // https://cqhttp.cc/docs/4.15/#/API?id=delete_msg-%E6%92%A4%E5%9B%9E%E6%B6%88%E6%81%AF
@ -486,16 +486,16 @@ func (bot *CQBot) CQDeleteMessage(messageId int32) MSG {
if _, ok := msg["group"]; ok { if _, ok := msg["group"]; ok {
if err := bot.Client.RecallGroupMessage(msg["group"].(int64), msg["message-id"].(int32), msg["internal-id"].(int32)); err != nil { if err := bot.Client.RecallGroupMessage(msg["group"].(int64), msg["message-id"].(int32), msg["internal-id"].(int32)); err != nil {
log.Warnf("撤回 %v 失败: %v", messageId, err) log.Warnf("撤回 %v 失败: %v", messageId, err)
return Failed(100) return Failed(100, "RECALL_API_ERROR", err.Error())
} }
} else { } else {
if msg["sender"].(message.Sender).Uin != bot.Client.Uin { if msg["sender"].(message.Sender).Uin != bot.Client.Uin {
log.Warnf("撤回 %v 失败: 好友会话无法撤回对方消息.") log.Warnf("撤回 %v 失败: 好友会话无法撤回对方消息.")
return Failed(100) return Failed(100, "CANNOT_RECALL_FRIEND_MSG", "无法撤回对方消息")
} }
if err := bot.Client.RecallPrivateMessage(msg["target"].(int64), int64(msg["time"].(int32)), msg["message-id"].(int32), msg["internal-id"].(int32)); err != nil { if err := bot.Client.RecallPrivateMessage(msg["target"].(int64), int64(msg["time"].(int32)), msg["message-id"].(int32), msg["internal-id"].(int32)); err != nil {
log.Warnf("撤回 %v 失败: %v", messageId, err) log.Warnf("撤回 %v 失败: %v", messageId, err)
return Failed(100) return Failed(100, "RECALL_API_ERROR", err.Error())
} }
} }
return OK(nil) return OK(nil)
@ -505,17 +505,17 @@ func (bot *CQBot) CQDeleteMessage(messageId int32) MSG {
func (bot *CQBot) CQSetGroupAdmin(groupId, userId int64, enable bool) MSG { func (bot *CQBot) CQSetGroupAdmin(groupId, userId int64, enable bool) MSG {
group := bot.Client.FindGroup(groupId) group := bot.Client.FindGroup(groupId)
if group == nil || group.OwnerUin != bot.Client.Uin { if group == nil || group.OwnerUin != bot.Client.Uin {
return Failed(100) return Failed(100, "PERMISSION_DENIED", "群不存在或权限不足")
} }
mem := group.FindMember(userId) mem := group.FindMember(userId)
if mem == nil { if mem == nil {
return Failed(100) return Failed(100, "GROUP_MEMBER_NOT_FOUND", "群成员不存在")
} }
mem.SetAdmin(enable) mem.SetAdmin(enable)
t, err := bot.Client.GetGroupMembers(group) t, err := bot.Client.GetGroupMembers(group)
if err != nil { if err != nil {
log.Warnf("刷新群 %v 成员列表失败: %v", groupId, err) log.Warnf("刷新群 %v 成员列表失败: %v", groupId, err)
return Failed(100) return Failed(100, "GET_MEMBERS_API_ERROR", err.Error())
} }
group.Members = t group.Members = t
return OK(nil) return OK(nil)
@ -524,7 +524,7 @@ func (bot *CQBot) CQSetGroupAdmin(groupId, userId int64, enable bool) MSG {
func (bot *CQBot) CQGetVipInfo(userId int64) MSG { func (bot *CQBot) CQGetVipInfo(userId int64) MSG {
vip, err := bot.Client.GetVipInfo(userId) vip, err := bot.Client.GetVipInfo(userId)
if err != nil { if err != nil {
return Failed(100) return Failed(100, "VIP_API_ERROR", err.Error())
} }
msg := MSG{ msg := MSG{
"user_id": vip.Uin, "user_id": vip.Uin,
@ -597,7 +597,7 @@ func (bot *CQBot) CQGetGroupHonorInfo(groupId int64, t string) MSG {
func (bot *CQBot) CQGetStrangerInfo(userId int64) MSG { func (bot *CQBot) CQGetStrangerInfo(userId int64) MSG {
info, err := bot.Client.GetSummaryInfo(userId) info, err := bot.Client.GetSummaryInfo(userId)
if err != nil { if err != nil {
return Failed(100) return Failed(100, "SUMMARY_API_ERROR", err.Error())
} }
return OK(MSG{ return OK(MSG{
"user_id": info.Uin, "user_id": info.Uin,
@ -692,14 +692,15 @@ func (bot *CQBot) CQGetImage(file string) MSG {
} }
msg["file"] = local msg["file"] = local
return OK(msg) return OK(msg)
} else {
return Failed(100, "LOAD_FILE_ERROR", err.Error())
} }
return Failed(100)
} }
func (bot *CQBot) CQGetForwardMessage(resId string) MSG { func (bot *CQBot) CQGetForwardMessage(resId string) MSG {
m := bot.Client.GetForwardMessage(resId) m := bot.Client.GetForwardMessage(resId)
if m == nil { if m == nil {
return Failed(100) return Failed(100, "MSG_NOT_FOUND", "消息不存在")
} }
r := make([]MSG, 0) r := make([]MSG, 0)
for _, n := range m.Nodes { for _, n := range m.Nodes {
@ -721,7 +722,7 @@ func (bot *CQBot) CQGetForwardMessage(resId string) MSG {
func (bot *CQBot) CQGetMessage(messageId int32) MSG { func (bot *CQBot) CQGetMessage(messageId int32) MSG {
msg := bot.GetMessage(messageId) msg := bot.GetMessage(messageId)
if msg == nil { if msg == nil {
return Failed(100) return Failed(100, "MSG_NOT_FOUND", "消息不存在")
} }
sender := msg["sender"].(message.Sender) sender := msg["sender"].(message.Sender)
gid, isGroup := msg["group"] gid, isGroup := msg["group"]
@ -749,7 +750,7 @@ func (bot *CQBot) CQGetGroupSystemMessages() MSG {
msg, err := bot.Client.GetGroupSystemMessages() msg, err := bot.Client.GetGroupSystemMessages()
if err != nil { if err != nil {
log.Warnf("获取群系统消息失败: %v", err) log.Warnf("获取群系统消息失败: %v", err)
return Failed(100) return Failed(100, "SYSTEM_MSG_API_ERROR", err.Error())
} }
return OK(msg) return OK(msg)
} }
@ -766,12 +767,12 @@ func (bot *CQBot) CQOcrImage(imageId string) MSG {
img, err := bot.makeImageElem(map[string]string{"file": imageId}, true) img, err := bot.makeImageElem(map[string]string{"file": imageId}, true)
if err != nil { if err != nil {
log.Warnf("load image error: %v", err) log.Warnf("load image error: %v", err)
return Failed(100) return Failed(100, "LOAD_FILE_ERROR", err.Error())
} }
rsp, err := bot.Client.ImageOcr(img) rsp, err := bot.Client.ImageOcr(img)
if err != nil { if err != nil {
log.Warnf("ocr image error: %v", err) log.Warnf("ocr image error: %v", err)
return Failed(100) return Failed(100, "OCR_API_ERROR", err.Error())
} }
return OK(rsp) return OK(rsp)
} }
@ -786,12 +787,12 @@ func (bot *CQBot) CQSetGroupPortrait(groupId int64, file, cache string) MSG {
img, err := global.FindFile(file, cache, global.IMAGE_PATH) img, err := global.FindFile(file, cache, global.IMAGE_PATH)
if err != nil { if err != nil {
log.Warnf("set group portrait error: %v", err) log.Warnf("set group portrait error: %v", err)
return Failed(100) return Failed(100, "LOAD_FILE_ERROR", err.Error())
} }
g.UpdateGroupHeadPortrait(img) g.UpdateGroupHeadPortrait(img)
return OK(nil) return OK(nil)
} }
return Failed(100) return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
} }
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_status-%E8%8E%B7%E5%8F%96%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81 // https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_status-%E8%8E%B7%E5%8F%96%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81
@ -827,6 +828,8 @@ func (bot *CQBot) CQGetVersionInfo() MSG {
return 1 return 1
case client.AndroidWatch: case client.AndroidWatch:
return 2 return 2
case client.MacOS:
return 3
default: default:
return -1 return -1
} }
@ -838,8 +841,16 @@ func OK(data interface{}) MSG {
return MSG{"data": data, "retcode": 0, "status": "ok"} return MSG{"data": data, "retcode": 0, "status": "ok"}
} }
func Failed(code int) MSG { func Failed(code int, msg ...string) MSG {
return MSG{"data": nil, "retcode": code, "status": "failed"} m := ""
w := ""
if len(msg) > 0 {
m = msg[0]
}
if len(msg) > 1 {
w = msg[1]
}
return MSG{"data": nil, "retcode": code, "msg": m, "wording": w, "status": "failed"}
} }
func convertGroupMemberInfo(groupId int64, m *client.GroupMemberInfo) MSG { func convertGroupMemberInfo(groupId int64, m *client.GroupMemberInfo) MSG {