Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
优化日志
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed May 11, 2024
1 parent 3759ea0 commit 0558d81
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
92 changes: 92 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package miraihttp
import (
"encoding/json"
"errors"
"fmt"
"github.com/tidwall/gjson"
"log/slog"
"strconv"
)

type MessageChain []SingleMessage
Expand Down Expand Up @@ -44,6 +46,10 @@ func (m *Source) FillMessageType() {
m.Type = "Source"
}

func (m *Source) String() string {
return ""
}

// Quote 引用回复
type Quote struct {
Type string `json:"type"`
Expand All @@ -58,6 +64,10 @@ func (m *Quote) FillMessageType() {
m.Type = "Quote"
}

func (m *Quote) String() string {
return "[引用]"

Check failure on line 68 in message.go

View workflow job for this annotation

GitHub Actions / build

string literal contains rune in Han script (gosmopolitan)
}

// At @消息
type At struct {
Type string `json:"type"`
Expand All @@ -69,6 +79,10 @@ func (m *At) FillMessageType() {
m.Type = "At"
}

func (m *At) String() string {
return "@" + strconv.FormatInt(m.Target, 10)
}

// AtAll @全体消息
type AtAll struct {
Type string `json:"type"`
Expand All @@ -78,6 +92,10 @@ func (m *AtAll) FillMessageType() {
m.Type = "AtAll"
}

func (m *AtAll) String() string {
return "@全体成员"

Check failure on line 96 in message.go

View workflow job for this annotation

GitHub Actions / build

string literal contains rune in Han script (gosmopolitan)
}

// Face QQ表情
type Face struct {
Type string `json:"type"`
Expand All @@ -89,6 +107,17 @@ func (m *Face) FillMessageType() {
m.Type = "Face"
}

func (m *Face) String() string {
switch {
case m.Name != "":
return "[" + m.Name + "]"
case m.FaceId != 0:
return fmt.Sprintf("[表情:%d]", m.FaceId)

Check failure on line 115 in message.go

View workflow job for this annotation

GitHub Actions / build

string literal contains rune in Han script (gosmopolitan)
default:
return "[表情]"
}
}

// Plain 文字消息
type Plain struct {
Type string `json:"type"`
Expand All @@ -99,6 +128,10 @@ func (m *Plain) FillMessageType() {
m.Type = "Plain"
}

func (m *Plain) String() string {
return m.Text
}

// Image 图片(参数优先级imageId > url > path > base64)
type Image struct {
Type string `json:"type"`
Expand All @@ -112,6 +145,10 @@ func (m *Image) FillMessageType() {
m.Type = "Image"
}

func (m *Image) String() string {
return "[图片]"
}

// FlashImage 闪照,参数同Image
type FlashImage struct {
Type string `json:"type"`
Expand All @@ -125,6 +162,10 @@ func (m *FlashImage) FillMessageType() {
m.Type = "FlashImage"
}

func (m *FlashImage) String() string {
return "[闪照]"
}

// Voice 语音(参数优先级imageId > url > path > base64)
type Voice struct {
Type string `json:"type"`
Expand All @@ -139,6 +180,10 @@ func (m *Voice) FillMessageType() {
m.Type = "Voice"
}

func (m *Voice) String() string {
return "[语音消息]"
}

type Xml struct {
Type string `json:"type"`
Xml string `json:"xml"` // XML文本
Expand All @@ -148,6 +193,10 @@ func (m *Xml) FillMessageType() {
m.Type = "Xml"
}

func (m *Xml) String() string {
return m.Xml
}

type Json struct {
Type string `json:"type"`
Json string `json:"json"` // Json文本
Expand All @@ -157,6 +206,10 @@ func (m *Json) FillMessageType() {
m.Type = "Json"
}

func (m *Json) String() string {
return m.Json
}

type App struct {
Type string `json:"type"`
Content string `json:"content"` // 内容
Expand All @@ -166,6 +219,10 @@ func (m *App) FillMessageType() {
m.Type = "App"
}

func (m *App) String() string {
return m.Content
}

// PokeName 戳一戳的类型
type PokeName string

Expand All @@ -188,6 +245,10 @@ func (m *Poke) FillMessageType() {
m.Type = "Poke"
}

func (m *Poke) String() string {
return "[戳一戳]"
}

// Dice 骰子
type Dice struct {
Type string `json:"type"`
Expand All @@ -198,6 +259,10 @@ func (m *Dice) FillMessageType() {
m.Type = "Dice"
}

func (m *Dice) String() string {
return fmt.Sprintf("[骰子:%d]", m.Value)
}

// MarketFace 商城表情(目前商城表情仅支持接收和转发,不支持构造发送)
type MarketFace struct {
Type string `json:"type"`
Expand All @@ -209,6 +274,17 @@ func (m *MarketFace) FillMessageType() {
m.Type = "MarketFace"
}

func (m *MarketFace) String() string {
switch {
case m.Name != "":
return "[" + m.Name + "]"
case m.Id != 0:
return fmt.Sprintf("[商城表情:%d]", m.Id)
default:
return "[商城表情]"
}
}

// MusicShare 音乐分享
type MusicShare struct {
Type string `json:"type"`
Expand All @@ -225,6 +301,10 @@ func (m *MusicShare) FillMessageType() {
m.Type = "MusicShare"
}

func (m *MusicShare) String() string {
return "[分享]" + m.Title
}

type ForwardMessageNode struct {
SenderId int64 `json:"senderId,omitempty"` // 消息节点
Time int64 `json:"time,omitempty"` // 发送时间
Expand Down Expand Up @@ -257,6 +337,10 @@ func (m *ForwardMessage) FillMessageType() {
m.Type = "ForwardMessage"
}

func (m *ForwardMessage) String() string {
return "[转发消息]"
}

// File 文件
type File struct {
Type string `json:"type"`
Expand All @@ -269,6 +353,10 @@ func (m *File) FillMessageType() {
m.Type = "File"
}

func (m *File) String() string {
return "[文件]" + m.Name
}

type MiraiCode struct {
Type string `json:"type"`
Code string `json:"code"`
Expand All @@ -278,6 +366,10 @@ func (m *MiraiCode) FillMessageType() {
m.Type = "MiraiCode"
}

func (m *MiraiCode) String() string {
return m.Code
}

var singleMessageBuilder = map[string]func() SingleMessage{
"Source": func() SingleMessage { return &Source{} },
"Quote": func() SingleMessage { return &Quote{} },
Expand Down
7 changes: 4 additions & 3 deletions miraihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func Connect(host string, port int, channel WsChannel, verifyKey string, qq int6
log.Error("read error", "error", err)
return
}
log.Debug("recv: " + string(message))
if !gjson.ValidBytes(message) {
log.Error("invalid json message: " + string(message))
continue
Expand All @@ -74,6 +73,7 @@ func Connect(host string, port int, channel WsChannel, verifyKey string, qq int6
continue
}
if len(syncId) > 0 && syncId[0] != '-' {
log.Debug("recv", "data", data, "syncId", syncId)
if ch, ok := b.syncIdMap.LoadAndDelete(syncId); ok {
ch0 := ch.(chan gjson.Result)
ch0 <- data
Expand All @@ -88,6 +88,7 @@ func Connect(host string, port int, channel WsChannel, verifyKey string, qq int6
if p := decoder[messageType]; p == nil {
log.Error("cannot find message decoder: " + messageType)
} else if m := p(data); m != nil {
log.Debug("recv", "content", m)
fun := func() {
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -143,10 +144,10 @@ func (b *Bot) request(command, subCommand string, m any) (gjson.Result, error) {
b.syncIdMap.Store(syncId, ch)
err = b.c.WriteMessage(websocket.TextMessage, buf)
if err != nil {
log.Error("write error", "error", err)
log.Error("send error", "error", err)
return gjson.Result{}, err
}
log.Debug("write: " + string(buf))
log.Debug("send", "content", m, "syncId", syncId, "cmd", command, "subCmd", subCommand)
timeoutTimer := time.AfterFunc(5*time.Second, func() {
if ch, ok := b.syncIdMap.LoadAndDelete(syncId); ok {
close(ch.(chan gjson.Result))
Expand Down

0 comments on commit 0558d81

Please sign in to comment.