AAAND its back
This commit is contained in:
parent
9c3467e72b
commit
0ff1d594ad
2 changed files with 33 additions and 33 deletions
17
lib/redis.go
17
lib/redis.go
|
@ -8,9 +8,11 @@ import (
|
||||||
"github.com/redis/go-redis/v9"
|
"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 {
|
func SetCache(key string, value string, ttlMinutes int) bool {
|
||||||
var redis_host = os.Getenv("REDIS_HOST")
|
println("Setting the Cache")
|
||||||
var redis_password = os.Getenv("REDIS_PASSWORD")
|
|
||||||
rdb := redis.NewClient(&redis.Options{
|
rdb := redis.NewClient(&redis.Options{
|
||||||
Addr: redis_host,
|
Addr: redis_host,
|
||||||
Password: redis_password,
|
Password: redis_password,
|
||||||
|
@ -20,8 +22,6 @@ func SetCache(key string, value string, ttlMinutes int) bool {
|
||||||
panic("Failed to create Redis client")
|
panic("Failed to create Redis client")
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Created Client in Set")
|
|
||||||
|
|
||||||
err := rdb.Set(context.Background(), key, value, time.Minute*time.Duration(ttlMinutes)).Err()
|
err := rdb.Set(context.Background(), key, value, time.Minute*time.Duration(ttlMinutes)).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -31,10 +31,7 @@ func SetCache(key string, value string, ttlMinutes int) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCache(key string) string {
|
func GetCache(key string) string {
|
||||||
var redis_host = os.Getenv("REDIS_HOST")
|
println("Fetching From Cache")
|
||||||
var redis_password = os.Getenv("REDIS_PASSWORD")
|
|
||||||
println("Entered Get")
|
|
||||||
|
|
||||||
rdb := redis.NewClient(&redis.Options{
|
rdb := redis.NewClient(&redis.Options{
|
||||||
Addr: redis_host,
|
Addr: redis_host,
|
||||||
Password: redis_password,
|
Password: redis_password,
|
||||||
|
@ -46,9 +43,11 @@ func GetCache(key string) string {
|
||||||
|
|
||||||
val, err := rdb.Get(context.Background(), key).Result()
|
val, err := rdb.Get(context.Background(), key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
println("Cache Miss")
|
||||||
return "nil"
|
return "nil"
|
||||||
}
|
}
|
||||||
println("Called Get")
|
|
||||||
|
println("Cache Hit")
|
||||||
|
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
49
main.go
49
main.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"himbot/lib"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -29,7 +30,7 @@ var commands = []api.CreateCommandData{
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "ask",
|
Name: "ask",
|
||||||
Description: "Ask Himbot!",
|
Description: "Ask Himbot! Cooldown: 1 Minute.",
|
||||||
Options: []discord.CommandOption{
|
Options: []discord.CommandOption{
|
||||||
&discord.StringOption{
|
&discord.StringOption{
|
||||||
OptionName: "prompt",
|
OptionName: "prompt",
|
||||||
|
@ -40,7 +41,7 @@ var commands = []api.CreateCommandData{
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "pic",
|
Name: "pic",
|
||||||
Description: "Generate an image using Stable Diffusion!",
|
Description: "Generate an image using Stable Diffusion! Cooldown: 1 Minute.",
|
||||||
Options: []discord.CommandOption{
|
Options: []discord.CommandOption{
|
||||||
&discord.StringOption{
|
&discord.StringOption{
|
||||||
OptionName: "prompt",
|
OptionName: "prompt",
|
||||||
|
@ -51,7 +52,7 @@ var commands = []api.CreateCommandData{
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "hdpic",
|
Name: "hdpic",
|
||||||
Description: "Generate an image using DALL·E 3!",
|
Description: "Generate an image using DALL·E 3! Cooldown: 10 Minutes.",
|
||||||
Options: []discord.CommandOption{
|
Options: []discord.CommandOption{
|
||||||
&discord.StringOption{
|
&discord.StringOption{
|
||||||
OptionName: "prompt",
|
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 {
|
func (h *handler) cmdAsk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
|
||||||
// Cooldown Logic
|
// Cooldown Logic
|
||||||
// cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "ask")
|
cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "ask")
|
||||||
// if cachedVal != "nil" {
|
if cachedVal != "nil" {
|
||||||
// return &api.InteractionResponseData{
|
return &api.InteractionResponseData{
|
||||||
// Content: option.NewNullableString("Please wait for the cooldown!"),
|
Content: option.NewNullableString("Please wait for the cooldown!"),
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// lib.SetCache(data.Event.User.ID.String()+":"+"ask", data.Event.User.ID.String()+":"+"ask", 1)
|
lib.SetCache(data.Event.User.ID.String()+":"+"ask", data.Event.User.ID.String()+":"+"ask", 1)
|
||||||
|
|
||||||
// Command Logic
|
// Command Logic
|
||||||
var options struct {
|
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 {
|
func (h *handler) cmdPic(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
|
||||||
// Cooldown Logic
|
// Cooldown Logic
|
||||||
// cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "pic")
|
cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "pic")
|
||||||
// if cachedVal != "nil" {
|
if cachedVal != "nil" {
|
||||||
// return &api.InteractionResponseData{
|
return &api.InteractionResponseData{
|
||||||
// Content: option.NewNullableString("Please wait for the cooldown!"),
|
Content: option.NewNullableString("Please wait for the cooldown!"),
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// lib.SetCache(data.Event.User.ID.String()+":"+"pic", data.Event.User.ID.String()+":"+"pic", 1)
|
lib.SetCache(data.Event.User.ID.String()+":"+"pic", data.Event.User.ID.String()+":"+"pic", 1)
|
||||||
|
|
||||||
// Command Logic
|
// Command Logic
|
||||||
var options struct {
|
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 {
|
func (h *handler) cmdHDPic(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
|
||||||
// Cooldown Logic
|
// Cooldown Logic
|
||||||
// cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "hdpic")
|
cachedVal := lib.GetCache(data.Event.User.ID.String() + ":" + "hdpic")
|
||||||
// if cachedVal != "nil" {
|
if cachedVal != "nil" {
|
||||||
// return &api.InteractionResponseData{
|
return &api.InteractionResponseData{
|
||||||
// Content: option.NewNullableString("Please wait for the cooldown!"),
|
Content: option.NewNullableString("Please wait for the cooldown!"),
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// lib.SetCache(data.Event.User.ID.String()+":"+"hdpic", data.Event.User.ID.String()+":"+"hdpic", 10)
|
lib.SetCache(data.Event.User.ID.String()+":"+"hdpic", data.Event.User.ID.String()+":"+"hdpic", 10)
|
||||||
|
|
||||||
// Command Logic
|
// Command Logic
|
||||||
var options struct {
|
var options struct {
|
||||||
|
|
Loading…
Add table
Reference in a new issue