qq_bot/message/at.go

42 lines
832 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package message
import (
"encoding/json"
"fmt"
"regexp"
)
// AtMessage @消息
type AtMessage struct {
Type string `json:"type"`
Data AtMessageData `json:"data"`
}
type AtMessageData struct {
QQ string `json:"qq"` // QQ号"all"表示@全体成员
}
func init() {
RegisterMessageType(TypeAt, func() CQMessage {
return &AtMessage{Type: TypeAt}
})
}
func (msg *AtMessage) SetData(data json.RawMessage) error {
return json.Unmarshal(data, &msg.Data)
}
func (msg *AtMessage) ToCQString() string {
return fmt.Sprintf("[CQ:at,qq=%s]", msg.Data.QQ)
}
func (msg *AtMessage) ParseMessage(data string) error {
re := regexp.MustCompile(`\[CQ:at,qq=(\d+|all)\]`)
matches := re.FindStringSubmatch(data)
if len(matches) < 2 {
return fmt.Errorf("@消息格式不正确")
}
msg.Data.QQ = matches[1]
return nil
}