added enhanced DB functions
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -45,6 +46,11 @@ func HandleSuggest(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = helpers.RemoveUserSubmittions(userID);
|
||||||
|
if err != nil {
|
||||||
|
log.Default().Println(fmt.Sprintf("%s", err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
// Store new submission time
|
// Store new submission time
|
||||||
if err := helpers.SetLastSubmission(userID); err != nil {
|
if err := helpers.SetLastSubmission(userID); err != nil {
|
||||||
helpers.HandleError(s, i, fmt.Errorf("failed to update cooldown: %w", err))
|
helpers.HandleError(s, i, fmt.Errorf("failed to update cooldown: %w", err))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package helpers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -62,6 +63,28 @@ func SetLastSubmission(userID string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveUserSubmittions(userId string) error {
|
||||||
|
_, err := db.Exec(`
|
||||||
|
DELETE FROM suggestion_cooldowns WHERE user_id = ?
|
||||||
|
`, userId)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func SweepExpiredSubmissions(expiration time.Duration) error {
|
||||||
|
cutoff := time.Now().Add(-expiration)
|
||||||
|
result, err := db.Exec("DELETE FROM suggestion_cooldowns WHERE last_submission <= ?", cutoff)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to sweep expired submissions: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsDeleted, _ := result.RowsAffected()
|
||||||
|
fmt.Printf("Swept %d expired entries from suggestion_cooldowns\n", rowsDeleted)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func CloseDB() error {
|
func CloseDB() error {
|
||||||
if db != nil {
|
if db != nil {
|
||||||
return db.Close()
|
return db.Close()
|
||||||
|
|||||||
+15
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
commands "ion606_bot/Bot/Commands"
|
commands "ion606_bot/Bot/Commands"
|
||||||
helpers "ion606_bot/Bot/Helpers"
|
helpers "ion606_bot/Bot/Helpers"
|
||||||
@@ -51,6 +52,20 @@ func Run() {
|
|||||||
log.Printf("Failed to initialize suggestion DB: %v", err)
|
log.Printf("Failed to initialize suggestion DB: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(5 * time.Minute) // adjust interval as needed
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
// For example, sweep entries older than 5 minutes:
|
||||||
|
if err := helpers.SweepExpiredSubmissions(5 * time.Minute); err != nil {
|
||||||
|
log.Printf("Error sweeping expired submissions: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
RegisterCommands(discord, "")
|
RegisterCommands(discord, "")
|
||||||
|
|
||||||
log.Println("Bot running....")
|
log.Println("Bot running....")
|
||||||
|
|||||||
Reference in New Issue
Block a user