chore: update project structure and enhance plugin functionality
- Added new entries to .gitignore for database files. - Updated go.mod and go.sum to include new indirect dependencies for database and ORM support. - Refactored event handling to improve message reply functionality in the protocol. - Enhanced the dispatcher to allow for better event processing and logging. - Removed outdated plugin documentation and unnecessary files to streamline the codebase. - Improved welcome message formatting and screenshot options for better user experience.
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
package protocol
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// EventType 事件类型
|
||||
type EventType string
|
||||
|
||||
const (
|
||||
// 事件类型常量
|
||||
EventTypeMessage EventType = "message"
|
||||
EventTypeNotice EventType = "notice"
|
||||
EventTypeRequest EventType = "request"
|
||||
EventTypeMeta EventType = "meta"
|
||||
EventTypeMessageSent EventType = "message_sent"
|
||||
EventTypeNoticeSent EventType = "notice_sent"
|
||||
EventTypeRequestSent EventType = "request_sent"
|
||||
EventTypeMessage EventType = "message"
|
||||
EventTypeNotice EventType = "notice"
|
||||
EventTypeRequest EventType = "request"
|
||||
EventTypeMeta EventType = "meta"
|
||||
EventTypeMessageSent EventType = "message_sent"
|
||||
EventTypeNoticeSent EventType = "notice_sent"
|
||||
EventTypeRequestSent EventType = "request_sent"
|
||||
)
|
||||
|
||||
// Event 通用事件接口
|
||||
@@ -31,6 +36,10 @@ type Event interface {
|
||||
GetSelfID() string
|
||||
// GetData 获取事件数据
|
||||
GetData() map[string]interface{}
|
||||
// Reply 在消息发生的群/私聊进行回复
|
||||
Reply(ctx context.Context, botManager *BotManager, logger *zap.Logger, message MessageChain) error
|
||||
// ReplyText 在消息发生的群/私聊进行文本回复(便捷方法)
|
||||
ReplyText(ctx context.Context, botManager *BotManager, logger *zap.Logger, text string) error
|
||||
}
|
||||
|
||||
// BaseEvent 基础事件结构
|
||||
@@ -73,6 +82,70 @@ func (e *BaseEvent) GetData() map[string]interface{} {
|
||||
return e.Data
|
||||
}
|
||||
|
||||
// Reply 在消息发生的群/私聊进行回复
|
||||
func (e *BaseEvent) Reply(ctx context.Context, botManager *BotManager, logger *zap.Logger, message MessageChain) error {
|
||||
data := e.GetData()
|
||||
userID, _ := data["user_id"]
|
||||
groupID, _ := data["group_id"]
|
||||
|
||||
// 获取 bot 实例
|
||||
selfID := e.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]
|
||||
}
|
||||
|
||||
// 根据消息类型发送回复
|
||||
var action *BaseAction
|
||||
if e.GetDetailType() == "private" {
|
||||
action = &BaseAction{
|
||||
Type: ActionTypeSendPrivateMessage,
|
||||
Params: map[string]interface{}{
|
||||
"user_id": userID,
|
||||
"message": message,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
// 群聊或其他有 group_id 的事件
|
||||
action = &BaseAction{
|
||||
Type: ActionTypeSendGroupMessage,
|
||||
Params: map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"message": message,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
_, err := bot.SendAction(ctx, action)
|
||||
if err != nil {
|
||||
logger.Error("Failed to send reply",
|
||||
zap.Any("user_id", userID),
|
||||
zap.Any("group_id", groupID),
|
||||
zap.String("detail_type", e.GetDetailType()),
|
||||
zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Debug("Reply sent",
|
||||
zap.Any("user_id", userID),
|
||||
zap.Any("group_id", groupID),
|
||||
zap.String("detail_type", e.GetDetailType()))
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReplyText 在消息发生的群/私聊进行文本回复(便捷方法)
|
||||
func (e *BaseEvent) ReplyText(ctx context.Context, botManager *BotManager, logger *zap.Logger, text string) error {
|
||||
message := NewMessageChain(
|
||||
NewTextSegment(text),
|
||||
)
|
||||
return e.Reply(ctx, botManager, logger, message)
|
||||
}
|
||||
|
||||
// MessageEvent 消息事件
|
||||
type MessageEvent struct {
|
||||
BaseEvent
|
||||
|
||||
Reference in New Issue
Block a user