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