diff --git a/api/types.go b/api/types.go index ffede8f..8293152 100644 --- a/api/types.go +++ b/api/types.go @@ -278,7 +278,7 @@ type EmojiLikeNotice struct { } `json:"likes"` } -type GroupRecallNotice struct { +type RecallNotice struct { GroupID uint64 `json:"group_id"` UserID uint64 `json:"user_id"` OperatorID uint64 `json:"operator_id"` @@ -286,12 +286,6 @@ type GroupRecallNotice struct { Time int64 `json:"time"` } -type FriendRecallNotice struct { - UserID uint64 `json:"user_id"` - MessageID uint64 `json:"message_id"` - Time int64 `json:"time"` -} - type PokeNotify struct { GroupID uint64 `json:"group_id"` UserID uint64 `json:"user_id"` // Sender diff --git a/event.go b/event.go index ddddd5e..0ed7a5d 100644 --- a/event.go +++ b/event.go @@ -22,16 +22,9 @@ func (b *Bot) handleEvents(postType *string, msgStr *[]byte, jsonMap *map[string } } case "group_recall": - notice := &api.GroupRecallNotice{} - if json.Unmarshal(*msgStr, notice) == nil { - if n := parseRecallNotice(notice); n != nil { - for _, handler := range b.eventHandlers.recall { - handler(b, n) - } - } - } + fallthrough case "friend_recall": - notice := &api.FriendRecallNotice{} + notice := &api.RecallNotice{} if json.Unmarshal(*msgStr, notice) == nil { if n := parseRecallNotice(notice); n != nil { for _, handler := range b.eventHandlers.recall { @@ -185,51 +178,43 @@ func parseMsgJson(raw *api.MessageJson) *Message { return &result } -func parseEmojiLikeNotice(raw *api.EmojiLikeNotice) *EmojiLikeNotice { - if raw == nil { +func parseEmojiLikeNotice(raw *api.EmojiLikeNotice) *EmojiReaction { + if raw == nil || len(raw.Likes) == 0 { return nil } - notice := &EmojiLikeNotice{ + + notice := &EmojiReaction{ GroupID: raw.GroupID, UserID: raw.UserID, MessageID: raw.MessageID, IsAdd: raw.IsAdd, + Count: raw.Likes[0].Count, } - for _, like := range raw.Likes { - notice.Likes = append(notice.Likes, &EmojiLikeItem{ - Count: like.Count, - EmojiID: like.EmojiID, - }) + + id, err := strconv.ParseUint(raw.Likes[0].EmojiID, 10, 64) + if err != nil { + return nil } + + if id < 1000 { + notice.IsQFace = true + notice.FaceID = id + } else { + notice.IsQFace = false + notice.EmojiRune = rune(id) + } + return notice } -func parseRecallNotice(raw any) *RecallNotice { - switch v := raw.(type) { - case *api.GroupRecallNotice: - if v == nil { - return nil - } - return &RecallNotice{ - ChatType: Group, - GroupID: v.GroupID, - UserID: v.UserID, - OperatorID: v.OperatorID, - MessageID: v.MessageID, - Time: v.Time, - } - case *api.FriendRecallNotice: - if v == nil { - return nil - } - return &RecallNotice{ - ChatType: Private, - UserID: v.UserID, - MessageID: v.MessageID, - Time: v.Time, - } - default: - return nil +func parseRecallNotice(raw *api.RecallNotice) *RecallNotice { + return &RecallNotice{ + ChatType: Group, + GroupID: raw.GroupID, + UserID: raw.UserID, + OperatorID: raw.OperatorID, + MessageID: raw.MessageID, + Time: raw.Time, } } diff --git a/http.go b/http.go index 7afdf93..d3875bc 100644 --- a/http.go +++ b/http.go @@ -20,14 +20,6 @@ type cqResponse struct { // Send raw parameters to NapCat func (b *Bot) SendParams(action string, params map[string]any) (json.RawMessage, error) { - if b.enableDebug { - log.Println() - jsonBytes, err := json.Marshal(params) - if err != nil { - return nil, err - } - log.Printf("[Debug] qbot.SendParams: %s\n%s", action, string(jsonBytes)) - } resp, err := b.sendHttpRequest(action, params) if err != nil { return nil, err @@ -73,10 +65,6 @@ func (b *Bot) sendHttpRequest(action string, params map[string]any) (*cqResponse return nil, err } - if b.enableDebug { - log.Printf("[Debug] %s: %s\n%s", action, httpResp.Status, string(body)) - } - var cqResp cqResponse if err := json.Unmarshal(body, &cqResp); err != nil { return nil, err @@ -104,13 +92,6 @@ func (b *Bot) handleHttpEvent(w http.ResponseWriter, r *http.Request) { return } - logJson, err := json.MarshalIndent(jsonMap, "", " ") - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - log.Println(string(logJson)) - if postType, exists := jsonMap["post_type"]; exists { if str, ok := postType.(string); ok && str != "" { go b.handleEvents(&str, &body, &jsonMap) diff --git a/message.go b/message.go index 3e8b98c..503fc5b 100644 --- a/message.go +++ b/message.go @@ -3,16 +3,16 @@ package qbot type MsgType int const ( - TextType MsgType = 0 - AtType MsgType = 1 - FaceType MsgType = 2 - ImageType MsgType = 3 - RecordType MsgType = 4 - FileType MsgType = 5 - ForwardType MsgType = 6 - JsonType MsgType = 7 + TextType MsgType = 1 + AtType MsgType = 2 + FaceType MsgType = 3 + ImageType MsgType = 4 + RecordType MsgType = 5 + FileType MsgType = 6 + ForwardType MsgType = 7 + JsonType MsgType = 8 - OtherType MsgType = -1 + OtherType MsgType = 0 ) type MsgItem interface { @@ -112,15 +112,18 @@ type Message struct { type EmojiLikeItem struct { Count int32 - EmojiID string + EmojiID uint64 } -type EmojiLikeNotice struct { +type EmojiReaction struct { GroupID uint64 UserID uint64 MessageID uint64 IsAdd bool - Likes []*EmojiLikeItem + IsQFace bool + Count int32 + FaceID uint64 + EmojiRune rune } type RecallNotice struct { diff --git a/qbot.go b/qbot.go index 48174ac..1121dc7 100644 --- a/qbot.go +++ b/qbot.go @@ -16,7 +16,7 @@ type Bot struct { enableDebug bool eventHandlers struct { message []func(b *Bot, msg *Message) - emojiLike []func(b *Bot, msg *EmojiLikeNotice) + emojiLike []func(b *Bot, msg *EmojiReaction) recall []func(b *Bot, msg *RecallNotice) poke []func(b *Bot, msg *PokeNotify) } @@ -35,7 +35,7 @@ func NewBot(address string) *Bot { }, } bot.eventHandlers.message = make([]func(b *Bot, msg *Message), 0) - bot.eventHandlers.emojiLike = make([]func(b *Bot, msg *EmojiLikeNotice), 0) + bot.eventHandlers.emojiLike = make([]func(b *Bot, msg *EmojiReaction), 0) bot.eventHandlers.recall = make([]func(b *Bot, msg *RecallNotice), 0) bot.eventHandlers.poke = make([]func(b *Bot, msg *PokeNotify), 0) @@ -103,15 +103,15 @@ func (b *Bot) OnMessage(handler func(b *Bot, msg *Message)) { b.eventHandlers.message = append(b.eventHandlers.message, handler) } -func (b *Bot) OnEmojiLike(handler func(b *Bot, msg *EmojiLikeNotice)) { +func (b *Bot) OnEmojiReaction(handler func(b *Bot, emoji *EmojiReaction)) { b.eventHandlers.emojiLike = append(b.eventHandlers.emojiLike, handler) } -func (b *Bot) OnRecall(handler func(b *Bot, msg *RecallNotice)) { +func (b *Bot) OnRecall(handler func(b *Bot, recall *RecallNotice)) { b.eventHandlers.recall = append(b.eventHandlers.recall, handler) } -func (b *Bot) OnPoke(handler func(b *Bot, msg *PokeNotify)) { +func (b *Bot) OnPoke(handler func(b *Bot, poke *PokeNotify)) { b.eventHandlers.poke = append(b.eventHandlers.poke, handler) }