chore: update dependencies and improve bot configuration
- Upgrade Go version to 1.24.0 and update toolchain. - Update various dependencies in go.mod and go.sum, including: - Upgrade `fasthttp/websocket` to v1.5.12 - Upgrade `fsnotify/fsnotify` to v1.9.0 - Upgrade `valyala/fasthttp` to v1.58.0 - Add new dependencies for `bytedance/sonic` and `google/uuid`. - Refactor bot configuration in config.toml to support multiple bot protocols, including "milky" and "onebot11". - Modify internal configuration structures to accommodate new bot settings. - Enhance event dispatcher with metrics tracking and asynchronous processing capabilities. - Implement WebSocket connection management with heartbeat and reconnection logic. - Update server handling for bot management and event publishing.
This commit is contained in:
306
internal/adapter/onebot11/action.go
Normal file
306
internal/adapter/onebot11/action.go
Normal file
@@ -0,0 +1,306 @@
|
||||
package onebot11
|
||||
|
||||
import (
|
||||
"cellbot/internal/protocol"
|
||||
)
|
||||
|
||||
// OneBot11 API动作常量
|
||||
const (
|
||||
ActionSendPrivateMsg = "send_private_msg"
|
||||
ActionSendGroupMsg = "send_group_msg"
|
||||
ActionSendMsg = "send_msg"
|
||||
ActionDeleteMsg = "delete_msg"
|
||||
ActionGetMsg = "get_msg"
|
||||
ActionGetForwardMsg = "get_forward_msg"
|
||||
ActionSendLike = "send_like"
|
||||
ActionSetGroupKick = "set_group_kick"
|
||||
ActionSetGroupBan = "set_group_ban"
|
||||
ActionSetGroupAnonymousBan = "set_group_anonymous_ban"
|
||||
ActionSetGroupWholeBan = "set_group_whole_ban"
|
||||
ActionSetGroupAdmin = "set_group_admin"
|
||||
ActionSetGroupAnonymous = "set_group_anonymous"
|
||||
ActionSetGroupCard = "set_group_card"
|
||||
ActionSetGroupName = "set_group_name"
|
||||
ActionSetGroupLeave = "set_group_leave"
|
||||
ActionSetGroupSpecialTitle = "set_group_special_title"
|
||||
ActionSetFriendAddRequest = "set_friend_add_request"
|
||||
ActionSetGroupAddRequest = "set_group_add_request"
|
||||
ActionGetLoginInfo = "get_login_info"
|
||||
ActionGetStrangerInfo = "get_stranger_info"
|
||||
ActionGetFriendList = "get_friend_list"
|
||||
ActionGetGroupInfo = "get_group_info"
|
||||
ActionGetGroupList = "get_group_list"
|
||||
ActionGetGroupMemberInfo = "get_group_member_info"
|
||||
ActionGetGroupMemberList = "get_group_member_list"
|
||||
ActionGetGroupHonorInfo = "get_group_honor_info"
|
||||
ActionGetCookies = "get_cookies"
|
||||
ActionGetCsrfToken = "get_csrf_token"
|
||||
ActionGetCredentials = "get_credentials"
|
||||
ActionGetRecord = "get_record"
|
||||
ActionGetImage = "get_image"
|
||||
ActionCanSendImage = "can_send_image"
|
||||
ActionCanSendRecord = "can_send_record"
|
||||
ActionGetStatus = "get_status"
|
||||
ActionGetVersionInfo = "get_version_info"
|
||||
ActionSetRestart = "set_restart"
|
||||
ActionCleanCache = "clean_cache"
|
||||
)
|
||||
|
||||
// ConvertAction 将通用Action转换为OneBot11 Action
|
||||
func ConvertAction(action protocol.Action) string {
|
||||
switch action.GetType() {
|
||||
case protocol.ActionTypeSendPrivateMessage:
|
||||
return ActionSendPrivateMsg
|
||||
case protocol.ActionTypeSendGroupMessage:
|
||||
return ActionSendGroupMsg
|
||||
case protocol.ActionTypeDeleteMessage:
|
||||
return ActionDeleteMsg
|
||||
case protocol.ActionTypeGetUserInfo:
|
||||
return ActionGetStrangerInfo
|
||||
case protocol.ActionTypeGetFriendList:
|
||||
return ActionGetFriendList
|
||||
case protocol.ActionTypeGetGroupInfo:
|
||||
return ActionGetGroupInfo
|
||||
case protocol.ActionTypeGetGroupMemberList:
|
||||
return ActionGetGroupMemberList
|
||||
case protocol.ActionTypeSetGroupKick:
|
||||
return ActionSetGroupKick
|
||||
case protocol.ActionTypeSetGroupBan:
|
||||
return ActionSetGroupBan
|
||||
case protocol.ActionTypeSetGroupAdmin:
|
||||
return ActionSetGroupAdmin
|
||||
case protocol.ActionTypeSetGroupWholeBan:
|
||||
return ActionSetGroupWholeBan
|
||||
case protocol.ActionTypeGetStatus:
|
||||
return ActionGetStatus
|
||||
case protocol.ActionTypeGetVersion:
|
||||
return ActionGetVersionInfo
|
||||
default:
|
||||
return string(action.GetType())
|
||||
}
|
||||
}
|
||||
|
||||
// SendPrivateMessageAction 发送私聊消息动作
|
||||
type SendPrivateMessageAction struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Message interface{} `json:"message"`
|
||||
AutoEscape bool `json:"auto_escape,omitempty"`
|
||||
}
|
||||
|
||||
// SendGroupMessageAction 发送群消息动作
|
||||
type SendGroupMessageAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
Message interface{} `json:"message"`
|
||||
AutoEscape bool `json:"auto_escape,omitempty"`
|
||||
}
|
||||
|
||||
// DeleteMessageAction 撤回消息动作
|
||||
type DeleteMessageAction struct {
|
||||
MessageID int32 `json:"message_id"`
|
||||
}
|
||||
|
||||
// GetMessageAction 获取消息动作
|
||||
type GetMessageAction struct {
|
||||
MessageID int32 `json:"message_id"`
|
||||
}
|
||||
|
||||
// SendLikeAction 发送好友赞动作
|
||||
type SendLikeAction struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Times int `json:"times,omitempty"`
|
||||
}
|
||||
|
||||
// SetGroupKickAction 群组踢人动作
|
||||
type SetGroupKickAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
RejectAddRequest bool `json:"reject_add_request,omitempty"`
|
||||
}
|
||||
|
||||
// SetGroupBanAction 群组禁言动作
|
||||
type SetGroupBanAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
Duration int64 `json:"duration,omitempty"` // 禁言时长,单位秒,0表示取消禁言
|
||||
}
|
||||
|
||||
// SetGroupWholeBanAction 群组全员禁言动作
|
||||
type SetGroupWholeBanAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
Enable bool `json:"enable,omitempty"`
|
||||
}
|
||||
|
||||
// SetGroupAdminAction 设置群管理员动作
|
||||
type SetGroupAdminAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
Enable bool `json:"enable,omitempty"`
|
||||
}
|
||||
|
||||
// SetGroupCardAction 设置群名片动作
|
||||
type SetGroupCardAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
Card string `json:"card,omitempty"`
|
||||
}
|
||||
|
||||
// SetGroupNameAction 设置群名动作
|
||||
type SetGroupNameAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
GroupName string `json:"group_name"`
|
||||
}
|
||||
|
||||
// SetGroupLeaveAction 退出群组动作
|
||||
type SetGroupLeaveAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
IsDismiss bool `json:"is_dismiss,omitempty"`
|
||||
}
|
||||
|
||||
// SetFriendAddRequestAction 处理加好友请求动作
|
||||
type SetFriendAddRequestAction struct {
|
||||
Flag string `json:"flag"`
|
||||
Approve bool `json:"approve,omitempty"`
|
||||
Remark string `json:"remark,omitempty"`
|
||||
}
|
||||
|
||||
// SetGroupAddRequestAction 处理加群请求动作
|
||||
type SetGroupAddRequestAction struct {
|
||||
Flag string `json:"flag"`
|
||||
SubType string `json:"sub_type,omitempty"` // add 或 invite
|
||||
Approve bool `json:"approve,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// GetStrangerInfoAction 获取陌生人信息动作
|
||||
type GetStrangerInfoAction struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
NoCache bool `json:"no_cache,omitempty"`
|
||||
}
|
||||
|
||||
// GetGroupInfoAction 获取群信息动作
|
||||
type GetGroupInfoAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
NoCache bool `json:"no_cache,omitempty"`
|
||||
}
|
||||
|
||||
// GetGroupMemberInfoAction 获取群成员信息动作
|
||||
type GetGroupMemberInfoAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
NoCache bool `json:"no_cache,omitempty"`
|
||||
}
|
||||
|
||||
// GetGroupMemberListAction 获取群成员列表动作
|
||||
type GetGroupMemberListAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
}
|
||||
|
||||
// GetGroupHonorInfoAction 获取群荣誉信息动作
|
||||
type GetGroupHonorInfoAction struct {
|
||||
GroupID int64 `json:"group_id"`
|
||||
Type string `json:"type"` // talkative, performer, legend, strong_newbie, emotion, all
|
||||
}
|
||||
|
||||
// GetCookiesAction 获取Cookies动作
|
||||
type GetCookiesAction struct {
|
||||
Domain string `json:"domain,omitempty"`
|
||||
}
|
||||
|
||||
// GetRecordAction 获取语音动作
|
||||
type GetRecordAction struct {
|
||||
File string `json:"file"`
|
||||
OutFormat string `json:"out_format"`
|
||||
}
|
||||
|
||||
// GetImageAction 获取图片动作
|
||||
type GetImageAction struct {
|
||||
File string `json:"file"`
|
||||
}
|
||||
|
||||
// SetRestartAction 重启OneBot实现动作
|
||||
type SetRestartAction struct {
|
||||
Delay int `json:"delay,omitempty"` // 延迟毫秒数
|
||||
}
|
||||
|
||||
// ActionResponse API响应
|
||||
type ActionResponse struct {
|
||||
Status string `json:"status"`
|
||||
RetCode int `json:"retcode"`
|
||||
Data map[string]interface{} `json:"data,omitempty"`
|
||||
Echo string `json:"echo,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Wording string `json:"wording,omitempty"`
|
||||
}
|
||||
|
||||
// 响应状态码常量
|
||||
const (
|
||||
RetCodeOK = 0
|
||||
RetCodeAsyncStarted = 1 // 异步操作已开始
|
||||
RetCodeBadRequest = 1400 // 请求格式错误
|
||||
RetCodeUnauthorized = 1401 // 未授权
|
||||
RetCodeForbidden = 1403 // 禁止访问
|
||||
RetCodeNotFound = 1404 // 接口不存在
|
||||
RetCodeMethodNotAllowed = 1405 // 请求方法不支持
|
||||
RetCodeInternalError = 1500 // 内部错误
|
||||
)
|
||||
|
||||
// BuildActionRequest 构建动作请求
|
||||
func BuildActionRequest(action string, params map[string]interface{}, echo string) *OB11Action {
|
||||
return &OB11Action{
|
||||
Action: action,
|
||||
Params: params,
|
||||
Echo: echo,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSendPrivateMsg 构建发送私聊消息请求
|
||||
func BuildSendPrivateMsg(userID int64, message interface{}, autoEscape bool) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"user_id": userID,
|
||||
"message": message,
|
||||
"auto_escape": autoEscape,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSendGroupMsg 构建发送群消息请求
|
||||
func BuildSendGroupMsg(groupID int64, message interface{}, autoEscape bool) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"message": message,
|
||||
"auto_escape": autoEscape,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildDeleteMsg 构建撤回消息请求
|
||||
func BuildDeleteMsg(messageID int32) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"message_id": messageID,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetGroupBan 构建群组禁言请求
|
||||
func BuildSetGroupBan(groupID, userID int64, duration int64) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"user_id": userID,
|
||||
"duration": duration,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetGroupKick 构建群组踢人请求
|
||||
func BuildSetGroupKick(groupID, userID int64, rejectAddRequest bool) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"user_id": userID,
|
||||
"reject_add_request": rejectAddRequest,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetGroupCard 构建设置群名片请求
|
||||
func BuildSetGroupCard(groupID, userID int64, card string) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"user_id": userID,
|
||||
"card": card,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user