This commit is contained in:
parent
ba9bf88a1c
commit
4cf9c3295f
5 changed files with 132 additions and 174 deletions
|
@ -1,2 +1,3 @@
|
|||
# Tokens
|
||||
DISCORD_TOKEN=""
|
||||
ROOT_DIR=""
|
||||
|
|
|
@ -22,15 +22,6 @@ func BalanceGetCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (st
|
|||
return fmt.Sprintf("💸 You have %d Himbucks! 💸", balance), nil
|
||||
}
|
||||
|
||||
func BalanceSyncCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (string, error) {
|
||||
syncError := lib.SyncBalance()
|
||||
if syncError != nil {
|
||||
return "", syncError
|
||||
}
|
||||
|
||||
return fmt.Sprintf("💸 Force-Synchronized Himbucks! 💸"), nil
|
||||
}
|
||||
|
||||
func LeaderboardCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (string, error) {
|
||||
entries, err := lib.GetLeaderboard(i.GuildID, 10)
|
||||
if err != nil {
|
||||
|
|
29
lib/db.go
29
lib/db.go
|
@ -17,35 +17,16 @@ var DBClient *sql.DB
|
|||
var DBConnector *libsql.Connector
|
||||
|
||||
func InitDB() error {
|
||||
dbUrl := os.Getenv("DATABASE_URL")
|
||||
dbToken := os.Getenv("DATABASE_AUTH_TOKEN")
|
||||
|
||||
if dbUrl == "" || dbToken == "" {
|
||||
return fmt.Errorf("database configuration missing")
|
||||
}
|
||||
|
||||
// Determine DB path based on /data directory existence
|
||||
dbPath := "himbot.db" // default to local
|
||||
if _, err := os.Stat("/data"); !os.IsNotExist(err) {
|
||||
dbPath = "/data/himbot.db"
|
||||
}
|
||||
dbName := "file:./himbot.db"
|
||||
|
||||
connector, connectorError := libsql.NewEmbeddedReplicaConnector(
|
||||
dbPath,
|
||||
dbUrl,
|
||||
libsql.WithAuthToken(dbToken),
|
||||
)
|
||||
|
||||
if connectorError != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open db %s: %s", dbUrl, connectorError)
|
||||
db, err := sql.Open("libsql", dbName)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to open db %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// finalDBUrl := fmt.Sprintf("%s?authToken=%s", dbUrl, dbToken)
|
||||
|
||||
client := sql.OpenDB(connector)
|
||||
|
||||
DBClient = client
|
||||
DBConnector = connector
|
||||
DBClient = db
|
||||
|
||||
return runMigrations()
|
||||
}
|
||||
|
|
|
@ -172,16 +172,6 @@ func SendBalance(fromDiscordID, toDiscordID, guildID string, amount int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func SyncBalance() error {
|
||||
_, syncError := DBConnector.Sync()
|
||||
if syncError != nil {
|
||||
fmt.Println("Error syncing database:", syncError)
|
||||
return syncError
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetLeaderboard(guildID string, limit int) ([]HimbucksEntry, error) {
|
||||
rows, err := DBClient.Query(`
|
||||
SELECT u.username, gp.currency_balance, gp.message_count
|
||||
|
|
5
main.go
5
main.go
|
@ -51,10 +51,6 @@ var (
|
|||
Name: "himboard",
|
||||
Description: "View the himbucks leaderboard",
|
||||
},
|
||||
{
|
||||
Name: "syncbucks",
|
||||
Description: "Sync your himbucks balance with the database",
|
||||
},
|
||||
{
|
||||
Name: "sendbucks",
|
||||
Description: "Send himbucks to another user",
|
||||
|
@ -82,7 +78,6 @@ var (
|
|||
"markov": lib.HandleCommand("markov", 30*time.Second, command.MarkovCommand),
|
||||
"himbucks": lib.HandleCommand("himbucks", 5*time.Second, command.BalanceGetCommand),
|
||||
"himboard": lib.HandleCommand("himboard", 5*time.Second, command.LeaderboardCommand),
|
||||
"syncbucks": lib.HandleCommand("syncbucks", 1800*time.Second, command.BalanceSyncCommand),
|
||||
"sendbucks": lib.HandleCommand("sendbucks", 1800*time.Second, command.BalanceSendCommand),
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue