From 8974adb0098042518a537e1e2fda0ffbfc0e42e3 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 7 Apr 2021 18:48:56 +0800 Subject: [PATCH] fix: panic on empty forward msg (cherry picked from commit c06aee39d9380269c69b566466bb2b66d10df8b5) --- coolq/cqcode.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 980f75d..cbf60be 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -405,9 +405,15 @@ func (bot *CQBot) ConvertStringMessage(s string, isGroup bool) (r []message.IMes } if t == "forward" { // 单独处理转发 if id, ok := d["id"]; ok { - r = []message.IMessageElement{bot.Client.DownloadForwardMessage(id)} - return + if fwdMsg := bot.Client.DownloadForwardMessage(id); fwdMsg == nil { + log.Warnf("警告: Forward 信息不存在或已过期") + } else { + r = []message.IMessageElement{fwdMsg} + } + } else { + log.Warnf("警告: Forward 元素中必须包含 id") } + return } elem, err := bot.ToElement(t, d, isGroup) if err != nil { @@ -557,7 +563,16 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, isGroup bool) (r []messag } } if t == "forward" { - r = []message.IMessageElement{bot.Client.DownloadForwardMessage(e.Get("data.id").String())} + id := e.Get("data.id").String() + if id == "" { + log.Warnf("警告: Forward 元素中必须包含 id") + } else { + if fwdMsg := bot.Client.DownloadForwardMessage(id); fwdMsg == nil { + log.Warnf("警告: Forward 信息不存在或已过期") + } else { + r = []message.IMessageElement{fwdMsg} + } + } return } d := make(map[string]string)