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 (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
|
@ -21,42 +20,42 @@ type HimbucksEntry struct {
|
||||||
MessageCount int
|
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 {
|
func ProcessMessage(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
||||||
// Ignore bot messages
|
// Ignore bot messages
|
||||||
if m.Author.Bot {
|
if m.Author.Bot {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user from database
|
// Get user from database, create if doesn't exist
|
||||||
var userID int
|
var userID int
|
||||||
err := DBClient.QueryRow("SELECT id FROM users WHERE discord_id = ?", m.Author.ID).Scan(&userID)
|
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
|
// Store user in database
|
||||||
dbErr := StoreUser(m.Author.ID, m.Author.Username)
|
err = StoreUser(m.Author.ID, m.Author.Username)
|
||||||
if dbErr != nil {
|
if err != nil {
|
||||||
// Log the error but don't stop command execution
|
return fmt.Errorf("failed to store user: %w", err)
|
||||||
log.Printf("Error storing user: %v", dbErr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
// Create himbucks entry if it doesn't exist
|
||||||
err = GetOrCreateHimbucksEntry(userID, m.GuildID)
|
_, 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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create himbucks entry: %w", err)
|
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()
|
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)
|
||||||
|
@ -67,9 +66,9 @@ func ProcessMessage(s *discordgo.Session, m *discordgo.MessageCreate) error {
|
||||||
var lastEarnedAt sql.NullTime
|
var lastEarnedAt sql.NullTime
|
||||||
|
|
||||||
err = tx.QueryRow(`
|
err = tx.QueryRow(`
|
||||||
SELECT message_count, last_earned_at
|
SELECT message_count, last_earned_at
|
||||||
FROM himbucks
|
FROM himbucks
|
||||||
WHERE user_id = ? AND guild_id = ?`,
|
WHERE user_id = ? AND guild_id = ?`,
|
||||||
userID, m.GuildID).Scan(&messageCount, &lastEarnedAt)
|
userID, m.GuildID).Scan(&messageCount, &lastEarnedAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get message count: %w", err)
|
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 {
|
if shouldReward {
|
||||||
_, err = tx.Exec(`
|
_, err = tx.Exec(`
|
||||||
UPDATE himbucks
|
UPDATE himbucks
|
||||||
SET balance = balance + ?, message_count = 0, last_earned_at = CURRENT_TIMESTAMP
|
SET balance = balance + ?, message_count = 0, last_earned_at = CURRENT_TIMESTAMP
|
||||||
WHERE user_id = ? AND guild_id = ?`,
|
WHERE user_id = ? AND guild_id = ?`,
|
||||||
HimbucksPerReward, userID, m.GuildID)
|
HimbucksPerReward, userID, m.GuildID)
|
||||||
} else {
|
} else {
|
||||||
_, err = tx.Exec(`
|
_, err = tx.Exec(`
|
||||||
UPDATE himbucks
|
UPDATE himbucks
|
||||||
SET message_count = ?
|
SET message_count = ?
|
||||||
WHERE user_id = ? AND guild_id = ?`,
|
WHERE user_id = ? AND guild_id = ?`,
|
||||||
messageCount, userID, m.GuildID)
|
messageCount, userID, m.GuildID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue