mirror of
https://github.com/awfufu/go-hurobot.git
synced 2026-03-01 05:29:43 +08:00
feat: add legacy game
This commit is contained in:
115
cmds/tts.go
115
cmds/tts.go
@@ -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 <voice_name>\n tts <voice_name> <text>"
|
||||
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 <voice_name>")
|
||||
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]+" <text>")
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
37
legacy/game.go
Normal file
37
legacy/game.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user