From 509f216bbb4ef6e7685f067e60534a738cf63261 Mon Sep 17 00:00:00 2001 From: luisgustavom1 Date: Sat, 1 Apr 2023 00:50:17 -0300 Subject: [PATCH 1/7] feat: friday handler to send a gif when message content contains the fridayTrigger --- handlers/friday.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 handlers/friday.go diff --git a/handlers/friday.go b/handlers/friday.go new file mode 100644 index 0000000..e0fec80 --- /dev/null +++ b/handlers/friday.go @@ -0,0 +1,58 @@ +package handlers + +import ( + "bytes" + "fmt" + "strings" + "time" + + "io" + "net/http" + + "github.com/bwmarrin/discordgo" +) + +var ( + fridayTrigger = "sextou" + fridayGifUrl = "https://media.tenor.com/zGlEbV_bTnIAAAAC/kowalski-familia.gif" +) + +func FridayHandler(s *discordgo.Session, m *discordgo.MessageCreate) { + if !strings.Contains(m.Content, fridayTrigger) { + return + } + + message := &discordgo.MessageSend{ + Files: []*discordgo.File{}, + } + + switch time.Now().Weekday() { + case time.Friday: + message.Content = "Sextouu família" + message.Files = append(message.Files, processGifUrl(fridayGifUrl)) + default: + message.Content = "Calma lá família ainda não é sexta!" + } + + s.ChannelMessageSendComplex(m.ChannelID, message) +} + +func processGifUrl(url string) *discordgo.File { + res, err := http.Get(url) + if err != nil { + fmt.Println("Bad request", err) + } + defer res.Body.Close() + + body, err := io.ReadAll(res.Body) + if err != nil { + fmt.Println("Failed to get GIT", err) + } + + gifFile := &discordgo.File{ + Name: "sextou-familia.gif", + Reader: bytes.NewReader(body), + } + + return gifFile +} From a08a414d76ef33ab0062b23243fa68515f59cf84 Mon Sep 17 00:00:00 2001 From: luisgustavom1 Date: Sat, 1 Apr 2023 00:51:42 -0300 Subject: [PATCH 2/7] feat: add message handlers --- main.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/main.go b/main.go index 8d75bda..82b62bc 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,9 @@ var ( "source": handlers.SourceHandler, "short_url": handlers.ShortUrlHandler, } + messageHandlers = map[string]func(s *discordgo.Session, m *discordgo.MessageCreate){ + "friday": handlers.FridayHandler, + } ) var bot *discordgo.Session @@ -96,6 +99,11 @@ func addHandlers() { handler(s, i) } }) + bot.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { + for _, handler := range messageHandlers { + handler(s, m) + } + }) } func addCommands() []*discordgo.ApplicationCommand { From 85ff231eb7bbd6b710560cd6f4fe5d2e4665ff9e Mon Sep 17 00:00:00 2001 From: luisgustavom1 Date: Sat, 1 Apr 2023 00:59:48 -0300 Subject: [PATCH 3/7] Feat: add friday countdown --- handlers/friday.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/handlers/friday.go b/handlers/friday.go index e0fec80..03192f3 100644 --- a/handlers/friday.go +++ b/handlers/friday.go @@ -31,7 +31,7 @@ func FridayHandler(s *discordgo.Session, m *discordgo.MessageCreate) { message.Content = "Sextouu família" message.Files = append(message.Files, processGifUrl(fridayGifUrl)) default: - message.Content = "Calma lá família ainda não é sexta!" + message.Content = fmt.Sprintf("Calma família ainda não é sexta! Falta %d dia(s)", daysRemainingToFriday()) } s.ChannelMessageSendComplex(m.ChannelID, message) @@ -56,3 +56,13 @@ func processGifUrl(url string) *discordgo.File { return gifFile } + +func daysRemainingToFriday() int { + today := time.Now() + + if today.Weekday() > time.Friday { + return int(today.Weekday()) + } else { + return int(time.Friday) - int(today.Weekday()) + } +} \ No newline at end of file From c2cbdd9cbfa99a304a8e085a17488eacc3fc1397 Mon Sep 17 00:00:00 2001 From: luisgustavom1 Date: Sat, 1 Apr 2023 19:39:40 -0300 Subject: [PATCH 4/7] feat: get a random gif from tenor api --- handlers/friday.go | 106 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/handlers/friday.go b/handlers/friday.go index 03192f3..0268bc6 100644 --- a/handlers/friday.go +++ b/handlers/friday.go @@ -2,6 +2,7 @@ package handlers import ( "bytes" + "encoding/json" "fmt" "strings" "time" @@ -13,10 +14,57 @@ import ( ) var ( + gTenorUrl = "https://g.tenor.com/v1" + fridayTrigger = "sextou" fridayGifUrl = "https://media.tenor.com/zGlEbV_bTnIAAAAC/kowalski-familia.gif" + + fallbackGifUrl = "https://tenor.com/view/dancing-random-duck-gif-25973520" ) +type GTenorMinimalReturn struct { + Results []struct { + ID string `json:"id"` + Title string `json:"title"` + ContentDescription string `json:"content_description"` + ContentRating string `json:"content_rating"` + H1Title string `json:"h1_title"` + Media []struct { + Mp4 struct { + Dims []int `json:"dims"` + Preview string `json:"preview"` + Size int `json:"size"` + URL string `json:"url"` + Duration float64 `json:"duration"` + } `json:"mp4"` + Gif struct { + Size int `json:"size"` + URL string `json:"url"` + Preview string `json:"preview"` + Dims []int `json:"dims"` + } `json:"gif"` + Tinygif struct { + Dims []int `json:"dims"` + Size int `json:"size"` + Preview string `json:"preview"` + URL string `json:"url"` + } `json:"tinygif"` + } `json:"media"` + BgColor string `json:"bg_color"` + Created float64 `json:"created"` + Itemurl string `json:"itemurl"` + URL string `json:"url"` + Tags []interface{} `json:"tags"` + Flags []interface{} `json:"flags"` + Shares int `json:"shares"` + Hasaudio bool `json:"hasaudio"` + Hascaption bool `json:"hascaption"` + SourceID string `json:"source_id"` + Composite interface{} `json:"composite"` + } `json:"results"` + Next string `json:"next"` +} + func FridayHandler(s *discordgo.Session, m *discordgo.MessageCreate) { if !strings.Contains(m.Content, fridayTrigger) { return @@ -29,14 +77,66 @@ func FridayHandler(s *discordgo.Session, m *discordgo.MessageCreate) { switch time.Now().Weekday() { case time.Friday: message.Content = "Sextouu família" - message.Files = append(message.Files, processGifUrl(fridayGifUrl)) + + randomGTenorGif := getRandomGif(fridayTrigger) + randomGifUrl := extractGifFromGTenor(randomGTenorGif, fridayGifUrl) + + message.Files = append(message.Files, processGifUrl(randomGifUrl)) + case time.Thursday: + message.Content = "Quasee, mas ainda não" + + randomThursdayGif := getRandomGif("falta-um-dia") + randomGifUrl := extractGifFromGTenor(randomThursdayGif, fallbackGifUrl) + + message.Files = append(message.Files, processGifUrl(randomGifUrl)) default: message.Content = fmt.Sprintf("Calma família ainda não é sexta! Falta %d dia(s)", daysRemainingToFriday()) + + randomWeekdayGif := getRandomGif(time.Now().Weekday().String()) + randomGifUrl := extractGifFromGTenor(randomWeekdayGif, fallbackGifUrl) + + message.Files = append(message.Files, processGifUrl(randomGifUrl)) } s.ChannelMessageSendComplex(m.ChannelID, message) } +func getRandomGif(q 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("key", "LIVDSRZULELA") + query.Add("q", q) + query.Add("media_filter", "minimal") + query.Add("limit", "1") + + req.URL.RawQuery = query.Encode() + + client := http.Client{} + res, err := client.Do(req) + if err != nil { + fmt.Println("Error on get a random gif", err) + } + + body, _ := io.ReadAll(res.Body) + if err := json.Unmarshal(body, &result); err != nil { + fmt.Println("Can not unmarshall JSON", err) + } + + return result +} + +func extractGifFromGTenor(gTenor GTenorMinimalReturn, fallback string) string { + if len(gTenor.Results) > 0 && len(gTenor.Results[0].Media) > 0 { + return gTenor.Results[0].Media[0].Gif.URL + } + + return fallback +} + func processGifUrl(url string) *discordgo.File { res, err := http.Get(url) if err != nil { @@ -63,6 +163,6 @@ func daysRemainingToFriday() int { if today.Weekday() > time.Friday { return int(today.Weekday()) } else { - return int(time.Friday) - int(today.Weekday()) + return int(time.Friday) - int(today.Weekday()) } -} \ No newline at end of file +} From 37d2a46b59b2144869b21f8d22b1cd23377962f3 Mon Sep 17 00:00:00 2001 From: luisgustavom1 Date: Sat, 1 Apr 2023 19:48:12 -0300 Subject: [PATCH 5/7] refactor: more semantic variables --- handlers/friday.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/handlers/friday.go b/handlers/friday.go index 0268bc6..1da4409 100644 --- a/handlers/friday.go +++ b/handlers/friday.go @@ -78,22 +78,22 @@ func FridayHandler(s *discordgo.Session, m *discordgo.MessageCreate) { case time.Friday: message.Content = "Sextouu família" - randomGTenorGif := getRandomGif(fridayTrigger) - randomGifUrl := extractGifFromGTenor(randomGTenorGif, fridayGifUrl) + randomFridayGif := getRandomGif(fridayTrigger) + randomFridayGifUrl := extractGifFromGTenor(randomFridayGif, fridayGifUrl) - message.Files = append(message.Files, processGifUrl(randomGifUrl)) + message.Files = append(message.Files, processGifUrl(randomFridayGifUrl)) case time.Thursday: message.Content = "Quasee, mas ainda não" randomThursdayGif := getRandomGif("falta-um-dia") - randomGifUrl := extractGifFromGTenor(randomThursdayGif, fallbackGifUrl) + randomThursdayGifUrl := extractGifFromGTenor(randomThursdayGif, fallbackGifUrl) - message.Files = append(message.Files, processGifUrl(randomGifUrl)) + message.Files = append(message.Files, processGifUrl(randomThursdayGifUrl)) default: message.Content = fmt.Sprintf("Calma família ainda não é sexta! Falta %d dia(s)", daysRemainingToFriday()) - randomWeekdayGif := getRandomGif(time.Now().Weekday().String()) - randomGifUrl := extractGifFromGTenor(randomWeekdayGif, fallbackGifUrl) + randomGif := getRandomGif(time.Now().Weekday().String()) + randomGifUrl := extractGifFromGTenor(randomGif, fallbackGifUrl) message.Files = append(message.Files, processGifUrl(randomGifUrl)) } @@ -108,8 +108,8 @@ func getRandomGif(q string) (result GTenorMinimalReturn) { } query := req.URL.Query() - query.Add("key", "LIVDSRZULELA") query.Add("q", q) + query.Add("key", "LIVDSRZULELA") query.Add("media_filter", "minimal") query.Add("limit", "1") From 60600e72b9e1df971619abb8bdb29e429600e334 Mon Sep 17 00:00:00 2001 From: luisgustavom1 Date: Sat, 1 Apr 2023 20:23:57 -0300 Subject: [PATCH 6/7] refactor: change thursday gif --- config/config.go | 9 +++++++++ handlers/friday.go | 5 ++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 48993c7..3174bf9 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,7 @@ package config import ( + "log" "os" "strconv" ) @@ -40,3 +41,11 @@ func Load() (*Config, error) { return cfg, nil } + +func GetConfig() { + if !cfg { + log.Fatal("You must load the config") + } + + return cfg +} diff --git a/handlers/friday.go b/handlers/friday.go index 1da4409..03766e4 100644 --- a/handlers/friday.go +++ b/handlers/friday.go @@ -6,7 +6,6 @@ import ( "fmt" "strings" "time" - "io" "net/http" @@ -83,9 +82,9 @@ func FridayHandler(s *discordgo.Session, m *discordgo.MessageCreate) { message.Files = append(message.Files, processGifUrl(randomFridayGifUrl)) case time.Thursday: - message.Content = "Quasee, mas ainda não" + message.Content = "Quase, mas ainda não" - randomThursdayGif := getRandomGif("falta-um-dia") + randomThursdayGif := getRandomGif("quase-la") randomThursdayGifUrl := extractGifFromGTenor(randomThursdayGif, fallbackGifUrl) message.Files = append(message.Files, processGifUrl(randomThursdayGifUrl)) From 59a3771973c2e851917ba6587c79b0ae18d5f981 Mon Sep 17 00:00:00 2001 From: luisgustavom1 Date: Sat, 1 Apr 2023 20:33:46 -0300 Subject: [PATCH 7/7] 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")