Made cooldowns a bit more intuitive

This commit is contained in:
2024-01-10 00:56:36 -07:00
parent bba02f0303
commit b3e6291ad6
3 changed files with 16 additions and 14 deletions

View File

@ -41,21 +41,21 @@ func (m *CooldownManager) StartCooldown(userID string, key string, duration time
m.cooldowns[userID+":"+key] = time.Now().Add(duration)
}
func (m *CooldownManager) IsOnCooldown(userID string, key string) bool {
func (m *CooldownManager) IsOnCooldown(userID string, key string) (bool, time.Duration) {
m.mu.Lock()
defer m.mu.Unlock()
cooldownEnd, exists := m.cooldowns[userID+":"+key]
if !exists {
return false
return false, 0
}
if time.Now().After(cooldownEnd) {
delete(m.cooldowns, userID+":"+key)
return false
return false, 0
}
return true
return true, time.Until(cooldownEnd)
}
func CancelCooldown(userID string, key string) {

View File

@ -1,6 +1,7 @@
package lib
import (
"fmt"
"os"
"strings"
"time"
@ -67,21 +68,22 @@ func GetUserObject(event discord.InteractionEvent) Userish {
}
}
func CooldownHandler(event discord.InteractionEvent, key string, duration time.Duration) bool {
func CooldownHandler(event discord.InteractionEvent, key string, duration time.Duration) (bool, string) {
user := GetUserObject(event)
allowList := strings.Split(os.Getenv("COOLDOWN_ALLOW_LIST"), ",")
// Check if the user ID is in the allowList
for _, id := range allowList {
if id == user.ID().String() {
return true
return true, ""
}
}
if manager.IsOnCooldown(user.ID().String(), key) {
return false
isOnCooldown, remaining := manager.IsOnCooldown(user.ID().String(), key)
if isOnCooldown {
return false, fmt.Sprintf("You are on cooldown. Please wait for %v", remaining)
}
manager.StartCooldown(user.ID().String(), key, duration)
return true
return true, ""
}