From 06074fb84ab686d773eef4a60204188423da9eca Mon Sep 17 00:00:00 2001 From: atridadl Date: Wed, 3 Jan 2024 14:32:37 -0700 Subject: [PATCH] No More Redis... simplify! --- .env.example | 2 -- lib/cooldowns.go | 41 ++++++++++++++++++++++++++++++++++++ lib/helpers.go | 11 ++++++---- lib/redis.go | 55 ------------------------------------------------ main.go | 7 +++--- 5 files changed, 52 insertions(+), 64 deletions(-) create mode 100644 lib/cooldowns.go delete mode 100644 lib/redis.go diff --git a/.env.example b/.env.example index 19d9b5f..25d37b8 100644 --- a/.env.example +++ b/.env.example @@ -2,7 +2,5 @@ DISCORD_TOKEN="" OPENAI_API_KEY="" REPLICATE_API_TOKEN="" -REDIS_HOST="" -REDIS_PASSWORD="" # Comma separated COOLDOWN_ALLOW_LIST="" \ No newline at end of file diff --git a/lib/cooldowns.go b/lib/cooldowns.go new file mode 100644 index 0000000..0adf1e8 --- /dev/null +++ b/lib/cooldowns.go @@ -0,0 +1,41 @@ +package lib + +import ( + "sync" + "time" +) + +type CooldownManager struct { + cooldowns map[string]time.Time + mu sync.Mutex +} + +func NewCooldownManager() *CooldownManager { + return &CooldownManager{ + cooldowns: make(map[string]time.Time), + } +} + +func (m *CooldownManager) StartCooldown(key string, duration time.Duration) { + m.mu.Lock() + defer m.mu.Unlock() + + m.cooldowns[key] = time.Now().Add(duration) +} + +func (m *CooldownManager) IsOnCooldown(key string) bool { + m.mu.Lock() + defer m.mu.Unlock() + + cooldownEnd, exists := m.cooldowns[key] + if !exists { + return false + } + + if time.Now().After(cooldownEnd) { + delete(m.cooldowns, key) + return false + } + + return true +} diff --git a/lib/helpers.go b/lib/helpers.go index 56bb022..f7e83a7 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -3,10 +3,13 @@ package lib import ( "os" "strings" + "time" "github.com/diamondburned/arikawa/v3/discord" ) +var manager = NewCooldownManager() + // 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. type Userish interface { @@ -64,7 +67,7 @@ func GetUserObject(event discord.InteractionEvent) Userish { } } -func CooldownHandler(event discord.InteractionEvent, command string, cooldownMinutes int) bool { +func CooldownHandler(event discord.InteractionEvent, key string, duration time.Duration) bool { user := GetUserObject(event) allowList := strings.Split(os.Getenv("COOLDOWN_ALLOW_LIST"), ",") @@ -75,10 +78,10 @@ func CooldownHandler(event discord.InteractionEvent, command string, cooldownMin } } - cachedVal := GetCache(user.ID().String() + ":" + command) - if cachedVal != "nil" { + if manager.IsOnCooldown(key) { return false } - SetCache(user.ID().String()+":"+"hdpic", user.ID().String()+":"+command, cooldownMinutes) + + manager.StartCooldown(key, duration) return true } diff --git a/lib/redis.go b/lib/redis.go deleted file mode 100644 index d775a4e..0000000 --- a/lib/redis.go +++ /dev/null @@ -1,55 +0,0 @@ -package lib - -import ( - "context" - "os" - "time" - - "github.com/redis/go-redis/v9" -) - -var redis_host = os.Getenv("REDIS_HOST") -var redis_password = os.Getenv("REDIS_PASSWORD") - -var rdb = redis.NewClient(&redis.Options{ - Addr: redis_host, - Password: redis_password, - DB: 0, -}) - -func SetCache(key string, value string, ttlMinutes int) bool { - println("Setting the Cache") - rdb := redis.NewClient(&redis.Options{ - Addr: redis_host, - Password: redis_password, - DB: 0, - }) - if rdb == nil { - panic("Failed to create Redis client") - } - - err := rdb.Set(context.Background(), key, value, time.Minute*time.Duration(ttlMinutes)).Err() - if err != nil { - panic(err) - } - - return err != nil -} - -func GetCache(key string) string { - println("Fetching From Cache") - - if rdb == nil { - panic("Failed to create Redis client") - } - - val, err := rdb.Get(context.Background(), key).Result() - if err != nil { - println("Cache Miss") - return "nil" - } - - println("Cache Hit") - - return val -} diff --git a/main.go b/main.go index 34533af..8d61dcb 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "os/signal" + "time" "github.com/diamondburned/arikawa/v3/api" "github.com/diamondburned/arikawa/v3/api/cmdroute" @@ -132,7 +133,7 @@ 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", 1) + allowed := lib.CooldownHandler(*data.Event, "ask", time.Minute) if !allowed { return errorResponse(errors.New("please wait for the cooldown")) @@ -194,7 +195,7 @@ 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", 1) + allowed := lib.CooldownHandler(*data.Event, "pic", time.Minute) if !allowed { return errorResponse(errors.New("please wait for the cooldown")) @@ -270,7 +271,7 @@ func (h *handler) cmdPic(ctx context.Context, data cmdroute.CommandData) *api.In func (h *handler) cmdHDPic(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { // Cooldown Logic - allowed := lib.CooldownHandler(*data.Event, "hdPic", 1) + allowed := lib.CooldownHandler(*data.Event, "hdPic", time.Minute*10) if !allowed { return errorResponse(errors.New("please wait for the cooldown"))