AAAND its back

This commit is contained in:
Atridad Lahiji 2023-12-27 15:02:23 -07:00
parent 9c3467e72b
commit 0ff1d594ad
No known key found for this signature in database
2 changed files with 33 additions and 33 deletions

View file

@ -8,9 +8,11 @@ import (
"github.com/redis/go-redis/v9"
)
var redis_host = os.Getenv("REDIS_HOST")
var redis_password = os.Getenv("REDIS_PASSWORD")
func SetCache(key string, value string, ttlMinutes int) bool {
var redis_host = os.Getenv("REDIS_HOST")
var redis_password = os.Getenv("REDIS_PASSWORD")
println("Setting the Cache")
rdb := redis.NewClient(&redis.Options{
Addr: redis_host,
Password: redis_password,
@ -20,8 +22,6 @@ func SetCache(key string, value string, ttlMinutes int) bool {
panic("Failed to create Redis client")
}
println("Created Client in Set")
err := rdb.Set(context.Background(), key, value, time.Minute*time.Duration(ttlMinutes)).Err()
if err != nil {
panic(err)
@ -31,10 +31,7 @@ func SetCache(key string, value string, ttlMinutes int) bool {
}
func GetCache(key string) string {
var redis_host = os.Getenv("REDIS_HOST")
var redis_password = os.Getenv("REDIS_PASSWORD")
println("Entered Get")
println("Fetching From Cache")
rdb := redis.NewClient(&redis.Options{
Addr: redis_host,
Password: redis_password,
@ -46,9 +43,11 @@ func GetCache(key string) string {
val, err := rdb.Get(context.Background(), key).Result()
if err != nil {
println("Cache Miss")
return "nil"
}
println("Called Get")
println("Cache Hit")
return val
}

49
main.go
View file

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"himbot/lib"
"io"
"log"
"net/http"
@ -29,7 +30,7 @@ var commands = []api.CreateCommandData{
},
{
Name: "ask",
Description: "Ask Himbot!",
Description: "Ask Himbot! Cooldown: 1 Minute.",
Options: []discord.CommandOption{
&discord.StringOption{
OptionName: "prompt",
@ -40,7 +41,7 @@ var commands = []api.CreateCommandData{
},
{
Name: "pic",
Description: "Generate an image using Stable Diffusion!",
Description: "Generate an image using Stable Diffusion! Cooldown: 1 Minute.",
Options: []discord.CommandOption{
&discord.StringOption{
OptionName: "prompt",
@ -51,7 +52,7 @@ var commands = []api.CreateCommandData{
},
{
Name: "hdpic",
Description: "Generate an image using DALL·E 3!",
Description: "Generate an image using DALL·E 3! Cooldown: 10 Minutes.",
Options: []discord.CommandOption{
&discord.StringOption{
OptionName: "prompt",
@ -130,13 +131,13 @@ 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
// cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "ask")
// if cachedVal != "nil" {
// return &api.InteractionResponseData{
// Content: option.NewNullableString("Please wait for the cooldown!"),
// }
// }
// lib.SetCache(data.Event.User.ID.String()+":"+"ask", data.Event.User.ID.String()+":"+"ask", 1)
cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "ask")
if cachedVal != "nil" {
return &api.InteractionResponseData{
Content: option.NewNullableString("Please wait for the cooldown!"),
}
}
lib.SetCache(data.Event.User.ID.String()+":"+"ask", data.Event.User.ID.String()+":"+"ask", 1)
// Command Logic
var options struct {
@ -178,13 +179,13 @@ 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
// cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "pic")
// if cachedVal != "nil" {
// return &api.InteractionResponseData{
// Content: option.NewNullableString("Please wait for the cooldown!"),
// }
// }
// lib.SetCache(data.Event.User.ID.String()+":"+"pic", data.Event.User.ID.String()+":"+"pic", 1)
cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "pic")
if cachedVal != "nil" {
return &api.InteractionResponseData{
Content: option.NewNullableString("Please wait for the cooldown!"),
}
}
lib.SetCache(data.Event.User.ID.String()+":"+"pic", data.Event.User.ID.String()+":"+"pic", 1)
// Command Logic
var options struct {
@ -256,13 +257,13 @@ 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
// cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "hdpic")
// if cachedVal != "nil" {
// return &api.InteractionResponseData{
// Content: option.NewNullableString("Please wait for the cooldown!"),
// }
// }
// lib.SetCache(data.Event.User.ID.String()+":"+"hdpic", data.Event.User.ID.String()+":"+"hdpic", 10)
cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "hdpic")
if cachedVal != "nil" {
return &api.InteractionResponseData{
Content: option.NewNullableString("Please wait for the cooldown!"),
}
}
lib.SetCache(data.Event.User.ID.String()+":"+"hdpic", data.Event.User.ID.String()+":"+"hdpic", 10)
// Command Logic
var options struct {