From f672ff8763528ad3cfe1aa2087449f8264d969f8 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Tue, 19 Mar 2024 14:31:40 -0600 Subject: [PATCH] Deps + Made "cooldown" lib more generic as a timer lib --- command/ask.go | 4 +-- command/pic.go | 4 +-- go.mod | 2 +- go.sum | 2 ++ lib/cooldowns.go | 72 ------------------------------------------------ lib/helpers.go | 6 ++-- 6 files changed, 10 insertions(+), 80 deletions(-) delete mode 100644 lib/cooldowns.go diff --git a/command/ask.go b/command/ask.go index 07e0e17..ef3501e 100644 --- a/command/ask.go +++ b/command/ask.go @@ -28,7 +28,7 @@ func Ask(ctx context.Context, data cmdroute.CommandData) *api.InteractionRespons } if err := data.Options.Unmarshal(&options); err != nil { - lib.CancelCooldown(data.Event.Member.User.ID.String(), "ask") + lib.CancelTimer(data.Event.Member.User.ID.String(), "ask") return lib.ErrorResponse(err) } @@ -36,7 +36,7 @@ func Ask(ctx context.Context, data cmdroute.CommandData) *api.InteractionRespons if err != nil { fmt.Printf("ChatCompletion error: %v\n", err) - lib.CancelCooldown(data.Event.Member.User.ID.String(), "ask") + lib.CancelTimer(data.Event.Member.User.ID.String(), "ask") return &api.InteractionResponseData{ Content: option.NewNullableString("ChatCompletion Error!"), AllowedMentions: &api.AllowedMentions{}, diff --git a/command/pic.go b/command/pic.go index 5e99451..3b5e638 100644 --- a/command/pic.go +++ b/command/pic.go @@ -27,7 +27,7 @@ func Pic(ctx context.Context, data cmdroute.CommandData) *api.InteractionRespons } if err := data.Options.Unmarshal(&options); err != nil { - lib.CancelCooldown(data.Event.Member.User.ID.String(), "pic") + lib.CancelTimer(data.Event.Member.User.ID.String(), "pic") return lib.ErrorResponse(err) } @@ -40,7 +40,7 @@ func Pic(ctx context.Context, data cmdroute.CommandData) *api.InteractionRespons imageFile, err := lib.ReplicateImageGeneration(options.Prompt, filename) if err != nil { - lib.CancelCooldown(data.Event.Member.User.ID.String(), "pic") + lib.CancelTimer(data.Event.Member.User.ID.String(), "pic") return lib.ErrorResponse(err) } diff --git a/go.mod b/go.mod index 9fec7b2..2d343b7 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( ) require ( - github.com/aws/aws-sdk-go v1.50.37 + github.com/aws/aws-sdk-go v1.51.3 github.com/gorilla/schema v1.2.1 // indirect github.com/gorilla/websocket v1.5.1 // indirect github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index cb639a1..fbf17cc 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/aws/aws-sdk-go v1.50.37 h1:gnAf6eYPSTb4QpVwugtWFqD07QXOoX7LewRrtLUx3lI= github.com/aws/aws-sdk-go v1.50.37/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= +github.com/aws/aws-sdk-go v1.51.3 h1:OqSyEXcJwf/XhZNVpMRgKlLA9nmbo5X8dwbll4RWxq8= +github.com/aws/aws-sdk-go v1.51.3/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/lib/cooldowns.go b/lib/cooldowns.go deleted file mode 100644 index 6e1164b..0000000 --- a/lib/cooldowns.go +++ /dev/null @@ -1,72 +0,0 @@ -package lib - -import ( - "sync" - "time" -) - -var ( - mu sync.Mutex - instance *CooldownManager -) - -type CooldownManager struct { - cooldowns map[string]time.Time - mu sync.Mutex -} - -func NewCooldownManager() *CooldownManager { - return &CooldownManager{ - cooldowns: make(map[string]time.Time), - } -} - -func GetInstance() *CooldownManager { - mu.Lock() - defer mu.Unlock() - - if instance == nil { - instance = &CooldownManager{ - cooldowns: make(map[string]time.Time), - } - } - - return instance -} - -func (m *CooldownManager) StartCooldown(userID string, key string, duration time.Duration) { - m.mu.Lock() - defer m.mu.Unlock() - - m.cooldowns[userID+":"+key] = time.Now().Add(duration) -} - -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, 0 - } - - if time.Now().After(cooldownEnd) { - delete(m.cooldowns, userID+":"+key) - return false, 0 - } - - return true, time.Until(cooldownEnd) -} - -func CancelCooldown(userID string, key string) { - manager := GetInstance() - - // Handle non-existent keys gracefully - if _, exists := manager.cooldowns[userID+":"+key]; !exists { - return - } - - manager.mu.Lock() - defer manager.mu.Unlock() - delete(manager.cooldowns, userID+":"+key) -} diff --git a/lib/helpers.go b/lib/helpers.go index e142166..2434fb5 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -9,7 +9,7 @@ import ( "github.com/diamondburned/arikawa/v3/discord" ) -var manager = NewCooldownManager() +var manager = NewTimerManager() // Userish is an interface that captures the common methods you may want to call // on either a discord.Member or discord.User, including a display name. @@ -79,11 +79,11 @@ func CooldownHandler(event discord.InteractionEvent, key string, duration time.D } } - isOnCooldown, remaining := manager.IsOnCooldown(user.ID().String(), key) + isOnCooldown, remaining := manager.TimerRunning(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) + manager.StartTimer(user.ID().String(), key, duration) return true, "" }