Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sextou handler #2

Merged
merged 7 commits into from
Apr 3, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 103 additions & 3 deletions handlers/friday.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
Expand All @@ -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
Expand All @@ -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))

randomFridayGif := getRandomGif(fridayTrigger)
randomFridayGifUrl := extractGifFromGTenor(randomFridayGif, fridayGifUrl)

message.Files = append(message.Files, processGifUrl(randomFridayGifUrl))
case time.Thursday:
message.Content = "Quasee, mas ainda não"

randomThursdayGif := getRandomGif("falta-um-dia")
randomThursdayGifUrl := extractGifFromGTenor(randomThursdayGif, fallbackGifUrl)

message.Files = append(message.Files, processGifUrl(randomThursdayGifUrl))
default:
message.Content = fmt.Sprintf("Calma família ainda não é sexta! Falta %d dia(s)", daysRemainingToFriday())

randomGif := getRandomGif(time.Now().Weekday().String())
randomGifUrl := extractGifFromGTenor(randomGif, 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("q", q)
query.Add("key", "LIVDSRZULELA")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this line to get the token from the environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand, you say to edit specifically the commit that entered this key? Because in the last commit i changed to get from the env

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 {
Expand All @@ -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())
}
}
}