qq_bot/message/reply.go

51 lines
1.0 KiB
Go

package message
import (
"encoding/json"
"fmt"
"regexp"
)
// ReplyMessage 回复消息
type ReplyMessage struct {
Type string `json:"type"`
Data ReplyMessageData `json:"data"`
}
type ReplyMessageData struct {
ID int `json:"id"`
}
func init() {
RegisterMessageType(TypeReply, func() CQMessage {
return NewReplyMessage()
})
}
func NewReplyMessage() *ReplyMessage {
return &ReplyMessage{Type: TypeReply}
}
func (msg *ReplyMessage) SetData(data json.RawMessage) error {
return json.Unmarshal(data, &msg.Data)
}
func (msg *ReplyMessage) ToCQString() string {
return fmt.Sprintf("[CQ:reply,id=%d]", msg.Data.ID)
}
func (msg *ReplyMessage) ParseMessage(data string) error {
re := regexp.MustCompile(`\[CQ:reply,id=(\d+)\]`)
matches := re.FindStringSubmatch(data)
if len(matches) < 2 {
return fmt.Errorf("回复消息格式不正确")
}
id := 0
_, err := fmt.Sscanf(matches[1], "%d", &id)
if err != nil {
return fmt.Errorf("解析回复ID失败: %v", err)
}
msg.Data.ID = id
return nil
}