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:
@@ -2,6 +2,7 @@ package echo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"cellbot/internal/engine"
|
||||
"cellbot/internal/protocol"
|
||||
@@ -10,56 +11,27 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 在 init 函数中注册多个处理函数(类似 ZeroBot 风格)
|
||||
|
||||
// 处理私聊消息(使用 OnlyPrivate 中间件,虽然 OnPrivateMessage 已经匹配私聊,这里作为示例)
|
||||
engine.OnPrivateMessage().
|
||||
Use(engine.OnlyPrivate()). // 只在私聊中响应
|
||||
// 注册 /echo 命令
|
||||
engine.OnCommand("/echo").
|
||||
Handle(func(ctx context.Context, event protocol.Event, botManager *protocol.BotManager, logger *zap.Logger) error {
|
||||
// 获取消息内容
|
||||
data := event.GetData()
|
||||
message, ok := data["message"]
|
||||
rawMessage, ok := data["raw_message"].(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
userID, ok := data["user_id"]
|
||||
if !ok {
|
||||
return nil
|
||||
// 解析命令参数(/echo 后面的内容)
|
||||
parts := strings.Fields(rawMessage)
|
||||
if len(parts) < 2 {
|
||||
// 如果没有参数,返回提示
|
||||
return event.ReplyText(ctx, botManager, logger, "用法: /echo <消息内容>")
|
||||
}
|
||||
|
||||
// 获取 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]
|
||||
}
|
||||
// 获取要回显的内容
|
||||
echoContent := strings.Join(parts[1:], " ")
|
||||
|
||||
// 发送回复
|
||||
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
|
||||
// 使用 ReplyText 发送回复
|
||||
return event.ReplyText(ctx, botManager, logger, echoContent)
|
||||
})
|
||||
|
||||
// 可以继续注册更多处理函数
|
||||
// engine.OnGroupMessage().Handle(...)
|
||||
// engine.OnCommand("help").Handle(...)
|
||||
}
|
||||
|
||||
375
internal/plugins/mcstatus/mcstatus.go
Normal file
375
internal/plugins/mcstatus/mcstatus.go
Normal file
@@ -0,0 +1,375 @@
|
||||
package mcstatus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cellbot/internal/database"
|
||||
"cellbot/internal/engine"
|
||||
"cellbot/internal/protocol"
|
||||
"cellbot/pkg/utils"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ServerBind 服务器绑定模型
|
||||
type ServerBind struct {
|
||||
ID string `gorm:"primaryKey"` // group_id
|
||||
ServerIP string `gorm:"not null"` // 服务器IP
|
||||
}
|
||||
|
||||
// TableName 指定表名(基础表名,实际使用时需要添加 botID 前缀)
|
||||
func (ServerBind) TableName() string {
|
||||
return "mc_server_binds"
|
||||
}
|
||||
|
||||
var dbService database.Database
|
||||
|
||||
func init() {
|
||||
// 注册命令(数据库将在依赖注入时初始化)
|
||||
engine.OnCommand("/mcs").
|
||||
Priority(100).
|
||||
Handle(handleMCSCommand)
|
||||
|
||||
engine.OnCommand("/mcsBind").
|
||||
Priority(100).
|
||||
Handle(handleMCSBindCommand)
|
||||
}
|
||||
|
||||
// InitDatabase 初始化数据库(由依赖注入调用)
|
||||
func InitDatabase(database database.Database) error {
|
||||
dbService = database
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleMCSCommand 处理 /mcs 命令
|
||||
func handleMCSCommand(ctx context.Context, event protocol.Event, botManager *protocol.BotManager, logger *zap.Logger) error {
|
||||
logger.Info("handleMCSCommand called",
|
||||
zap.String("self_id", event.GetSelfID()),
|
||||
zap.String("detail_type", event.GetDetailType()))
|
||||
|
||||
data := event.GetData()
|
||||
|
||||
// 获取原始消息内容
|
||||
rawMessage, ok := data["raw_message"].(string)
|
||||
if !ok {
|
||||
logger.Warn("raw_message not found in event data")
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.Info("Processing /mcs command",
|
||||
zap.String("raw_message", rawMessage))
|
||||
|
||||
// 解析命令参数(/mcs 后面的内容)
|
||||
var serverIP string
|
||||
parts := strings.Fields(rawMessage)
|
||||
if len(parts) > 1 {
|
||||
serverIP = strings.Join(parts[1:], " ")
|
||||
}
|
||||
|
||||
// 如果没有提供 IP,尝试从群绑定或默认配置获取
|
||||
if serverIP == "" {
|
||||
groupID, _ := data["group_id"]
|
||||
botID := event.GetSelfID()
|
||||
if groupID != nil && dbService != nil && botID != "" {
|
||||
db, err := dbService.GetDB(botID)
|
||||
if err != nil {
|
||||
logger.Warn("Failed to get database for bot",
|
||||
zap.String("bot_id", botID),
|
||||
zap.Error(err))
|
||||
} else {
|
||||
var bind ServerBind
|
||||
tableName := database.GetTableName(botID, "mc_server_binds")
|
||||
if err := db.Table(tableName).Where("id = ?", fmt.Sprintf("%v", groupID)).First(&bind).Error; err != nil {
|
||||
logger.Debug("No server bind found for group",
|
||||
zap.String("bot_id", botID),
|
||||
zap.Any("group_id", groupID),
|
||||
zap.Error(err))
|
||||
} else {
|
||||
serverIP = bind.ServerIP
|
||||
}
|
||||
}
|
||||
}
|
||||
// 如果还是没有,使用默认值
|
||||
if serverIP == "" {
|
||||
serverIP = "mc.hypixel.net" // 默认服务器
|
||||
}
|
||||
}
|
||||
|
||||
// 解析服务器地址和端口
|
||||
host := serverIP
|
||||
port := 25565
|
||||
if strings.Contains(serverIP, ":") {
|
||||
parts := strings.Split(serverIP, ":")
|
||||
host = parts[0]
|
||||
fmt.Sscanf(parts[1], "%d", &port)
|
||||
}
|
||||
|
||||
logger.Info("Querying Minecraft server",
|
||||
zap.String("host", host),
|
||||
zap.Int("port", port))
|
||||
|
||||
// 查询服务器状态
|
||||
status, err := Ping(host, port, 10*time.Second)
|
||||
if err != nil {
|
||||
logger.Error("Failed to ping server", zap.Error(err))
|
||||
|
||||
// 使用 ReplyText 发送错误消息
|
||||
errorMsg := fmt.Sprintf("查询失败: %v", err)
|
||||
event.ReplyText(ctx, botManager, logger, errorMsg)
|
||||
return err
|
||||
}
|
||||
|
||||
// 构建 HTML 模板
|
||||
htmlTemplate := buildStatusHTML(status)
|
||||
|
||||
// 配置截图选项
|
||||
opts := &utils.ScreenshotOptions{
|
||||
Width: 1200,
|
||||
Height: 800,
|
||||
Timeout: 60 * time.Second,
|
||||
WaitTime: 3 * time.Second,
|
||||
FullPage: false,
|
||||
Quality: 90,
|
||||
Logger: logger,
|
||||
}
|
||||
|
||||
// 使用独立的 context 进行截图,避免受 dispatcher context 影响
|
||||
// 如果 dispatcher context 被取消,截图操作仍能完成
|
||||
screenshotCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// 渲染并截图
|
||||
chain, err := utils.ScreenshotHTMLToMessageChain(screenshotCtx, htmlTemplate, opts)
|
||||
if err != nil {
|
||||
logger.Error("Failed to render status image", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
// 使用 Reply 发送图片
|
||||
err = event.Reply(ctx, botManager, logger, chain)
|
||||
if err != nil {
|
||||
logger.Error("Failed to send status image", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("Minecraft server status sent",
|
||||
zap.String("host", host),
|
||||
zap.Int("port", port))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleMCSBindCommand 处理 /mcsBind 命令
|
||||
func handleMCSBindCommand(ctx context.Context, event protocol.Event, botManager *protocol.BotManager, logger *zap.Logger) error {
|
||||
data := event.GetData()
|
||||
|
||||
// 只允许群聊使用
|
||||
if event.GetDetailType() != "group" {
|
||||
return event.ReplyText(ctx, botManager, logger, "此命令只能在群聊中使用")
|
||||
}
|
||||
|
||||
// 获取原始消息内容
|
||||
rawMessage, ok := data["raw_message"].(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 解析命令参数(/mcsBind 后面的内容)
|
||||
parts := strings.Fields(rawMessage)
|
||||
if len(parts) < 2 {
|
||||
groupID, _ := data["group_id"]
|
||||
errorMsg := protocol.NewMessageChain(
|
||||
protocol.NewTextSegment("用法: /mcsBind <服务器IP>"),
|
||||
)
|
||||
|
||||
action := &protocol.BaseAction{
|
||||
Type: protocol.ActionTypeSendGroupMessage,
|
||||
Params: map[string]interface{}{
|
||||
"group_id": groupID,
|
||||
"message": errorMsg,
|
||||
},
|
||||
}
|
||||
|
||||
selfID := event.GetSelfID()
|
||||
bot, ok := botManager.Get(selfID)
|
||||
if !ok {
|
||||
bots := botManager.GetAll()
|
||||
if len(bots) > 0 {
|
||||
bot = bots[0]
|
||||
}
|
||||
}
|
||||
if bot != nil {
|
||||
bot.SendAction(ctx, action)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
serverIP := strings.Join(parts[1:], " ")
|
||||
groupID, _ := data["group_id"]
|
||||
groupIDStr := fmt.Sprintf("%v", groupID)
|
||||
botID := event.GetSelfID()
|
||||
|
||||
// 保存绑定到数据库
|
||||
if dbService != nil && botID != "" {
|
||||
db, err := dbService.GetDB(botID)
|
||||
if err == nil {
|
||||
// 自动迁移表(如果不存在)
|
||||
tableName := database.GetTableName(botID, "mc_server_binds")
|
||||
if err := db.Table(tableName).AutoMigrate(&ServerBind{}); err != nil {
|
||||
logger.Error("Failed to migrate table", zap.Error(err))
|
||||
} else {
|
||||
bind := ServerBind{
|
||||
ID: groupIDStr,
|
||||
ServerIP: serverIP,
|
||||
}
|
||||
// 使用 Save 方法,如果存在则更新,不存在则创建
|
||||
if err := db.Table(tableName).Save(&bind).Error; err != nil {
|
||||
logger.Error("Failed to save server bind", zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 使用 ReplyText 发送确认消息
|
||||
successMsg := fmt.Sprintf("已绑定服务器 %s 到本群", serverIP)
|
||||
err := event.ReplyText(ctx, botManager, logger, successMsg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("Minecraft server bound",
|
||||
zap.String("group_id", groupIDStr),
|
||||
zap.String("server_ip", serverIP))
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildStatusHTML 构建服务器状态 HTML
|
||||
func buildStatusHTML(status *ServerStatus) string {
|
||||
iconHTML := ""
|
||||
if status.Favicon != "" {
|
||||
iconHTML = fmt.Sprintf(`<img src="data:image/png;base64,%s" alt="icon" style="width: 64px; height: 64px; border-radius: 8px;" />`, status.Favicon)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
||||
background: linear-gradient(135deg, #2e3440 0%%, #434c5e 100%%);
|
||||
padding: 40px 20px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
color: #cdd6f4;
|
||||
}
|
||||
.container {
|
||||
background: #2e3440;
|
||||
border-radius: 20px;
|
||||
padding: 40px;
|
||||
max-width: 800px;
|
||||
width: 100%%;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
.header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 20px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.icon {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.title {
|
||||
font-size: 28px;
|
||||
font-weight: bold;
|
||||
color: #cdd6f4;
|
||||
}
|
||||
.info-section {
|
||||
background: #434c5e;
|
||||
border-radius: 10px;
|
||||
padding: 20px;
|
||||
margin: 20px 0;
|
||||
}
|
||||
.info-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
padding: 10px 0;
|
||||
border-bottom: 1px solid #3b4252;
|
||||
}
|
||||
.info-item:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
.info-label {
|
||||
font-weight: 600;
|
||||
color: #81a1c1;
|
||||
}
|
||||
.info-value {
|
||||
color: #cdd6f4;
|
||||
}
|
||||
.description {
|
||||
background: #3b4252;
|
||||
border-radius: 8px;
|
||||
padding: 15px;
|
||||
margin: 20px 0;
|
||||
color: #cdd6f4;
|
||||
font-size: 16px;
|
||||
}
|
||||
.footer {
|
||||
text-align: center;
|
||||
margin-top: 30px;
|
||||
color: #81a1c1;
|
||||
font-size: 14px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
%s
|
||||
<div class="title">Minecraft 服务器状态</div>
|
||||
</div>
|
||||
|
||||
<div class="info-section">
|
||||
<div class="info-item">
|
||||
<span class="info-label">服务器地址:</span>
|
||||
<span class="info-value">%s</span>
|
||||
</div>
|
||||
<div class="info-item">
|
||||
<span class="info-label">延迟:</span>
|
||||
<span class="info-value">%d ms</span>
|
||||
</div>
|
||||
<div class="info-item">
|
||||
<span class="info-label">版本:</span>
|
||||
<span class="info-value">%s (协议 %d)</span>
|
||||
</div>
|
||||
<div class="info-item">
|
||||
<span class="info-label">在线人数:</span>
|
||||
<span class="info-value">%d / %d</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="description">
|
||||
<strong>服务器描述:</strong><br>
|
||||
%s
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
Generated by mc-server-status
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`, iconHTML, fmt.Sprintf("%s:%d", status.Host, status.Port), status.Latency, status.Version.Name, status.Version.Protocol, status.Players.Online, status.Players.Max, status.Description)
|
||||
}
|
||||
141
internal/plugins/mcstatus/ping.go
Normal file
141
internal/plugins/mcstatus/ping.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package mcstatus
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mcnet "github.com/Tnze/go-mc/net"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
// ServerStatus 服务器状态信息
|
||||
type ServerStatus struct {
|
||||
Host string
|
||||
Port int
|
||||
Latency int64 // 延迟(毫秒)
|
||||
Version Version
|
||||
Players Players
|
||||
Description string // MOTD
|
||||
Favicon string // Base64 编码的图标
|
||||
}
|
||||
|
||||
// Version 版本信息
|
||||
type Version struct {
|
||||
Name string
|
||||
Protocol int
|
||||
}
|
||||
|
||||
// Players 玩家信息
|
||||
type Players struct {
|
||||
Online int
|
||||
Max int
|
||||
}
|
||||
|
||||
// Ping 查询 Minecraft 服务器状态
|
||||
func Ping(host string, port int, timeout time.Duration) (*ServerStatus, error) {
|
||||
// 连接服务器
|
||||
startTime := time.Now()
|
||||
conn, err := mcnet.DialMC(fmt.Sprintf("%s:%d", host, port))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect: %w", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
latency := time.Since(startTime).Milliseconds()
|
||||
|
||||
// 发送握手包
|
||||
handshake := pk.Marshal(
|
||||
0x00, // Handshake packet ID
|
||||
pk.VarInt(47), // Protocol version (1.8+)
|
||||
pk.String(host),
|
||||
pk.UnsignedShort(port),
|
||||
pk.VarInt(1), // Next state: Status
|
||||
)
|
||||
if err := conn.WritePacket(handshake); err != nil {
|
||||
return nil, fmt.Errorf("failed to send handshake: %w", err)
|
||||
}
|
||||
|
||||
// 发送状态请求
|
||||
request := pk.Marshal(0x00) // Status request packet ID
|
||||
if err := conn.WritePacket(request); err != nil {
|
||||
return nil, fmt.Errorf("failed to send status request: %w", err)
|
||||
}
|
||||
|
||||
// 读取响应
|
||||
var response pk.Packet
|
||||
if err := conn.ReadPacket(&response); err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
// 解析响应
|
||||
var statusJSON pk.String
|
||||
if err := response.Scan(&statusJSON); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
// 解析 JSON 响应
|
||||
var statusData map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(statusJSON), &statusData); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse JSON: %w", err)
|
||||
}
|
||||
|
||||
status := &ServerStatus{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Latency: latency,
|
||||
}
|
||||
|
||||
// 解析 description
|
||||
if desc, ok := statusData["description"].(map[string]interface{}); ok {
|
||||
if text, ok := desc["text"].(string); ok {
|
||||
status.Description = text
|
||||
} else if text, ok := desc["extra"].([]interface{}); ok && len(text) > 0 {
|
||||
// 处理 extra 数组
|
||||
var descText strings.Builder
|
||||
for _, item := range text {
|
||||
if itemMap, ok := item.(map[string]interface{}); ok {
|
||||
if text, ok := itemMap["text"].(string); ok {
|
||||
descText.WriteString(text)
|
||||
}
|
||||
}
|
||||
}
|
||||
status.Description = descText.String()
|
||||
}
|
||||
} else if desc, ok := statusData["description"].(string); ok {
|
||||
status.Description = desc
|
||||
}
|
||||
|
||||
// 解析 version
|
||||
if version, ok := statusData["version"].(map[string]interface{}); ok {
|
||||
if name, ok := version["name"].(string); ok {
|
||||
status.Version.Name = name
|
||||
}
|
||||
if protocol, ok := version["protocol"].(float64); ok {
|
||||
status.Version.Protocol = int(protocol)
|
||||
}
|
||||
}
|
||||
|
||||
// 解析 players
|
||||
if players, ok := statusData["players"].(map[string]interface{}); ok {
|
||||
if online, ok := players["online"].(float64); ok {
|
||||
status.Players.Online = int(online)
|
||||
}
|
||||
if max, ok := players["max"].(float64); ok {
|
||||
status.Players.Max = int(max)
|
||||
}
|
||||
}
|
||||
|
||||
// 解析 favicon
|
||||
if favicon, ok := statusData["favicon"].(string); ok {
|
||||
// 移除 data:image/png;base64, 前缀
|
||||
if strings.HasPrefix(favicon, "data:image/png;base64,") {
|
||||
status.Favicon = strings.TrimPrefix(favicon, "data:image/png;base64,")
|
||||
} else {
|
||||
status.Favicon = favicon
|
||||
}
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
@@ -317,8 +317,8 @@ func buildWelcomeMessage(ctx context.Context, userID, operatorID interface{}, su
|
||||
|
||||
// 配置截图选项
|
||||
opts := &utils.ScreenshotOptions{
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Width: 1200, // 增加宽度,确保内容有足够空间
|
||||
Height: 800, // 增加高度,确保内容完整显示
|
||||
Timeout: 60 * time.Second, // 增加超时时间到60秒
|
||||
WaitTime: 3 * time.Second, // 增加等待时间,确保页面完全加载
|
||||
FullPage: false,
|
||||
|
||||
Reference in New Issue
Block a user