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:
@@ -3,6 +3,7 @@ package engine
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
@@ -208,11 +209,16 @@ var (
|
||||
// HandlerFunc 处理函数类型(支持依赖注入)
|
||||
type HandlerFunc func(ctx context.Context, event protocol.Event, botManager *protocol.BotManager, logger *zap.Logger) error
|
||||
|
||||
// HandlerMiddleware 处理器中间件函数类型
|
||||
// 返回 true 表示通过中间件检查,false 表示不通过
|
||||
type HandlerMiddleware func(event protocol.Event) bool
|
||||
|
||||
// HandlerBuilder 处理器构建器(类似 ZeroBot 的 API)
|
||||
type HandlerBuilder struct {
|
||||
matchFunc func(protocol.Event) bool
|
||||
priority int
|
||||
handleFunc HandlerFunc
|
||||
matchFunc func(protocol.Event) bool
|
||||
priority int
|
||||
handleFunc HandlerFunc
|
||||
middlewares []HandlerMiddleware
|
||||
}
|
||||
|
||||
// OnPrivateMessage 匹配私聊消息
|
||||
@@ -246,20 +252,82 @@ func OnMessage() *HandlerBuilder {
|
||||
}
|
||||
|
||||
// OnNotice 匹配通知事件
|
||||
func OnNotice() *HandlerBuilder {
|
||||
// 用法:
|
||||
//
|
||||
// OnNotice() - 匹配所有通知事件
|
||||
// OnNotice("group_increase") - 匹配群成员增加事件
|
||||
// OnNotice("group_increase", "group_decrease") - 匹配群成员增加或减少事件
|
||||
func OnNotice(detailTypes ...string) *HandlerBuilder {
|
||||
return &HandlerBuilder{
|
||||
matchFunc: func(event protocol.Event) bool {
|
||||
return event.GetType() == protocol.EventTypeNotice
|
||||
if event.GetType() != protocol.EventTypeNotice {
|
||||
return false
|
||||
}
|
||||
// 如果没有指定类型,匹配所有通知事件
|
||||
if len(detailTypes) == 0 {
|
||||
return true
|
||||
}
|
||||
// 检查 detail_type 是否在指定列表中
|
||||
eventDetailType := event.GetDetailType()
|
||||
for _, dt := range detailTypes {
|
||||
if dt == eventDetailType {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
priority: 100,
|
||||
}
|
||||
}
|
||||
|
||||
// OnRequest 匹配请求事件
|
||||
func OnRequest() *HandlerBuilder {
|
||||
// 用法:
|
||||
//
|
||||
// OnRequest() - 匹配所有请求事件
|
||||
// OnRequest("friend") - 匹配好友请求事件
|
||||
// OnRequest("friend", "group") - 匹配好友或群请求事件
|
||||
func OnRequest(detailTypes ...string) *HandlerBuilder {
|
||||
return &HandlerBuilder{
|
||||
matchFunc: func(event protocol.Event) bool {
|
||||
return event.GetType() == protocol.EventTypeRequest
|
||||
if event.GetType() != protocol.EventTypeRequest {
|
||||
return false
|
||||
}
|
||||
// 如果没有指定类型,匹配所有请求事件
|
||||
if len(detailTypes) == 0 {
|
||||
return true
|
||||
}
|
||||
// 检查 detail_type 是否在指定列表中
|
||||
eventDetailType := event.GetDetailType()
|
||||
for _, dt := range detailTypes {
|
||||
if dt == eventDetailType {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
priority: 100,
|
||||
}
|
||||
}
|
||||
|
||||
// OnEvent 匹配指定类型的事件(可传一个或多个 EventType)
|
||||
// 用法:
|
||||
//
|
||||
// OnEvent() - 匹配所有事件
|
||||
// OnEvent(protocol.EventTypeMessage) - 匹配消息事件
|
||||
// OnEvent(protocol.EventTypeMessage, protocol.EventTypeNotice) - 匹配消息和通知事件
|
||||
func OnEvent(eventTypes ...protocol.EventType) *HandlerBuilder {
|
||||
return &HandlerBuilder{
|
||||
matchFunc: func(event protocol.Event) bool {
|
||||
if len(eventTypes) == 0 {
|
||||
return true // 不传参数时匹配所有事件
|
||||
}
|
||||
// 检查事件类型是否在指定列表中
|
||||
for _, et := range eventTypes {
|
||||
if event.GetType() == et {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
priority: 100,
|
||||
}
|
||||
@@ -273,8 +341,13 @@ func On(matchFunc func(protocol.Event) bool) *HandlerBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
// OnCommand 匹配命令(以指定前缀开头的消息)
|
||||
func OnCommand(prefix string) *HandlerBuilder {
|
||||
// OnCommand 匹配命令
|
||||
// 用法:
|
||||
//
|
||||
// OnCommand("/help") - 匹配 /help 命令(前缀为 /,命令为 help)
|
||||
// OnCommand("/", "help") - 匹配以 / 开头且命令为 help 的消息
|
||||
// OnCommand("/", "help", "h") - 匹配以 / 开头且命令为 help 或 h 的消息
|
||||
func OnCommand(prefix string, commands ...string) *HandlerBuilder {
|
||||
return &HandlerBuilder{
|
||||
matchFunc: func(event protocol.Event) bool {
|
||||
if event.GetType() != protocol.EventTypeMessage {
|
||||
@@ -285,9 +358,35 @@ func OnCommand(prefix string) *HandlerBuilder {
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
// 检查是否以命令前缀开头
|
||||
if len(rawMessage) > 0 && len(prefix) > 0 {
|
||||
return len(rawMessage) >= len(prefix) && rawMessage[:len(prefix)] == prefix
|
||||
|
||||
// 检查是否以前缀开头
|
||||
if len(rawMessage) < len(prefix) || rawMessage[:len(prefix)] != prefix {
|
||||
return false
|
||||
}
|
||||
|
||||
// 如果没有指定具体命令,匹配所有以该前缀开头的消息
|
||||
if len(commands) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
// 提取命令部分(去除前缀和空格)
|
||||
cmdText := strings.TrimSpace(rawMessage[len(prefix):])
|
||||
if cmdText == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// 获取第一个单词作为命令
|
||||
parts := strings.Fields(cmdText)
|
||||
if len(parts) == 0 {
|
||||
return false
|
||||
}
|
||||
cmd := parts[0]
|
||||
|
||||
// 检查是否匹配指定的命令
|
||||
for _, c := range commands {
|
||||
if cmd == c {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
@@ -364,12 +463,56 @@ func contains(s, substr string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// OnFullMatch 完全匹配文本
|
||||
func OnFullMatch(text string) *HandlerBuilder {
|
||||
return &HandlerBuilder{
|
||||
matchFunc: func(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 rawMessage == text
|
||||
},
|
||||
priority: 100,
|
||||
}
|
||||
}
|
||||
|
||||
// OnDetailType 匹配指定 detail_type 的事件
|
||||
func OnDetailType(detailType string) *HandlerBuilder {
|
||||
return &HandlerBuilder{
|
||||
matchFunc: func(event protocol.Event) bool {
|
||||
return event.GetDetailType() == detailType
|
||||
},
|
||||
priority: 100,
|
||||
}
|
||||
}
|
||||
|
||||
// OnSubType 匹配指定 sub_type 的事件
|
||||
func OnSubType(subType string) *HandlerBuilder {
|
||||
return &HandlerBuilder{
|
||||
matchFunc: func(event protocol.Event) bool {
|
||||
return event.GetSubType() == subType
|
||||
},
|
||||
priority: 100,
|
||||
}
|
||||
}
|
||||
|
||||
// Priority 设置优先级
|
||||
func (b *HandlerBuilder) Priority(priority int) *HandlerBuilder {
|
||||
b.priority = priority
|
||||
return b
|
||||
}
|
||||
|
||||
// Use 添加中间件(链式调用)
|
||||
func (b *HandlerBuilder) Use(middleware HandlerMiddleware) *HandlerBuilder {
|
||||
b.middlewares = append(b.middlewares, middleware)
|
||||
return b
|
||||
}
|
||||
|
||||
// Handle 注册处理函数(在 init 中调用)
|
||||
func (b *HandlerBuilder) Handle(handleFunc HandlerFunc) {
|
||||
globalHandlerMu.Lock()
|
||||
@@ -379,6 +522,16 @@ func (b *HandlerBuilder) Handle(handleFunc HandlerFunc) {
|
||||
globalHandlerRegistry = append(globalHandlerRegistry, b)
|
||||
}
|
||||
|
||||
// applyMiddlewares 应用所有中间件
|
||||
func (b *HandlerBuilder) applyMiddlewares(event protocol.Event) bool {
|
||||
for _, middleware := range b.middlewares {
|
||||
if !middleware(event) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// generateHandlerName 生成处理器名称
|
||||
var handlerCounter int64
|
||||
|
||||
@@ -406,11 +559,21 @@ func LoadAllHandlers(botManager *protocol.BotManager, logger *zap.Logger) []prot
|
||||
return builder.handleFunc(ctx, event, botManager, logger)
|
||||
}
|
||||
|
||||
// 创建包装的匹配函数,应用中间件
|
||||
matchFunc := func(event protocol.Event) bool {
|
||||
// 先检查基础匹配
|
||||
if builder.matchFunc != nil && !builder.matchFunc(event) {
|
||||
return false
|
||||
}
|
||||
// 再应用中间件
|
||||
return builder.applyMiddlewares(event)
|
||||
}
|
||||
|
||||
handler := &simplePlugin{
|
||||
name: pluginName,
|
||||
description: "Handler registered via OnXXX().Handle()",
|
||||
priority: builder.priority,
|
||||
matchFunc: builder.matchFunc,
|
||||
matchFunc: matchFunc,
|
||||
handleFunc: handleFunc,
|
||||
}
|
||||
|
||||
@@ -419,3 +582,129 @@ func LoadAllHandlers(botManager *protocol.BotManager, logger *zap.Logger) []prot
|
||||
|
||||
return handlers
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 常用中间件(类似 NoneBot 风格)
|
||||
// ============================================================================
|
||||
|
||||
// OnlyToMe 只响应@机器人的消息(群聊中)
|
||||
func OnlyToMe() HandlerMiddleware {
|
||||
return func(event protocol.Event) bool {
|
||||
// 只对群消息生效
|
||||
if event.GetType() != protocol.EventTypeMessage || event.GetDetailType() != "group" {
|
||||
return true // 非群消息不检查,让其他中间件处理
|
||||
}
|
||||
|
||||
data := event.GetData()
|
||||
selfID := event.GetSelfID()
|
||||
|
||||
// 检查消息段中是否包含@机器人的消息
|
||||
if segments, ok := data["message_segments"].([]interface{}); ok {
|
||||
for _, seg := range segments {
|
||||
if segMap, ok := seg.(map[string]interface{}); ok {
|
||||
segType, _ := segMap["type"].(string)
|
||||
if segType == "at" || segType == "mention" {
|
||||
segData, _ := segMap["data"].(map[string]interface{})
|
||||
// 检查是否@了机器人
|
||||
if userID, ok := segData["user_id"]; ok {
|
||||
if userIDStr := fmt.Sprintf("%v", userID); userIDStr == selfID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if qq, ok := segData["qq"]; ok {
|
||||
if qqStr := fmt.Sprintf("%v", qq); qqStr == selfID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查 raw_message 中是否包含@机器人的信息(兼容性检查)
|
||||
if rawMessage, ok := data["raw_message"].(string); ok {
|
||||
// 简单的检查:消息是否以 @机器人 开头
|
||||
// 注意:这里需要根据实际协议调整
|
||||
if strings.Contains(rawMessage, fmt.Sprintf("[CQ:at,qq=%s]", selfID)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// OnlyPrivate 只在私聊中响应
|
||||
func OnlyPrivate() HandlerMiddleware {
|
||||
return func(event protocol.Event) bool {
|
||||
return event.GetType() == protocol.EventTypeMessage && event.GetDetailType() == "private"
|
||||
}
|
||||
}
|
||||
|
||||
// OnlyGroup 只在群聊中响应(消息事件)或群相关事件(通知/请求事件)
|
||||
func OnlyGroup() HandlerMiddleware {
|
||||
return func(event protocol.Event) bool {
|
||||
// 消息事件:检查 detail_type
|
||||
if event.GetType() == protocol.EventTypeMessage {
|
||||
return event.GetDetailType() == "group"
|
||||
}
|
||||
// 通知/请求事件:检查是否有 group_id
|
||||
data := event.GetData()
|
||||
_, hasGroupID := data["group_id"]
|
||||
return hasGroupID
|
||||
}
|
||||
}
|
||||
|
||||
// OnlySuperuser 只允许超级用户(需要从配置或数据中获取)
|
||||
func OnlySuperuser(superusers []string) HandlerMiddleware {
|
||||
return func(event protocol.Event) bool {
|
||||
data := event.GetData()
|
||||
userID, ok := data["user_id"]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
userIDStr := fmt.Sprintf("%v", userID)
|
||||
for _, su := range superusers {
|
||||
if su == userIDStr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// BlockPrivate 阻止私聊消息
|
||||
func BlockPrivate() HandlerMiddleware {
|
||||
return func(event protocol.Event) bool {
|
||||
return !(event.GetType() == protocol.EventTypeMessage && event.GetDetailType() == "private")
|
||||
}
|
||||
}
|
||||
|
||||
// BlockGroup 阻止群聊消息
|
||||
func BlockGroup() HandlerMiddleware {
|
||||
return func(event protocol.Event) bool {
|
||||
// 消息事件:检查 detail_type
|
||||
if event.GetType() == protocol.EventTypeMessage {
|
||||
return event.GetDetailType() != "group"
|
||||
}
|
||||
// 通知/请求事件:检查是否有 group_id
|
||||
data := event.GetData()
|
||||
_, hasGroupID := data["group_id"]
|
||||
return !hasGroupID
|
||||
}
|
||||
}
|
||||
|
||||
// OnlyDetailType 只匹配指定的 detail_type
|
||||
func OnlyDetailType(detailType string) HandlerMiddleware {
|
||||
return func(event protocol.Event) bool {
|
||||
return event.GetDetailType() == detailType
|
||||
}
|
||||
}
|
||||
|
||||
// OnlySubType 只匹配指定的 sub_type
|
||||
func OnlySubType(subType string) HandlerMiddleware {
|
||||
return func(event protocol.Event) bool {
|
||||
return event.GetSubType() == subType
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user