diff --git a/Processor/ProcessC2CMessage.go b/Processor/ProcessC2CMessage.go index cbf40c07..0bc4854b 100644 --- a/Processor/ProcessC2CMessage.go +++ b/Processor/ProcessC2CMessage.go @@ -30,6 +30,7 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error { if data.Author.ID == "" { mylog.Printf("出现ID为空未知错误.%v\n", data) + return nil } //获取当前的s值 当前ws连接所收到的信息条数 diff --git a/Processor/ProcessGroupMessage.go b/Processor/ProcessGroupMessage.go index a943eb60..127ce343 100644 --- a/Processor/ProcessGroupMessage.go +++ b/Processor/ProcessGroupMessage.go @@ -37,6 +37,7 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error { if data.Author.ID == "" { mylog.Printf("出现ID为空未知错误.%v\n", data) + return nil } if !config.GetStringOb11() { diff --git a/botgo/sessions/multi/multi.go b/botgo/sessions/multi/multi.go index 3409b0cc..7e6db5d3 100644 --- a/botgo/sessions/multi/multi.go +++ b/botgo/sessions/multi/multi.go @@ -1,6 +1,7 @@ package multi import ( + "fmt" "sync" "time" @@ -93,6 +94,19 @@ func (sm *ShardManager) newConnect(session dto.Session, shardID uint32) { } if err := wsClient.Listening(); err != nil { log.Errorf("[ws/session] Listening error: %+v", err) - sm.SessionChans[shardID] <- session // Reconnect + currentSession := wsClient.Session() + // 对于不能够进行重连的session,需要清空 session id 与 seq + if manager.CanNotResume(err) { + currentSession.ID = "" + currentSession.LastSeq = 0 + } + // 一些错误不能够鉴权,比如机器人被封禁,这里就直接退出了 + if manager.CanNotIdentify(err) { + msg := fmt.Sprintf("can not identify because server return %+v, so process exit", err) + log.Errorf(msg) + panic(msg) // 当机器人被下架,或者封禁,将不能再连接,所以 panic + } + // 将 session 放到 session chan 中,用于启动新的连接,当前连接退出 + sm.SessionChans[shardID] <- *currentSession // Reconnect } } diff --git a/handlers/send_private_msg_sse.go b/handlers/send_private_msg_sse.go index 44cbe02c..2a7dacd3 100644 --- a/handlers/send_private_msg_sse.go +++ b/handlers/send_private_msg_sse.go @@ -109,6 +109,7 @@ func HandleSendPrivateMsgSSE(client callapi.Client, api openapi.OpenAPI, apiv2 o // 输出反序列化后的对象,确认是否成功转换 fmt.Printf("Recovered InterfaceBody: %+v\n", messageBody) + // 使用 echo 获取消息ID var messageID string if config.GetLazyMessageId() { @@ -135,7 +136,9 @@ func HandleSendPrivateMsgSSE(client callapi.Client, api openapi.OpenAPI, apiv2 o // 获取并打印相关ID relatedID := GetRelatedID(messageID) - fmt.Println("相关ID:", relatedID) + + //fmt.Println("相关ID:", relatedID) + dtoSSE := generateMessageSSE(messageBody, messageID, relatedID) mylog.Printf("私聊发信息sse:%v", dtoSSE) diff --git a/main.go b/main.go index dfa86835..911e2395 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,8 @@ import ( "syscall" "time" + // _ "net/http/pprof" + "github.com/fatih/color" "github.com/fsnotify/fsnotify" "github.com/hoshinonyaruko/gensokyo/Processor" @@ -125,6 +127,7 @@ func main() { //logger logLevel := mylog.GetLogLevelFromConfig(config.GetLogLevel()) loggerAdapter := mylog.NewMyLogAdapter(logLevel, config.GetSaveLogs()) + mylog.SetLogLevel(logLevel) botgo.SetLogger(loggerAdapter) if *m { @@ -577,6 +580,14 @@ func main() { }() } + // // 启动一个用于 pprof 的 HTTP 服务器 + // go func() { + // log.Println("pprof server running on :6060") + // if err := http.ListenAndServe("localhost:6060", nil); err != nil { + // log.Fatalf("pprof server failed: %s", err) + // } + // }() + //杂七杂八的地方 if conf.Settings.MemoryMsgid { echo.StartCleanupRoutine() diff --git a/mylog/mylog.go b/mylog/mylog.go index 44ff9b65..bc8c812f 100644 --- a/mylog/mylog.go +++ b/mylog/mylog.go @@ -23,6 +23,8 @@ const ( LogLevelError ) +var currentLevel = LogLevelInfo // 默认日志级别为 INFO + type Client struct { conn *websocket.Conn send chan EnhancedLogEntry @@ -249,32 +251,56 @@ type EnhancedLogEntry struct { // 日志频道,所有的 WebSocket 客户端都会在此监听日志事件 var logChannel = make(chan EnhancedLogEntry, 1000) +func SetLogLevel(level LogLevel) { + if level >= LogLevelDebug && level <= LogLevelError { + currentLevel = level + } else { + log.Printf("Invalid log level: %d", level) + } +} + func Println(v ...interface{}) { - log.Println(v...) - message := fmt.Sprint(v...) - emitLog("INFO", message) - LogToFile("INFO", message) + if currentLevel <= LogLevelInfo { + log.Println(v...) + message := fmt.Sprint(v...) + emitLog("INFO", message) + LogToFile("INFO", message) + } } func Printf(format string, v ...interface{}) { - log.Printf(format, v...) - message := fmt.Sprintf(format, v...) - emitLog("INFO", message) - LogToFile("INFO", message) + if currentLevel <= LogLevelInfo { + log.Printf(format, v...) + message := fmt.Sprintf(format, v...) + emitLog("INFO", message) + LogToFile("INFO", message) + } +} + +func Warnf(format string, v ...interface{}) { + if currentLevel <= LogLevelWarn { + log.Printf(format, v...) + message := fmt.Sprintf(format, v...) + emitLog("WARN", message) + LogToFile("WARN", message) + } } func Errorf(format string, v ...interface{}) { - log.Printf(format, v...) - message := fmt.Sprintf(format, v...) - emitLog("ERROR", message) - LogToFile("ERROR", message) + if currentLevel <= LogLevelError { + log.Printf(format, v...) + message := fmt.Sprintf(format, v...) + emitLog("ERROR", message) + LogToFile("ERROR", message) + } } func Fatalf(format string, v ...interface{}) { log.Printf(format, v...) message := fmt.Sprintf(format, v...) - emitLog("Fatal", message) - LogToFile("Fatal", message) + emitLog("FATAL", message) + LogToFile("FATAL", message) + os.Exit(1) // Fatal logs usually terminate the program } func emitLog(level, message string) {