Made cooldowns a bit more intuitive

This commit is contained in:
Atridad Lahiji 2024-01-10 00:56:36 -07:00
parent bba02f0303
commit b3e6291ad6
No known key found for this signature in database
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) 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() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
cooldownEnd, exists := m.cooldowns[userID+":"+key] cooldownEnd, exists := m.cooldowns[userID+":"+key]
if !exists { if !exists {
return false return false, 0
} }
if time.Now().After(cooldownEnd) { if time.Now().After(cooldownEnd) {
delete(m.cooldowns, userID+":"+key) delete(m.cooldowns, userID+":"+key)
return false return false, 0
} }
return true return true, time.Until(cooldownEnd)
} }
func CancelCooldown(userID string, key string) { func CancelCooldown(userID string, key string) {

View file

@ -1,6 +1,7 @@
package lib package lib
import ( import (
"fmt"
"os" "os"
"strings" "strings"
"time" "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) user := GetUserObject(event)
allowList := strings.Split(os.Getenv("COOLDOWN_ALLOW_LIST"), ",") allowList := strings.Split(os.Getenv("COOLDOWN_ALLOW_LIST"), ",")
// Check if the user ID is in the allowList // Check if the user ID is in the allowList
for _, id := range allowList { for _, id := range allowList {
if id == user.ID().String() { if id == user.ID().String() {
return true return true, ""
} }
} }
if manager.IsOnCooldown(user.ID().String(), key) { isOnCooldown, remaining := manager.IsOnCooldown(user.ID().String(), key)
return false if isOnCooldown {
return false, fmt.Sprintf("You are on cooldown. Please wait for %v", remaining)
} }
manager.StartCooldown(user.ID().String(), key, duration) manager.StartCooldown(user.ID().String(), key, duration)
return true return true, ""
} }

10
main.go
View file

@ -29,7 +29,7 @@ var commands = []api.CreateCommandData{
}, },
{ {
Name: "ask", Name: "ask",
Description: "Ask Himbot! Cooldown: 1 Minute.", Description: "Ask Himbot! Cooldown: 2 Minutes.",
Options: []discord.CommandOption{ Options: []discord.CommandOption{
&discord.StringOption{ &discord.StringOption{
OptionName: "prompt", OptionName: "prompt",
@ -118,10 +118,10 @@ func (h *handler) cmdPing(ctx context.Context, data cmdroute.CommandData) *api.I
func (h *handler) cmdAsk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { func (h *handler) cmdAsk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
// Cooldown Logic // Cooldown Logic
allowed := lib.CooldownHandler(*data.Event, "ask", time.Minute) allowed, cooldownString := lib.CooldownHandler(*data.Event, "ask", time.Minute)
if !allowed { if !allowed {
return errorResponse(errors.New("please wait for the cooldown")) return errorResponse(errors.New(cooldownString))
} }
// Command Logic // Command Logic
@ -167,10 +167,10 @@ func (h *handler) cmdAsk(ctx context.Context, data cmdroute.CommandData) *api.In
func (h *handler) cmdPic(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { func (h *handler) cmdPic(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
// Cooldown Logic // Cooldown Logic
allowed := lib.CooldownHandler(*data.Event, "pic", time.Minute) allowed, cooldownString := lib.CooldownHandler(*data.Event, "pic", time.Minute*2)
if !allowed { if !allowed {
return errorResponse(errors.New("please wait for the cooldown")) return errorResponse(errors.New(cooldownString))
} }
// Command Logic // Command Logic