feat: enhance event handling and add scheduling capabilities
- Introduced a new scheduler to manage timed tasks within the event dispatcher. - Updated the dispatcher to support the new scheduler, allowing for improved event processing. - Enhanced action serialization in the OneBot11 adapter to convert message chains to the appropriate format. - Added new dependencies for cron scheduling and other indirect packages in go.mod and go.sum. - Improved logging for event publishing and handler matching, providing better insights during execution. - Refactored plugin loading to include scheduled job management.
This commit is contained in:
133
internal/protocol/message.go
Normal file
133
internal/protocol/message.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package protocol
|
||||
|
||||
import "fmt"
|
||||
|
||||
// MessageSegment 消息段(基于 OneBot12 设计)
|
||||
// 通用消息段结构,适配器负责转换为具体协议格式
|
||||
type MessageSegment struct {
|
||||
Type string `json:"type"` // 消息段类型
|
||||
Data map[string]interface{} `json:"data"` // 消息段数据
|
||||
}
|
||||
|
||||
// MessageChain 消息链(基于 OneBot12 设计)
|
||||
// 消息由多个消息段组成
|
||||
type MessageChain []MessageSegment
|
||||
|
||||
// 消息段类型常量(基于 OneBot12)
|
||||
const (
|
||||
SegmentTypeText = "text" // 文本
|
||||
SegmentTypeMention = "mention" // @提及(OneBot12)
|
||||
SegmentTypeImage = "image" // 图片
|
||||
SegmentTypeVoice = "voice" // 语音
|
||||
SegmentTypeVideo = "video" // 视频
|
||||
SegmentTypeFile = "file" // 文件
|
||||
SegmentTypeLocation = "location" // 位置
|
||||
SegmentTypeReply = "reply" // 回复
|
||||
SegmentTypeForward = "forward" // 转发
|
||||
SegmentTypeFace = "face" // 表情(QQ)
|
||||
SegmentTypeAt = "at" // @提及(OneBot11 兼容)
|
||||
SegmentTypeRecord = "record" // 语音(OneBot11 兼容)
|
||||
)
|
||||
|
||||
// NewTextSegment 创建文本消息段
|
||||
func NewTextSegment(text string) MessageSegment {
|
||||
return MessageSegment{
|
||||
Type: SegmentTypeText,
|
||||
Data: map[string]interface{}{
|
||||
"text": text,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewMentionSegment 创建@提及消息段(OneBot12 标准)
|
||||
func NewMentionSegment(userID interface{}) MessageSegment {
|
||||
return MessageSegment{
|
||||
Type: SegmentTypeMention,
|
||||
Data: map[string]interface{}{
|
||||
"user_id": userID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewImageSegment 创建图片消息段
|
||||
func NewImageSegment(fileID string) MessageSegment {
|
||||
return MessageSegment{
|
||||
Type: SegmentTypeImage,
|
||||
Data: map[string]interface{}{
|
||||
"file_id": fileID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewImageSegmentFromBase64 从base64字符串创建图片消息段
|
||||
func NewImageSegmentFromBase64(base64Data string) MessageSegment {
|
||||
return MessageSegment{
|
||||
Type: SegmentTypeImage,
|
||||
Data: map[string]interface{}{
|
||||
"file": fmt.Sprintf("base64://%s", base64Data),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewReplySegment 创建回复消息段
|
||||
func NewReplySegment(messageID interface{}) MessageSegment {
|
||||
return MessageSegment{
|
||||
Type: SegmentTypeReply,
|
||||
Data: map[string]interface{}{
|
||||
"message_id": messageID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewMessageChain 创建消息链
|
||||
func NewMessageChain(segments ...MessageSegment) MessageChain {
|
||||
return MessageChain(segments)
|
||||
}
|
||||
|
||||
// Append 追加消息段到消息链
|
||||
func (mc MessageChain) Append(segments ...MessageSegment) MessageChain {
|
||||
return append(mc, segments...)
|
||||
}
|
||||
|
||||
// AppendText 追加文本到消息链
|
||||
func (mc MessageChain) AppendText(text string) MessageChain {
|
||||
return mc.Append(NewTextSegment(text))
|
||||
}
|
||||
|
||||
// AppendMention 追加@提及到消息链
|
||||
func (mc MessageChain) AppendMention(userID interface{}) MessageChain {
|
||||
return mc.Append(NewMentionSegment(userID))
|
||||
}
|
||||
|
||||
// AppendImage 追加图片到消息链
|
||||
func (mc MessageChain) AppendImage(fileID string) MessageChain {
|
||||
return mc.Append(NewImageSegment(fileID))
|
||||
}
|
||||
|
||||
// AppendImageFromBase64 从base64追加图片到消息链
|
||||
func (mc MessageChain) AppendImageFromBase64(base64Data string) MessageChain {
|
||||
return mc.Append(NewImageSegmentFromBase64(base64Data))
|
||||
}
|
||||
|
||||
// ToString 将消息链转换为字符串(用于调试)
|
||||
func (mc MessageChain) ToString() string {
|
||||
result := ""
|
||||
for _, seg := range mc {
|
||||
switch seg.Type {
|
||||
case SegmentTypeText:
|
||||
if text, ok := seg.Data["text"].(string); ok {
|
||||
result += text
|
||||
}
|
||||
case SegmentTypeMention, SegmentTypeAt:
|
||||
if userID, ok := seg.Data["user_id"]; ok {
|
||||
result += fmt.Sprintf("@%v", userID)
|
||||
} else if qq, ok := seg.Data["qq"]; ok {
|
||||
// OneBot11 兼容
|
||||
result += fmt.Sprintf("@%v", qq)
|
||||
}
|
||||
default:
|
||||
result += fmt.Sprintf("[%s]", seg.Type)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user