diff --git a/cmds/tts.go b/cmds/tts.go deleted file mode 100644 index 38bbfb8..0000000 --- a/cmds/tts.go +++ /dev/null @@ -1,115 +0,0 @@ -package cmds - -import ( - "fmt" - "os" - "os/exec" - "path/filepath" - "strings" - - "go-hurobot/qbot" -) - -type TTSVoice struct { - Name string `gorm:"column:name"` - Local string `gorm:"column:local"` -} - -func cmd_tts(c *qbot.Client, msg *qbot.Message, args *ArgsList) { - const help = "Usage:\n tts search \n tts " - for _, t := range args.Types { - if t != qbot.Text { - c.SendMsg(msg, help) - return - } - } - - if args.Size < 2 { - c.SendMsg(msg, help) - return - } - - switch args.Contents[1] { - case "help", "-h", "-?", "--help", "?": - c.SendMsg(msg, help) - return - case "search", "q": - if args.Size < 3 { - c.SendMsg(msg, "tts search ") - return - } - keyword := strings.Join(args.Contents[2:], " ") - var voices []TTSVoice - result := qbot.PsqlDB.Table("tts_voices"). - Where("name ~ ?", keyword). - Limit(11). - Find(&voices) - - if result.Error != nil { - c.SendMsg(msg, result.Error.Error()) - return - } - - if len(voices) == 0 { - c.SendMsg(msg, "[]") - return - } - - var output strings.Builder - for i, voice := range voices { - if i == 10 { - output.WriteString("...\n") - break - } - output.WriteString(fmt.Sprintf("%s (%s)\n", voice.Name, voice.Local)) - } - c.SendMsg(msg, output.String()) - - default: - if args.Size < 3 { - c.SendMsg(msg, "tts "+args.Contents[1]+" ") - return - } - - voiceName := args.Contents[1] - text := strings.Join(args.Contents[2:], " ") - - var voice TTSVoice - result := qbot.PsqlDB.Table("tts_voices"). - Where("name = ?", voiceName). - First(&voice) - - if result.Error != nil { - c.SendMsg(msg, "voice not found: "+voiceName) - return - } - - cmd := exec.Command("./tts", voiceName, text) - output, err := cmd.CombinedOutput() - - if err != nil { - c.SendMsg(msg, err.Error()) - return - } - - if cmd.ProcessState.ExitCode() != 0 { - c.SendMsg(msg, string(output)) - return - } - - filename := strings.TrimSpace(string(output)) - filePath := filepath.Join("out", filename) - - fileInfo, err := os.Stat(filePath) - if err != nil { - c.SendMsg(msg, "failed: file not found") - return - } - if fileInfo.Size() == 0 { - c.SendMsg(msg, "failed: file is empty") - return - } - - c.SendRecord(msg, filePath) - } -} diff --git a/docker/psql-init/init.sql b/docker/psql-init/init.sql index 8477a79..87b4cc5 100644 --- a/docker/psql-init/init.sql +++ b/docker/psql-init/init.sql @@ -65,4 +65,12 @@ CREATE TABLE group_rcon_configs ( ); INSERT INTO suppliers ("name", "base_url", "api_key", "default_model") VALUES -('siliconflow', 'https://api.siliconflow.cn/v1', '', 'deepseek-ai/DeepSeek-V3'); +('siliconflow', 'https://api.siliconflow.cn/v1', '', 'deepseek-ai/DeepSeek-V3.1'); + +CREATE TABLE legacy_game ( + "user_id" BIGINT NOT NULL, + "energy" INT NOT NULL DEFAULT 0, + "balance" INT NOT NULL DEFAULT 0, + PRIMARY KEY ("user_id"), + FOREIGN KEY ("user_id") REFERENCES users(user_id) +) diff --git a/handlers.go b/handlers.go index e5c734c..0c2d4c1 100644 --- a/handlers.go +++ b/handlers.go @@ -3,6 +3,7 @@ package main import ( "go-hurobot/cmds" "go-hurobot/config" + "go-hurobot/legacy" "go-hurobot/llm" "go-hurobot/mc" "go-hurobot/qbot" @@ -23,6 +24,12 @@ func messageHandler(c *qbot.Client, msg *qbot.Message) { llm.LLMMsgHandle(c, msg) return } - cmds.CheckUserEvents(c, msg) + if legacy.IsGameCommand(msg) { + legacy.GameCommandHandle(c, msg) + return + } + if cmds.CheckUserEvents(c, msg) { + return + } } } diff --git a/legacy/game.go b/legacy/game.go new file mode 100644 index 0000000..d7fa2c1 --- /dev/null +++ b/legacy/game.go @@ -0,0 +1,37 @@ +package legacy + +import ( + "errors" + "go-hurobot/qbot" + "strings" + + "gorm.io/gorm" +) + +func IsGameCommand(msg *qbot.Message) bool { + msg0 := msg.Array[0].Content + return msg.Array[0].Type == qbot.Text && strings.HasPrefix(msg0, "&") +} + +func GameCommandHandle(c *qbot.Client, msg *qbot.Message) { + cmd := msg.Array[0].Content[1:] + + var gameData qbot.LegacyGame + result := qbot.PsqlDB.Where("user_id = ?", msg.UserID).First(&gameData) + + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + newGameData := qbot.LegacyGame{ + UserID: msg.UserID, + } + if err := qbot.PsqlDB.Create(&newGameData).Error; err != nil { + return + } + gameData = newGameData + } else { + return + } + } + _ = gameData + _ = cmd +} diff --git a/qbot/database.go b/qbot/database.go index c345f18..59ed27a 100644 --- a/qbot/database.go +++ b/qbot/database.go @@ -46,13 +46,19 @@ type GroupRconConfigs struct { Enabled bool `gorm:"not null;column:enabled;default:false"` } +type LegacyGame struct { + UserID uint64 `gorm:"primaryKey;column:user_id"` + Energy int `gorm:"not null;column:energy;default:0"` + Balance int `gorm:"not null;column:balance;default:0"` +} + func initPsqlDB(dsn string) error { var err error if PsqlDB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}); err != nil { return err } PsqlConnected = true - return PsqlDB.AutoMigrate(&Users{}, &Messages{}, &UserEvents{}, &GroupRconConfigs{}) + return PsqlDB.AutoMigrate(&Users{}, &Messages{}, &UserEvents{}, &GroupRconConfigs{}, &LegacyGame{}) } func SaveDatabase(msg *Message, isCmd bool) error {