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)
}
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, ""
}

10
main.go
View file

@ -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