feat: add rate limiting and improve event handling
- Introduced rate limiting configuration in config.toml with options for enabling, requests per second (RPS), and burst capacity. - Enhanced event handling in the OneBot11 adapter to ignore messages sent by the bot itself. - Updated the dispatcher to register rate limit middleware based on configuration settings. - Refactored WebSocket message handling to support flexible JSON parsing and improved event type detection. - Removed deprecated echo plugin and associated tests to streamline the codebase.
This commit is contained in:
64
internal/plugins/echo/echo_new.go
Normal file
64
internal/plugins/echo/echo_new.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package echo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cellbot/internal/engine"
|
||||
"cellbot/internal/protocol"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 在 init 函数中注册多个处理函数(类似 ZeroBot 风格)
|
||||
|
||||
// 处理私聊消息
|
||||
engine.OnPrivateMessage().
|
||||
Handle(func(ctx context.Context, event protocol.Event, botManager *protocol.BotManager, logger *zap.Logger) error {
|
||||
// 获取消息内容
|
||||
data := event.GetData()
|
||||
message, ok := data["message"]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
userID, ok := data["user_id"]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取 bot 实例
|
||||
selfID := event.GetSelfID()
|
||||
bot, ok := botManager.Get(selfID)
|
||||
if !ok {
|
||||
bots := botManager.GetAll()
|
||||
if len(bots) == 0 {
|
||||
logger.Error("No bot instance available")
|
||||
return nil
|
||||
}
|
||||
bot = bots[0]
|
||||
}
|
||||
|
||||
// 发送回复
|
||||
action := &protocol.BaseAction{
|
||||
Type: protocol.ActionTypeSendPrivateMessage,
|
||||
Params: map[string]interface{}{
|
||||
"user_id": userID,
|
||||
"message": message,
|
||||
},
|
||||
}
|
||||
|
||||
_, err := bot.SendAction(ctx, action)
|
||||
if err != nil {
|
||||
logger.Error("Failed to send reply", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("Echo reply sent", zap.Any("user_id", userID))
|
||||
return nil
|
||||
})
|
||||
|
||||
// 可以继续注册更多处理函数
|
||||
// engine.OnGroupMessage().Handle(...)
|
||||
// engine.OnCommand("help").Handle(...)
|
||||
}
|
||||
Reference in New Issue
Block a user