HIMBOT RETURNS
All checks were successful
Docker Deploy / build-and-push (push) Successful in 1m26s

This commit is contained in:
Atridad Lahiji 2024-11-22 17:04:33 -06:00
parent ba9bf88a1c
commit 4cf9c3295f
Signed by: atridad
SSH key fingerprint: SHA256:LGomp8Opq0jz+7kbwNcdfTcuaLRb5Nh0k5AchDDb438
5 changed files with 132 additions and 174 deletions

View file

@ -1,2 +1,3 @@
# Tokens # Tokens
DISCORD_TOKEN="" DISCORD_TOKEN=""
ROOT_DIR=""

View file

@ -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 {
@ -51,48 +42,48 @@ func LeaderboardCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (s
} }
func BalanceSendCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (string, error) { func BalanceSendCommand(s *discordgo.Session, i *discordgo.InteractionCreate) (string, error) {
options := i.ApplicationCommandData().Options options := i.ApplicationCommandData().Options
// Discord handles the user mention/tag and provides the correct user ID // Discord handles the user mention/tag and provides the correct user ID
var recipientID string var recipientID string
var amount int var amount int
for _, opt := range options { for _, opt := range options {
switch opt.Name { switch opt.Name {
case "user": case "user":
recipientID = opt.UserValue(nil).ID recipientID = opt.UserValue(nil).ID
case "amount": case "amount":
amount = int(opt.IntValue()) amount = int(opt.IntValue())
} }
} }
// Validate amount // Validate amount
if amount <= 0 { if amount <= 0 {
return "", fmt.Errorf("amount must be positive") return "", fmt.Errorf("amount must be positive")
} }
// Get sender's info // Get sender's info
sender, err := lib.GetUser(i) sender, err := lib.GetUser(i)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to get sender info: %w", err) return "", fmt.Errorf("failed to get sender info: %w", err)
} }
// Don't allow sending to self // Don't allow sending to self
if sender.ID == recipientID { if sender.ID == recipientID {
return "", fmt.Errorf("you cannot send himbucks to yourself") return "", fmt.Errorf("you cannot send himbucks to yourself")
} }
// Get recipient's info // Get recipient's info
recipient, err := s.User(recipientID) recipient, err := s.User(recipientID)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to get recipient info: %w", err) return "", fmt.Errorf("failed to get recipient info: %w", err)
} }
// Send the himbucks // Send the himbucks
err = lib.SendBalance(sender.ID, recipientID, i.GuildID, amount) err = lib.SendBalance(sender.ID, recipientID, i.GuildID, amount)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to send himbucks: %w", err) return "", fmt.Errorf("failed to send himbucks: %w", err)
} }
return fmt.Sprintf("💸 Successfully sent %d Himbucks to %s! 💸", amount, recipient.Username), nil return fmt.Sprintf("💸 Successfully sent %d Himbucks to %s! 💸", amount, recipient.Username), nil
} }

View file

@ -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()
} }

View file

