refactor: modernize codebase for Go 1.26 and fix adapter issues

- Upgrade Go version from 1.24 to 1.26.1 with updated dependencies
- Replace interface{} with any type throughout codebase
- Add message deduplication in OneBot11 adapter to prevent duplicate event processing
- URL-encode access token in WebSocket connections to handle special characters
- Remove duplicate bot startup hooks in DI providers (now handled by botManager.StartAll)
- Use slices.Clone and errgroup.Go patterns for cleaner concurrent code
- Apply omitzero JSON tags for optional fields (Go 1.24+ feature)
This commit is contained in:
lafay
2026-03-18 01:22:49 +08:00
parent f3a72264af
commit f3220a2381
32 changed files with 2262 additions and 296 deletions

View File

@@ -6,9 +6,9 @@ type Action interface {
// GetType 获取动作类型
GetType() ActionType
// GetParams 获取动作参数
GetParams() map[string]interface{}
GetParams() map[string]any
// Execute 执行动作
Execute(ctx interface{}) (map[string]interface{}, error)
Execute(ctx any) (map[string]any, error)
}
// ActionType 动作类型
@@ -27,20 +27,20 @@ const (
ActionTypeGetGroupMemberList ActionType = "get_group_member_list"
// 群组相关动作
ActionTypeSetGroupKick ActionType = "set_group_kick"
ActionTypeSetGroupBan ActionType = "set_group_ban"
ActionTypeSetGroupAdmin ActionType = "set_group_admin"
ActionTypeSetGroupWholeBan ActionType = "set_group_whole_ban"
ActionTypeSetGroupKick ActionType = "set_group_kick"
ActionTypeSetGroupBan ActionType = "set_group_ban"
ActionTypeSetGroupAdmin ActionType = "set_group_admin"
ActionTypeSetGroupWholeBan ActionType = "set_group_whole_ban"
// 其他动作
ActionTypeGetStatus ActionType = "get_status"
ActionTypeGetVersion ActionType = "get_version"
ActionTypeGetStatus ActionType = "get_status"
ActionTypeGetVersion ActionType = "get_version"
)
// BaseAction 基础动作结构
type BaseAction struct {
Type ActionType `json:"type"`
Params map[string]interface{} `json:"params"`
Type ActionType `json:"type"`
Params map[string]any `json:"params"`
}
// GetType 获取动作类型
@@ -49,19 +49,19 @@ func (a *BaseAction) GetType() ActionType {
}
// GetParams 获取动作参数
func (a *BaseAction) GetParams() map[string]interface{} {
func (a *BaseAction) GetParams() map[string]any {
return a.Params
}
// Execute 执行动作(需子类实现)
func (a *BaseAction) Execute(ctx interface{}) (map[string]interface{}, error) {
func (a *BaseAction) Execute(ctx any) (map[string]any, error) {
return nil, ErrNotImplemented
}
// SendPrivateMessageAction 发送私聊消息动作
type SendPrivateMessageAction struct {
BaseAction
UserID string `json:"user_id"`
UserID string `json:"user_id"`
Message string `json:"message"`
}

View File

@@ -91,7 +91,7 @@ func (b *BaseBotInstance) IsConnected() bool {
}
// SendAction 发送动作
func (b *BaseBotInstance) SendAction(ctx context.Context, action Action) (map[string]interface{}, error) {
func (b *BaseBotInstance) SendAction(ctx context.Context, action Action) (map[string]any, error) {
return b.protocol.SendAction(ctx, action)
}

View File

@@ -35,7 +35,7 @@ type Event interface {
// GetSelfID 获取机器人自身ID
GetSelfID() string
// GetData 获取事件数据
GetData() map[string]interface{}
GetData() map[string]any
// Reply 在消息发生的群/私聊进行回复
Reply(ctx context.Context, botManager *BotManager, logger *zap.Logger, message MessageChain) error
// ReplyText 在消息发生的群/私聊进行文本回复(便捷方法)
@@ -44,12 +44,12 @@ type Event interface {
// BaseEvent 基础事件结构
type BaseEvent struct {
Type EventType `json:"type"`
DetailType string `json:"detail_type"`
SubType string `json:"sub_type,omitempty"`
Timestamp int64 `json:"timestamp"`
SelfID string `json:"self_id"`
Data map[string]interface{} `json:"data"`
Type EventType `json:"type"`
DetailType string `json:"detail_type"`
SubType string `json:"sub_type,omitzero"`
Timestamp int64 `json:"timestamp"`
SelfID string `json:"self_id"`
Data map[string]any `json:"data"`
}
// GetType 获取事件类型
@@ -78,7 +78,7 @@ func (e *BaseEvent) GetSelfID() string {
}
// GetData 获取事件数据
func (e *BaseEvent) GetData() map[string]interface{} {
func (e *BaseEvent) GetData() map[string]any {
return e.Data
}
@@ -105,7 +105,7 @@ func (e *BaseEvent) Reply(ctx context.Context, botManager *BotManager, logger *z
if e.GetDetailType() == "private" {
action = &BaseAction{
Type: ActionTypeSendPrivateMessage,
Params: map[string]interface{}{
Params: map[string]any{
"user_id": userID,
"message": message,
},
@@ -114,7 +114,7 @@ func (e *BaseEvent) Reply(ctx context.Context, botManager *BotManager, logger *z
// 群聊或其他有 group_id 的事件
action = &BaseAction{
Type: ActionTypeSendGroupMessage,
Params: map[string]interface{}{
Params: map[string]any{
"group_id": groupID,
"message": message,
},
@@ -151,15 +151,15 @@ type MessageEvent struct {
BaseEvent
MessageID string `json:"message_id"`
Message string `json:"message"`
AltText string `json:"alt_text,omitempty"`
AltText string `json:"alt_text,omitzero"`
}
// NoticeEvent 通知事件
type NoticeEvent struct {
BaseEvent
GroupID string `json:"group_id,omitempty"`
UserID string `json:"user_id,omitempty"`
Operator string `json:"operator,omitempty"`
GroupID string `json:"group_id,omitzero"`
UserID string `json:"user_id,omitzero"`
Operator string `json:"operator,omitzero"`
}
// RequestEvent 请求事件

View File

@@ -18,7 +18,7 @@ type Protocol interface {
// IsConnected 检查连接状态
IsConnected() bool
// SendAction 发送动作
SendAction(ctx context.Context, action Action) (map[string]interface{}, error)
SendAction(ctx context.Context, action Action) (map[string]any, error)
// HandleEvent 处理事件
HandleEvent(ctx context.Context, event Event) error
// GetSelfID 获取机器人自身ID

View File

@@ -5,8 +5,8 @@ import "fmt"
// MessageSegment 消息段(基于 OneBot12 设计)
// 通用消息段结构,适配器负责转换为具体协议格式
type MessageSegment struct {
Type string `json:"type"` // 消息段类型
Data map[string]interface{} `json:"data"` // 消息段数据
Type string `json:"type"` // 消息段类型
Data map[string]any `json:"data"` // 消息段数据
}
// MessageChain 消息链(基于 OneBot12 设计)
@@ -33,17 +33,17 @@ const (
func NewTextSegment(text string) MessageSegment {
return MessageSegment{
Type: SegmentTypeText,
Data: map[string]interface{}{
Data: map[string]any{
"text": text,
},
}
}
// NewMentionSegment 创建@提及消息段OneBot12 标准)
func NewMentionSegment(userID interface{}) MessageSegment {
func NewMentionSegment(userID any) MessageSegment {
return MessageSegment{
Type: SegmentTypeMention,
Data: map[string]interface{}{
Data: map[string]any{
"user_id": userID,
},
}
@@ -53,7 +53,7 @@ func NewMentionSegment(userID interface{}) MessageSegment {
func NewImageSegment(fileID string) MessageSegment {
return MessageSegment{
Type: SegmentTypeImage,
Data: map[string]interface{}{
Data: map[string]any{
"file_id": fileID,
},
}
@@ -63,17 +63,17 @@ func NewImageSegment(fileID string) MessageSegment {
func NewImageSegmentFromBase64(base64Data string) MessageSegment {
return MessageSegment{
Type: SegmentTypeImage,
Data: map[string]interface{}{
Data: map[string]any{
"file": fmt.Sprintf("base64://%s", base64Data),
},
}
}
// NewReplySegment 创建回复消息段
func NewReplySegment(messageID interface{}) MessageSegment {
func NewReplySegment(messageID any) MessageSegment {
return MessageSegment{
Type: SegmentTypeReply,
Data: map[string]interface{}{
Data: map[string]any{
"message_id": messageID,
},
}
@@ -95,7 +95,7 @@ func (mc MessageChain) AppendText(text string) MessageChain {
}
// AppendMention 追加@提及到消息链
func (mc MessageChain) AppendMention(userID interface{}) MessageChain {
func (mc MessageChain) AppendMention(userID any) MessageChain {
return mc.Append(NewMentionSegment(userID))
}