From 59a3771973c2e851917ba6587c79b0ae18d5f981 Mon Sep 17 00:00:00 2001 From: luisgustavom1 Date: Sat, 1 Apr 2023 20:33:46 -0300 Subject: [PATCH] feat: add GTENOR_KEY --- .env.example | 3 ++- config/config.go | 8 +++++--- handlers/friday.go | 15 ++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 8a28e5e..5ec74f1 100644 --- a/.env.example +++ b/.env.example @@ -6,4 +6,5 @@ FRONTEND_ROLE_ID= BACKEND_ROLE_ID= MOBILE_ROLE_ID= FULLSTACK_ROLE_ID= -QA_ROLE_ID= \ No newline at end of file +QA_ROLE_ID= +GTENOR_KEY= \ No newline at end of file diff --git a/config/config.go b/config/config.go index 3174bf9..06b273d 100644 --- a/config/config.go +++ b/config/config.go @@ -11,6 +11,7 @@ type Config struct { GuildID string ChannelID string MessageID string + GTenorKey string RemoveCommands bool RoleMap map[string]string } @@ -25,6 +26,7 @@ func Load() (*Config, error) { cfg.GuildID = os.Getenv("GUILD_ID") cfg.ChannelID = os.Getenv("CHANNEL_ID") cfg.MessageID = os.Getenv("MESSAGE_ID") + cfg.GTenorKey = os.Getenv("GTENOR_KEY") cfg.RemoveCommands, err = strconv.ParseBool(os.Getenv("REMOVE_COMMANDS")) if err != nil { @@ -42,10 +44,10 @@ func Load() (*Config, error) { return cfg, nil } -func GetConfig() { - if !cfg { +func GetConfig() Config { + if cfg == nil { log.Fatal("You must load the config") } - return cfg + return *cfg } diff --git a/handlers/friday.go b/handlers/friday.go index 03766e4..97bd6d5 100644 --- a/handlers/friday.go +++ b/handlers/friday.go @@ -4,12 +4,13 @@ import ( "bytes" "encoding/json" "fmt" - "strings" - "time" "io" "net/http" + "strings" + "time" "github.com/bwmarrin/discordgo" + "github.com/liverday/medeiro-tech-bot/config" ) var ( @@ -21,6 +22,8 @@ var ( fallbackGifUrl = "https://tenor.com/view/dancing-random-duck-gif-25973520" ) +var cfg config.Config + type GTenorMinimalReturn struct { Results []struct { ID string `json:"id"` @@ -65,6 +68,8 @@ type GTenorMinimalReturn struct { } func FridayHandler(s *discordgo.Session, m *discordgo.MessageCreate) { + cfg = config.GetConfig() + if !strings.Contains(m.Content, fridayTrigger) { return } @@ -100,15 +105,15 @@ func FridayHandler(s *discordgo.Session, m *discordgo.MessageCreate) { s.ChannelMessageSendComplex(m.ChannelID, message) } -func getRandomGif(q string) (result GTenorMinimalReturn) { +func getRandomGif(search string) (result GTenorMinimalReturn) { req, err := http.NewRequest("GET", gTenorUrl+"/random", nil) if err != nil { fmt.Println("Cannot make a new http Request", err) } query := req.URL.Query() - query.Add("q", q) - query.Add("key", "LIVDSRZULELA") + query.Add("q", search) + query.Add("key", cfg.GTenorKey) query.Add("media_filter", "minimal") query.Add("limit", "1")