Skip to content

Commit

Permalink
optimize the return value of group-message sending error
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrs4s committed Aug 13, 2022
1 parent 202a75e commit 933bdee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
14 changes: 7 additions & 7 deletions coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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("@全体成员")
Expand All @@ -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 发送私聊消息
Expand Down

0 comments on commit 933bdee

Please sign in to comment.