mirror of
https://github.com/Xevion/banner.git
synced 2025-12-08 16:06:31 -06:00
Separate term reload logic, bump interval to 24h
This commit is contained in:
44
helpers.go
44
helpers.go
@@ -14,6 +14,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
log "github.com/rs/zerolog/log"
|
log "github.com/rs/zerolog/log"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
@@ -231,9 +232,15 @@ func GetFirstEnv(key ...string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPointer returns a pointer to the given value.
|
// GetIntPointer returns a pointer to the given value.
|
||||||
// This function is useful for discordgo, which inexplicably requires pointers to integers for minLength arguments.
|
// This function is useful for discordgo, which inexplicably requires pointers to integers for minLength arguments.
|
||||||
func GetPointer(value int) *int {
|
func GetIntPointer(value int) *int {
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFloatPointer returns a pointer to the given value.
|
||||||
|
// This function is useful for discordgo, which inexplicably requires pointers to floats for minLength arguments.
|
||||||
|
func GetFloatPointer(value float64) *float64 {
|
||||||
return &value
|
return &value
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -381,20 +388,31 @@ func EncodeParams(params map[string]*[]string) string {
|
|||||||
var terms []BannerTerm
|
var terms []BannerTerm
|
||||||
var lastTermUpdate time.Time
|
var lastTermUpdate time.Time
|
||||||
|
|
||||||
|
// TryReloadTerms attempts to reload the terms if they are not loaded or the last update was more than 24 hours ago
|
||||||
|
func TryReloadTerms() error {
|
||||||
|
if len(terms) > 0 && time.Since(lastTermUpdate) < 24*time.Hour {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the terms
|
||||||
|
var err error
|
||||||
|
terms, err = GetTerms("", 1, 100)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to load terms")
|
||||||
|
}
|
||||||
|
|
||||||
|
lastTermUpdate = time.Now()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// IsTermArchived checks if the given term is archived
|
// IsTermArchived checks if the given term is archived
|
||||||
// TODO: Add error, switch missing term logic to error
|
// TODO: Add error, switch missing term logic to error
|
||||||
func IsTermArchived(term string) bool {
|
func IsTermArchived(term string) bool {
|
||||||
// If the terms are not loaded, or the last update was more than 4 hours ago, update the terms
|
// Ensure the terms are loaded
|
||||||
if len(terms) == 0 || time.Since(lastTermUpdate) > 4*time.Hour {
|
err := TryReloadTerms()
|
||||||
// Load the terms
|
if err != nil {
|
||||||
var err error
|
log.Err(err).Stack().Msg("Failed to reload terms")
|
||||||
terms, err = GetTerms("", 1, 10)
|
return true
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg("Failed to get terms")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
lastTermUpdate = time.Now()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the term is in the list of terms
|
// Check if the term is in the list of terms
|
||||||
|
|||||||
Reference in New Issue
Block a user