Deps + Made "cooldown" lib more generic as a timer lib
This commit is contained in:
parent
3ac076709e
commit
f672ff8763
6 changed files with 10 additions and 80 deletions
|
@ -28,7 +28,7 @@ func Ask(ctx context.Context, data cmdroute.CommandData) *api.InteractionRespons
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := data.Options.Unmarshal(&options); err != nil {
|
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)
|
return lib.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ func Ask(ctx context.Context, data cmdroute.CommandData) *api.InteractionRespons
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ChatCompletion error: %v\n", err)
|
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{
|
return &api.InteractionResponseData{
|
||||||
Content: option.NewNullableString("ChatCompletion Error!"),
|
Content: option.NewNullableString("ChatCompletion Error!"),
|
||||||
AllowedMentions: &api.AllowedMentions{},
|
AllowedMentions: &api.AllowedMentions{},
|
||||||
|
|
|
@ -27,7 +27,7 @@ func Pic(ctx context.Context, data cmdroute.CommandData) *api.InteractionRespons
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := data.Options.Unmarshal(&options); err != nil {
|
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)
|
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)
|
imageFile, err := lib.ReplicateImageGeneration(options.Prompt, filename)
|
||||||
|
|
||||||
if err != nil {
|
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)
|
return lib.ErrorResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -11,7 +11,7 @@ require (
|
||||||
)
|
)
|
||||||
|
|
||||||
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/schema v1.2.1 // indirect
|
||||||
github.com/gorilla/websocket v1.5.1 // indirect
|
github.com/gorilla/websocket v1.5.1 // indirect
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
|
|
2
go.sum
2
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 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.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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/diamondburned/arikawa/v3/discord"
|
"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
|
// 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.
|
// 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 {
|
if isOnCooldown {
|
||||||
return false, fmt.Sprintf("You are on cooldown. Please wait for %v", remaining)
|
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, ""
|
return true, ""
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue