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,13 +2,17 @@ package di
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"cellbot/internal/adapter/milky"
|
||||
"cellbot/internal/adapter/onebot11"
|
||||
"cellbot/internal/config"
|
||||
"cellbot/internal/database"
|
||||
"cellbot/internal/engine"
|
||||
_ "cellbot/internal/plugins/echo" // 导入插件以触发 init 函数
|
||||
_ "cellbot/internal/plugins/welcome" // 导入插件以触发 init 函数
|
||||
_ "cellbot/internal/plugins/echo" // 导入插件以触发 init 函数
|
||||
"cellbot/internal/plugins/mcstatus"
|
||||
_ "cellbot/internal/plugins/mcstatus" // 导入插件以触发 init 函数
|
||||
_ "cellbot/internal/plugins/welcome" // 导入插件以触发 init 函数
|
||||
"cellbot/internal/protocol"
|
||||
"cellbot/pkg/net"
|
||||
|
||||
@@ -82,6 +86,52 @@ func ProvideServer(cfg *config.Config, logger *zap.Logger, botManager *protocol.
|
||||
return net.NewServer(cfg.Server.Host, cfg.Server.Port, logger, botManager, eventBus)
|
||||
}
|
||||
|
||||
// ProvideDatabase 提供数据库服务
|
||||
func ProvideDatabase(logger *zap.Logger) database.Database {
|
||||
return database.NewSQLiteDatabase(logger, "data/cellbot.db")
|
||||
}
|
||||
|
||||
// InitMCStatusDatabase 初始化 MC 状态插件的数据库
|
||||
func InitMCStatusDatabase(dbService database.Database, logger *zap.Logger, botManager *protocol.BotManager) error {
|
||||
// 为每个 bot 初始化数据库表
|
||||
bots := botManager.GetAll()
|
||||
for _, bot := range bots {
|
||||
botID := bot.GetID()
|
||||
db, err := dbService.GetDB(botID)
|
||||
if err != nil {
|
||||
logger.Error("Failed to get database for bot",
|
||||
zap.String("bot_id", botID),
|
||||
zap.Error(err))
|
||||
continue
|
||||
}
|
||||
|
||||
// 创建表(使用原始 SQL 避免循环依赖)
|
||||
// 注意:虽然使用 fmt.Sprintf,但 tableName 已经通过 sanitizeTableName 清理过,相对安全
|
||||
tableName := database.GetTableName(botID, "mc_server_binds")
|
||||
// 使用参数化查询更安全,但 SQLite 的 CREATE TABLE 不支持参数化表名
|
||||
// 所以这里使用清理过的表名是合理的
|
||||
if err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id TEXT PRIMARY KEY,
|
||||
server_ip TEXT NOT NULL
|
||||
)
|
||||
`, tableName)).Error; err != nil {
|
||||
logger.Error("Failed to create table",
|
||||
zap.String("bot_id", botID),
|
||||
zap.String("table", tableName),
|
||||
zap.Error(err))
|
||||
} else {
|
||||
logger.Info("Database table initialized",
|
||||
zap.String("bot_id", botID),
|
||||
zap.String("table", tableName))
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化插件数据库
|
||||
mcstatus.InitDatabase(dbService)
|
||||
return nil
|
||||
}
|
||||
|
||||
func ProvideMilkyBots(cfg *config.Config, logger *zap.Logger, eventBus *engine.EventBus, wsManager *net.WebSocketManager, botManager *protocol.BotManager, lc fx.Lifecycle) error {
|
||||
for _, botCfg := range cfg.Bots {
|
||||
if botCfg.Protocol == "milky" && botCfg.Enabled {
|
||||
@@ -214,10 +264,12 @@ var Providers = fx.Options(
|
||||
ProvidePluginRegistry,
|
||||
ProvideBotManager,
|
||||
ProvideWebSocketManager,
|
||||
ProvideDatabase,
|
||||
ProvideServer,
|
||||
),
|
||||
fx.Invoke(ProvideMilkyBots),
|
||||
fx.Invoke(ProvideOneBot11Bots),
|
||||
fx.Invoke(LoadPlugins),
|
||||
fx.Invoke(LoadScheduledJobs),
|
||||
fx.Invoke(InitMCStatusDatabase),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user