@ -85,98 +85,88 @@ func GetBalance(discordID, guildID string) (int, error) {
} }
func SendBalance(fromDiscordID, toDiscordID, guildID string, amount int) error { func SendBalance(fromDiscordID, toDiscordID, guildID string, amount int) error {
// Start database transaction // Start database transaction
tx, err := DBClient.Begin() tx, err := DBClient.Begin()
if err != nil { if err != nil {
return fmt.Errorf("failed to start transaction: %w", err) return fmt.Errorf("failed to start transaction: %w", err)
} }
defer tx.Rollback() defer tx.Rollback()
// Get sender's user ID // Get sender's user ID
var fromUserID string var fromUserID string
err = tx.QueryRow(` err = tx.QueryRow(`
SELECT id SELECT id
FROM users FROM users
WHERE discord_id = ?`, fromDiscordID).Scan(&fromUserID) WHERE discord_id = ?`, fromDiscordID).Scan(&fromUserID)
if err != nil { if err != nil {
return fmt.Errorf("sender not found: %w", err) return fmt.Errorf("sender not found: %w", err)
} }
// Get recipient's user ID // Get recipient's user ID
var toUserID string var toUserID string
err = tx.QueryRow(` err = tx.QueryRow(`
SELECT id SELECT id
FROM users FROM users
WHERE discord_id = ?`, toDiscordID).Scan(&toUserID) WHERE discord_id = ?`, toDiscordID).Scan(&toUserID)
if err != nil { if err != nil {
return fmt.Errorf("recipient not found: %w", err) return fmt.Errorf("recipient not found: %w", err)
} }
// Check if sender has sufficient balance // Check if sender has sufficient balance
var senderBalance int var senderBalance int
err = tx.QueryRow(` err = tx.QueryRow(`
SELECT currency_balance SELECT currency_balance
FROM guild_profiles FROM guild_profiles
WHERE user_id = ? AND guild_id = ?`, WHERE user_id = ? AND guild_id = ?`,
fromUserID, guildID).Scan(&senderBalance) fromUserID, guildID).Scan(&senderBalance)
if err != nil { if err != nil {
return fmt.Errorf("failed to get sender balance: %w", err) return fmt.Errorf("failed to get sender balance: %w", err)
} }
if senderBalance < amount { if senderBalance < amount {
return fmt.Errorf("insufficient balance: have %d, trying to send %d", senderBalance, amount) return fmt.Errorf("insufficient balance: have %d, trying to send %d", senderBalance, amount)
} }
// Deduct from sender // Deduct from sender
_, err = tx.Exec(` _, err = tx.Exec(`
UPDATE guild_profiles UPDATE guild_profiles
SET currency_balance = currency_balance - ? SET currency_balance = currency_balance - ?
WHERE user_id = ? AND guild_id = ?`, WHERE user_id = ? AND guild_id = ?`,
amount, fromUserID, guildID) amount, fromUserID, guildID)
if err != nil { if err != nil {
return fmt.Errorf("failed to deduct from sender: %w", err) return fmt.Errorf("failed to deduct from sender: %w", err)
} }
// Add to recipient // Add to recipient
result, err := tx.Exec(` result, err := tx.Exec(`
UPDATE guild_profiles UPDATE guild_profiles
SET currency_balance = currency_balance + ? SET currency_balance = currency_balance + ?
WHERE user_id = ? AND guild_id = ?`, WHERE user_id = ? AND guild_id = ?`,
amount, toUserID, guildID) amount, toUserID, guildID)
if err != nil { if err != nil {
return fmt.Errorf("failed to add to recipient: %w", err) return fmt.Errorf("failed to add to recipient: %w", err)
} }
// Check if recipient exists in guild_profiles // Check if recipient exists in guild_profiles
rowsAffected, err := result.RowsAffected() rowsAffected, err := result.RowsAffected()
if err != nil { if err != nil {
return fmt.Errorf("failed to check rows affected: %w", err) return fmt.Errorf("failed to check rows affected: %w", err)
} }
// If recipient doesn't have a profile in this guild, create one // If recipient doesn't have a profile in this guild, create one
if rowsAffected == 0 { if rowsAffected == 0 {
_, err = tx.Exec(` _, err = tx.Exec(`
INSERT INTO guild_profiles (user_id, guild_id, currency_balance, message_count) INSERT INTO guild_profiles (user_id, guild_id, currency_balance, message_count)
VALUES (?, ?, ?, 0)`, VALUES (?, ?, ?, 0)`,
toUserID, guildID, amount) toUserID, guildID, amount)
if err != nil { if err != nil {
return fmt.Errorf("failed to create recipient profile: %w", err) return fmt.Errorf("failed to create recipient profile: %w", err)
} }
} }
// Commit transaction // Commit transaction
if err = tx.Commit(); err != nil { if err = tx.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err) return fmt.Errorf("failed to commit transaction: %w", err)
}
return nil
}
func SyncBalance() error {
_, syncError := DBConnector.Sync()
if syncError != nil {
fmt.Println("Error syncing database:", syncError)
return syncError
} }
return nil return nil

37
main.go
View file

@ -51,28 +51,24 @@ 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",
Options: []*discordgo.ApplicationCommandOption{ Options: []*discordgo.ApplicationCommandOption{
{ {
Type: discordgo.ApplicationCommandOptionUser, Type: discordgo.ApplicationCommandOptionUser,
Name: "user", Name: "user",
Description: "The user to send himbucks to", Description: "The user to send himbucks to",
Required: true, Required: true,
}, },
{ {
Type: discordgo.ApplicationCommandOptionInteger, Type: discordgo.ApplicationCommandOptionInteger,
Name: "amount", Name: "amount",
Description: "Amount of himbucks to send", Description: "Amount of himbucks to send",
Required: true, Required: true,
MinValue: &[]float64{1}[0], MinValue: &[]float64{1}[0],
}, },
}, },
}, },
} }
@ -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),
} }
) )