feat: 重构消息处理模块,引入统一的消息接口和类型安全的消息解析

This commit is contained in:
lixiangwuxian
2025-03-08 16:10:06 +08:00
parent e0637ab81f
commit 13ea5d7f98
16 changed files with 412 additions and 165 deletions

36
message/at.go Normal file
View 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
}