feat: 在 MsgInHandler 中添加 Panic 恢复日志记录,并在出现 Panic 时发送警告消息

This commit is contained in:
lixiangwuxian 2025-04-10 01:05:44 +08:00
parent 49be7aafb9
commit 546f1abcd1
2 changed files with 15 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package action
import (
"encoding/json"
"fmt"
"log"
"strconv"
"sync"
"time"
@ -62,6 +63,7 @@ func (am *actionManager) SendMsg(reply *model.Reply) error {
for !am.Started() {
time.Sleep(100 * time.Millisecond)
}
log.Default().Printf("\033[32m↑\033[0m:%s", string(sendPkgJson))
if err = am.botConn.WriteMessage(websocket.TextMessage, sendPkgJson); err != nil {
return err
}
@ -84,6 +86,7 @@ func (am *actionManager) SendForward(reply *model.Reply) error {
if err != nil {
return err
}
log.Default().Printf("\033[32m↑\033[0m:%s", string(sendPkgJson))
if err = am.botConn.WriteMessage(websocket.TextMessage, sendPkgJson); err != nil {
return err
}

View File

@ -8,7 +8,9 @@ import (
"strings"
"time"
"git.lxtend.com/qqbot/action"
"git.lxtend.com/qqbot/auth"
"git.lxtend.com/qqbot/config"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/model"
)
@ -95,6 +97,15 @@ func MsgInHandler(msg model.Message) (reply *model.Reply) {
errMsg := fmt.Sprintf("[%s] Panic recovered:\nError: %v\nStack Trace:\n%s\n",
time.Now().Format("2006-01-02 15:04:05"), r, stack)
log.Print(errMsg)
action.ActionManager.SendMsg(&model.Reply{
ReplyMsg: "出现了Panic:\n" + errMsg,
ReferOriginMsg: false,
FromMsg: model.Message{
GroupInfo: model.GroupInfo{
GroupId: config.ConfigManager.GetConfig().Management.ReportGroup,
},
},
})
}
}()
if msg.RawMsg != "" {
@ -104,7 +115,7 @@ func MsgInHandler(msg model.Message) (reply *model.Reply) {
}
defer func() {
if reply != nil {
log.Default().Printf("\033[32m↑\033[0m:%v", reply)
// log.Default().Printf("\033[32m↑\033[0m:%v", reply)
}
}()
if handlerUserID, ok := liveHandlers[msg.GroupInfo.GroupId]; ok {