From 933bdee18e6865f13a9ce7be1f8d9b6f8d4a39a7 Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Sun, 14 Aug 2022 01:40:14 +0800 Subject: [PATCH] optimize the return value of group-message sending error --- coolq/api.go | 6 +++--- coolq/bot.go | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/coolq/api.go b/coolq/api.go index 63307f84c..81f7dedfe 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -765,9 +765,9 @@ func (bot *CQBot) CQSendGroupMessage(groupID int64, m gjson.Result, autoEscape b } } fixAt(elem) - mid := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem}) - if mid == -1 { - return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出") + mid, err := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem}) + if err != nil { + return Failed(100, "SEND_MSG_API_ERROR", err.Error()) } log.Infof("发送群 %v(%v) 的消息: %v (%v)", group.Name, groupID, limitedString(m.String()), mid) return OK(global.MSG{"message_id": mid}) diff --git a/coolq/bot.go b/coolq/bot.go index 17722488a..126de6367 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -248,7 +248,7 @@ func (bot *CQBot) uploadMedia(target message.Source, elements []message.IMessage } // SendGroupMessage 发送群消息 -func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int32 { +func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) (int32, error) { newElem := make([]message.IMessageElement, 0, len(m.Elements)) group := bot.Client.FindGroup(groupID) source := message.Source{ @@ -264,14 +264,14 @@ func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int mem.Poke() } } - return 0 + return 0, nil case *message.MusicShareElement: ret, err := bot.Client.SendGroupMusicShare(groupID, i) if err != nil { log.Warnf("警告: 群 %v 富文本消息发送失败: %v", groupID, err) - return -1 + return -1, errors.Wrap(err, "send group music share error") } - return bot.InsertGroupMessage(ret) + return bot.InsertGroupMessage(ret), nil case *message.AtElement: if i.Target == 0 && group.SelfPermission() == client.Member { e = message.NewText("@全体成员") @@ -281,16 +281,16 @@ func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int } if len(newElem) == 0 { log.Warnf("群消息发送失败: 消息为空.") - return -1 + return -1, errors.New("empty message") } m.Elements = newElem bot.checkMedia(newElem, groupID) ret := bot.Client.SendGroupMessage(groupID, m) if ret == nil || ret.Id == -1 { log.Warnf("群消息发送失败: 账号可能被风控.") - return -1 + return -1, errors.New("send group message failed: blocked by server") } - return bot.InsertGroupMessage(ret) + return bot.InsertGroupMessage(ret), nil } // SendPrivateMessage 发送私聊消息