46 lines
891 B
Go
46 lines
891 B
Go
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 NewAtMessage()
|
||
})
|
||
}
|
||
|
||
func NewAtMessage() *AtMessage {
|
||
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
|
||
}
|