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:
312
internal/engine/handler.go
Normal file
312
internal/engine/handler.go
Normal file
@@ -0,0 +1,312 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"cellbot/internal/protocol"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// BaseHandler 基础处理器
|
||||
type BaseHandler struct {
|
||||
priority int
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewBaseHandler 创建基础处理器
|
||||
func NewBaseHandler(priority int, logger *zap.Logger) *BaseHandler {
|
||||
return &BaseHandler{
|
||||
priority: priority,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Priority 获取优先级
|
||||
func (h *BaseHandler) Priority() int {
|
||||
return h.priority
|
||||
}
|
||||
|
||||
// MessageHandler 消息处理器
|
||||
type MessageHandler struct {
|
||||
*BaseHandler
|
||||
matchFunc func(protocol.Event) bool
|
||||
handleFunc func(context.Context, protocol.Event) error
|
||||
}
|
||||
|
||||
// NewMessageHandler 创建消息处理器
|
||||
func NewMessageHandler(priority int, logger *zap.Logger, matchFunc func(protocol.Event) bool, handleFunc func(context.Context, protocol.Event) error) *MessageHandler {
|
||||
return &MessageHandler{
|
||||
BaseHandler: NewBaseHandler(priority, logger.Named("handler.message")),
|
||||
matchFunc: matchFunc,
|
||||
handleFunc: handleFunc,
|
||||
}
|
||||
}
|
||||
|
||||
// Match 判断是否匹配事件
|
||||
func (h *MessageHandler) Match(event protocol.Event) bool {
|
||||
if event.GetType() != protocol.EventTypeMessage {
|
||||
return false
|
||||
}
|
||||
|
||||
if h.matchFunc != nil {
|
||||
return h.matchFunc(event)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Handle 处理事件
|
||||
func (h *MessageHandler) Handle(ctx context.Context, event protocol.Event) error {
|
||||
if h.handleFunc != nil {
|
||||
return h.handleFunc(ctx, event)
|
||||
}
|
||||
|
||||
h.logger.Info("Message event handled",
|
||||
zap.String("detail_type", event.GetDetailType()),
|
||||
zap.String("self_id", event.GetSelfID()))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandHandler 命令处理器
|
||||
type CommandHandler struct {
|
||||
*BaseHandler
|
||||
prefix string
|
||||
commands map[string]func(context.Context, protocol.Event, []string) error
|
||||
}
|
||||
|
||||
// NewCommandHandler 创建命令处理器
|
||||
func NewCommandHandler(priority int, logger *zap.Logger, prefix string) *CommandHandler {
|
||||
return &CommandHandler{
|
||||
BaseHandler: NewBaseHandler(priority, logger.Named("handler.command")),
|
||||
prefix: prefix,
|
||||
commands: make(map[string]func(context.Context, protocol.Event, []string) error),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterCommand 注册命令
|
||||
func (h *CommandHandler) RegisterCommand(cmd string, handler func(context.Context, protocol.Event, []string) error) {
|
||||
h.commands[cmd] = handler
|
||||
h.logger.Debug("Command registered", zap.String("command", cmd))
|
||||
}
|
||||
|
||||
// Match 判断是否匹配事件
|
||||
func (h *CommandHandler) Match(event protocol.Event) bool {
|
||||
if event.GetType() != protocol.EventTypeMessage {
|
||||
return false
|
||||
}
|
||||
|
||||
data := event.GetData()
|
||||
rawMessage, ok := data["raw_message"].(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.HasPrefix(rawMessage, h.prefix)
|
||||
}
|
||||
|
||||
// Handle 处理事件
|
||||
func (h *CommandHandler) Handle(ctx context.Context, event protocol.Event) error {
|
||||
data := event.GetData()
|
||||
rawMessage, ok := data["raw_message"].(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 去除前缀
|
||||
cmdText := strings.TrimPrefix(rawMessage, h.prefix)
|
||||
cmdText = strings.TrimSpace(cmdText)
|
||||
|
||||
// 解析命令和参数
|
||||
parts := strings.Fields(cmdText)
|
||||
if len(parts) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
cmd := parts[0]
|
||||
args := parts[1:]
|
||||
|
||||
// 查找命令处理器
|
||||
handler, exists := h.commands[cmd]
|
||||
if !exists {
|
||||
h.logger.Debug("Unknown command", zap.String("command", cmd))
|
||||
return nil
|
||||
}
|
||||
|
||||
h.logger.Info("Executing command",
|
||||
zap.String("command", cmd),
|
||||
zap.Strings("args", args))
|
||||
|
||||
return handler(ctx, event, args)
|
||||
}
|
||||
|
||||
// KeywordHandler 关键词处理器
|
||||
type KeywordHandler struct {
|
||||
*BaseHandler
|
||||
keywords map[string]func(context.Context, protocol.Event) error
|
||||
caseSensitive bool
|
||||
}
|
||||
|
||||
// NewKeywordHandler 创建关键词处理器
|
||||
func NewKeywordHandler(priority int, logger *zap.Logger, caseSensitive bool) *KeywordHandler {
|
||||
return &KeywordHandler{
|
||||
BaseHandler: NewBaseHandler(priority, logger.Named("handler.keyword")),
|
||||
keywords: make(map[string]func(context.Context, protocol.Event) error),
|
||||
caseSensitive: caseSensitive,
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterKeyword 注册关键词
|
||||
func (h *KeywordHandler) RegisterKeyword(keyword string, handler func(context.Context, protocol.Event) error) {
|
||||
if !h.caseSensitive {
|
||||
keyword = strings.ToLower(keyword)
|
||||
}
|
||||
h.keywords[keyword] = handler
|
||||
h.logger.Debug("Keyword registered", zap.String("keyword", keyword))
|
||||
}
|
||||
|
||||
// Match 判断是否匹配事件
|
||||
func (h *KeywordHandler) Match(event protocol.Event) bool {
|
||||
if event.GetType() != protocol.EventTypeMessage {
|
||||
return false
|
||||
}
|
||||
|
||||
data := event.GetData()
|
||||
rawMessage, ok := data["raw_message"].(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if !h.caseSensitive {
|
||||
rawMessage = strings.ToLower(rawMessage)
|
||||
}
|
||||
|
||||
for keyword := range h.keywords {
|
||||
if strings.Contains(rawMessage, keyword) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Handle 处理事件
|
||||
func (h *KeywordHandler) Handle(ctx context.Context, event protocol.Event) error {
|
||||
data := event.GetData()
|
||||
rawMessage, ok := data["raw_message"].(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !h.caseSensitive {
|
||||
rawMessage = strings.ToLower(rawMessage)
|
||||
}
|
||||
|
||||
// 执行所有匹配的关键词处理器
|
||||
for keyword, handler := range h.keywords {
|
||||
if strings.Contains(rawMessage, keyword) {
|
||||
h.logger.Info("Keyword matched",
|
||||
zap.String("keyword", keyword))
|
||||
|
||||
if err := handler(ctx, event); err != nil {
|
||||
h.logger.Error("Keyword handler failed",
|
||||
zap.String("keyword", keyword),
|
||||
zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NoticeHandler 通知处理器
|
||||
type NoticeHandler struct {
|
||||
*BaseHandler
|
||||
noticeTypes map[string]func(context.Context, protocol.Event) error
|
||||
}
|
||||
|
||||
// NewNoticeHandler 创建通知处理器
|
||||
func NewNoticeHandler(priority int, logger *zap.Logger) *NoticeHandler {
|
||||
return &NoticeHandler{
|
||||
BaseHandler: NewBaseHandler(priority, logger.Named("handler.notice")),
|
||||
noticeTypes: make(map[string]func(context.Context, protocol.Event) error),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterNoticeType 注册通知类型处理器
|
||||
func (h *NoticeHandler) RegisterNoticeType(noticeType string, handler func(context.Context, protocol.Event) error) {
|
||||
h.noticeTypes[noticeType] = handler
|
||||
h.logger.Debug("Notice type registered", zap.String("notice_type", noticeType))
|
||||
}
|
||||
|
||||
// Match 判断是否匹配事件
|
||||
func (h *NoticeHandler) Match(event protocol.Event) bool {
|
||||
if event.GetType() != protocol.EventTypeNotice {
|
||||
return false
|
||||
}
|
||||
|
||||
detailType := event.GetDetailType()
|
||||
_, exists := h.noticeTypes[detailType]
|
||||
return exists
|
||||
}
|
||||
|
||||
// Handle 处理事件
|
||||
func (h *NoticeHandler) Handle(ctx context.Context, event protocol.Event) error {
|
||||
detailType := event.GetDetailType()
|
||||
handler, exists := h.noticeTypes[detailType]
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
h.logger.Info("Notice event handled",
|
||||
zap.String("notice_type", detailType))
|
||||
|
||||
return handler(ctx, event)
|
||||
}
|
||||
|
||||
// RequestHandler 请求处理器
|
||||
type RequestHandler struct {
|
||||
*BaseHandler
|
||||
requestTypes map[string]func(context.Context, protocol.Event) error
|
||||
}
|
||||
|
||||
// NewRequestHandler 创建请求处理器
|
||||
func NewRequestHandler(priority int, logger *zap.Logger) *RequestHandler {
|
||||
return &RequestHandler{
|
||||
BaseHandler: NewBaseHandler(priority, logger.Named("handler.request")),
|
||||
requestTypes: make(map[string]func(context.Context, protocol.Event) error),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRequestType 注册请求类型处理器
|
||||
func (h *RequestHandler) RegisterRequestType(requestType string, handler func(context.Context, protocol.Event) error) {
|
||||
h.requestTypes[requestType] = handler
|
||||
h.logger.Debug("Request type registered", zap.String("request_type", requestType))
|
||||
}
|
||||
|
||||
// Match 判断是否匹配事件
|
||||
func (h *RequestHandler) Match(event protocol.Event) bool {
|
||||
if event.GetType() != protocol.EventTypeRequest {
|
||||
return false
|
||||
}
|
||||
|
||||
detailType := event.GetDetailType()
|
||||
_, exists := h.requestTypes[detailType]
|
||||
return exists
|
||||
}
|
||||
|
||||
// Handle 处理事件
|
||||
func (h *RequestHandler) Handle(ctx context.Context, event protocol.Event) error {
|
||||
detailType := event.GetDetailType()
|
||||
handler, exists := h.requestTypes[detailType]
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
h.logger.Info("Request event handled",
|
||||
zap.String("request_type", detailType))
|
||||
|
||||
return handler(ctx, event)
|
||||
}
|
||||
Reference in New Issue
Block a user