Fixed himbucks
This commit is contained in:
parent
022686e14f
commit
e14051569c
1 changed files with 28 additions and 29 deletions
|
@ -3,7 +3,6 @@ package lib
|
|||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
@ -21,42 +20,42 @@ type HimbucksEntry struct {
|
|||
MessageCount int
|
||||
}
|
||||
|
||||
func GetOrCreateHimbucksEntry(userID int, guildID string) error {
|
||||
_, err := DBClient.Exec(`
|
||||
INSERT INTO himbucks (user_id, guild_id, balance, message_count)
|
||||
VALUES (?, ?, 0, 0)
|
||||
ON CONFLICT (user_id, guild_id) DO NOTHING`,
|
||||
userID, guildID)
|
||||
return err
|
||||
}
|
||||
|
||||
func ProcessMessage(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
||||
// Ignore bot messages
|
||||
if m.Author.Bot {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get user from database
|
||||
// Get user from database, create if doesn't exist
|
||||
var userID int
|
||||
err := DBClient.QueryRow("SELECT id FROM users WHERE discord_id = ?", m.Author.ID).Scan(&userID)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
// Store user in database
|
||||
dbErr := StoreUser(m.Author.ID, m.Author.Username)
|
||||
if dbErr != nil {
|
||||
// Log the error but don't stop command execution
|
||||
log.Printf("Error storing user: %v", dbErr)
|
||||
err = StoreUser(m.Author.ID, m.Author.Username)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to store user: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed to get user: %w", err)
|
||||
// Get the ID of the newly created user
|
||||
err = DBClient.QueryRow("SELECT id FROM users WHERE discord_id = ?", m.Author.ID).Scan(&userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get new user ID: %w", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("failed to query user: %w", err)
|
||||
}
|
||||
|
||||
// Ensure himbucks entry exists
|
||||
err = GetOrCreateHimbucksEntry(userID, m.GuildID)
|
||||
// Create himbucks entry if it doesn't exist
|
||||
_, err = DBClient.Exec(`
|
||||
INSERT INTO himbucks (user_id, guild_id, balance, message_count)
|
||||
VALUES (?, ?, 0, 0)
|
||||
ON CONFLICT (user_id, guild_id) DO NOTHING`,
|
||||
userID, m.GuildID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create himbucks entry: %w", err)
|
||||
}
|
||||
|
||||
// Update message count and check for rewards
|
||||
// Continue with the rest of the message processing...
|
||||
tx, err := DBClient.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to start transaction: %w", err)
|
||||
|
@ -67,9 +66,9 @@ func ProcessMessage(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|||
var lastEarnedAt sql.NullTime
|
||||
|
||||
err = tx.QueryRow(`
|
||||
SELECT message_count, last_earned_at
|
||||
FROM himbucks
|
||||
WHERE user_id = ? AND guild_id = ?`,
|
||||
SELECT message_count, last_earned_at
|
||||
FROM himbucks
|
||||
WHERE user_id = ? AND guild_id = ?`,
|
||||
userID, m.GuildID).Scan(&messageCount, &lastEarnedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get message count: %w", err)
|
||||
|
@ -81,15 +80,15 @@ func ProcessMessage(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|||
|
||||
if shouldReward {
|
||||
_, err = tx.Exec(`
|
||||
UPDATE himbucks
|
||||
SET balance = balance + ?, message_count = 0, last_earned_at = CURRENT_TIMESTAMP
|
||||
WHERE user_id = ? AND guild_id = ?`,
|
||||
UPDATE himbucks
|
||||
SET balance = balance + ?, message_count = 0, last_earned_at = CURRENT_TIMESTAMP
|
||||
WHERE user_id = ? AND guild_id = ?`,
|
||||
HimbucksPerReward, userID, m.GuildID)
|
||||
} else {
|
||||
_, err = tx.Exec(`
|
||||
UPDATE himbucks
|
||||
SET message_count = ?
|
||||
WHERE user_id = ? AND guild_id = ?`,
|
||||
UPDATE himbucks
|
||||
SET message_count = ?
|
||||
WHERE user_id = ? AND guild_id = ?`,
|
||||
messageCount, userID, m.GuildID)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue