feat: 重构消息处理模块,引入统一的消息接口和类型安全的消息解析
This commit is contained in:
36
message/at.go
Normal file
36
message/at.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// @某个QQ用户的结构体
|
||||
type AtMessage struct {
|
||||
Type string `json:"type"`
|
||||
Data AtData `json:"data"`
|
||||
}
|
||||
|
||||
// @消息数据结构体
|
||||
type AtData struct {
|
||||
QQ string `json:"qq"`
|
||||
}
|
||||
|
||||
func (msg *AtMessage) ToCQString() string {
|
||||
return fmt.Sprintf(`\[CQ:at,qq=%s\]`, msg.Data.QQ)
|
||||
}
|
||||
|
||||
func (msg *AtMessage) ParseMessage(data string) error {
|
||||
// 使用正则表达式提取QQ号
|
||||
re := regexp.MustCompile(`\[CQ:at,qq=(\d+)\]`)
|
||||
matches := re.FindStringSubmatch(data)
|
||||
if len(matches) < 2 {
|
||||
return fmt.Errorf("数据格式不正确")
|
||||
}
|
||||
|
||||
// 返回解析后的结构体
|
||||
msg.Data = AtData{
|
||||
QQ: matches[1],
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user