From b3e6291ad6556769fd6e4353aec9d1299aac8bd8 Mon Sep 17 00:00:00 2001 From: atridadl Date: Wed, 10 Jan 2024 00:56:36 -0700 Subject: [PATCH] Made cooldowns a bit more intuitive --- lib/cooldowns.go | 8 ++++---- lib/helpers.go | 12 +++++++----- main.go | 10 +++++----- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/cooldowns.go b/lib/cooldowns.go index e2cc173..70184b8 100644 --- a/lib/cooldowns.go +++ b/lib/cooldowns.go @@ -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) { diff --git a/lib/helpers.go b/lib/helpers.go index 74123a5..e142166 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -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, "" } diff --git a/main.go b/main.go index 27ddee2..7afd4ba 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ var commands = []api.CreateCommandData{ }, { Name: "ask", - Description: "Ask Himbot! Cooldown: 1 Minute.", + Description: "Ask Himbot! Cooldown: 2 Minutes.", Options: []discord.CommandOption{ &discord.StringOption{ 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 { // Cooldown Logic - allowed := lib.CooldownHandler(*data.Event, "ask", time.Minute) + allowed, cooldownString := lib.CooldownHandler(*data.Event, "ask", time.Minute) if !allowed { - return errorResponse(errors.New("please wait for the cooldown")) + return errorResponse(errors.New(cooldownString)) } // 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 { // Cooldown Logic - allowed := lib.CooldownHandler(*data.Event, "pic", time.Minute) + allowed, cooldownString := lib.CooldownHandler(*data.Event, "pic", time.Minute*2) if !allowed { - return errorResponse(errors.New("please wait for the cooldown")) + return errorResponse(errors.New(cooldownString)) } // Command Logic