Files
cellbot/internal/protocol/action.go
lafay f3220a2381 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)
2026-03-18 01:22:49 +08:00

108 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package protocol
// Action 动作接口
// 参考OneBot12协议定义统一的动作操作接口
type Action interface {
// GetType 获取动作类型
GetType() ActionType
// GetParams 获取动作参数
GetParams() map[string]any
// Execute 执行动作
Execute(ctx any) (map[string]any, error)
}
// ActionType 动作类型
type ActionType string
const (
// 消息相关动作
ActionTypeSendPrivateMessage ActionType = "send_private_message"
ActionTypeSendGroupMessage ActionType = "send_group_message"
ActionTypeDeleteMessage ActionType = "delete_message"
// 用户相关动作
ActionTypeGetUserInfo ActionType = "get_user_info"
ActionTypeGetFriendList ActionType = "get_friend_list"
ActionTypeGetGroupInfo ActionType = "get_group_info"
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"
// 其他动作
ActionTypeGetStatus ActionType = "get_status"
ActionTypeGetVersion ActionType = "get_version"
)
// BaseAction 基础动作结构
type BaseAction struct {
Type ActionType `json:"type"`
Params map[string]any `json:"params"`
}
// GetType 获取动作类型
func (a *BaseAction) GetType() ActionType {
return a.Type
}
// GetParams 获取动作参数
func (a *BaseAction) GetParams() map[string]any {
return a.Params
}
// Execute 执行动作(需子类实现)
func (a *BaseAction) Execute(ctx any) (map[string]any, error) {
return nil, ErrNotImplemented
}
// SendPrivateMessageAction 发送私聊消息动作
type SendPrivateMessageAction struct {
BaseAction
UserID string `json:"user_id"`
Message string `json:"message"`
}
// SendGroupMessageAction 发送群聊消息动作
type SendGroupMessageAction struct {
BaseAction
GroupID string `json:"group_id"`
Message string `json:"message"`
}
// DeleteMessageAction 删除消息动作
type DeleteMessageAction struct {
BaseAction
MessageID string `json:"message_id"`
}
// GetUserInfoAction 获取用户信息动作
type GetUserInfoAction struct {
BaseAction
UserID string `json:"user_id"`
}
// GetGroupInfoAction 获取群信息动作
type GetGroupInfoAction struct {
BaseAction
GroupID string `json:"group_id"`
}
// 错误定义
var (
ErrNotImplemented = &ProtocolError{Code: 10001, Message: "action not implemented"}
ErrInvalidParams = &ProtocolError{Code: 10002, Message: "invalid parameters"}
)
// ProtocolError 协议错误
type ProtocolError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (e *ProtocolError) Error() string {
return e.Message
}