package main import ( "bytes" "encoding/json" "io" "net/http" ) type Variables struct { ID string `json:"id"` } type GqlQuery struct { OperationName string `json:"operationName"` Variables Variables `json:"variables"` Query string `json:"query"` } type SevenTv struct { Query GqlQuery `json:"query"` UserID string Badges struct { Data struct { Cosmetics struct { Badges []struct { ID string `json:"id"` Kind string `json:"kind"` Name string `json:"name"` Tooltip string `json:"tooltip"` Tag string `json:"tag"` Typename string `json:"__typename"` } Typename string `json:"__typename"` } `json:"cosmetics"` } `json:"data"` } `json:"sevenTvBadges"` } type Chatterino struct { Badges []struct { Tooltip string `json:"tooltip"` Image1 string `json:"image1"` Image2 string `json:"image2"` Image3 string `json:"image3"` Users []string `json:"users"` } `json:"badges"` } type Bttv struct{} type FrankerFz struct { Badge struct { ID int64 `json:"id"` Name string `json:"name"` Title string `json:"title"` Slot int64 `json:"slot"` Replaces *string `json:"replaces"` Color string `json:"color"` Image string `json:"image"` Urls map[string]string `json:"urls"` CSS interface{} `json:"css"` } `json:"badges"` Users map[string][]int64 `json:"users"` } type Chatty struct{} type DankChat struct { Type string `json:"type"` URL string `json:"url"` Users []string `json:"users"` } type Homies struct{} type PurpleTV struct{} func doGetRequest(url string) []byte { request, err := http.NewRequest("GET", url, nil) if err != nil { logger.Error().Msg(err.Error()) } res, err := client.Do(request) if err != nil { logger.Error().Msg(err.Error()) } defer res.Body.Close() body, err := io.ReadAll(res.Body) return body } func doPostRequest(url string, data []byte) []byte { request, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) if err != nil { logger.Error().Msg(err.Error()) } res, err := client.Do(request) if err != nil { logger.Error().Msg(err.Error()) } defer res.Body.Close() body, err := io.ReadAll(res.Body) return body } func (s *SevenTv) getUserID(userId string) (string, error) { req := doGetRequest("https://7tv.io/v3/users/twitch/" + userId) return string(req), nil } func (s *SevenTv) getBadges() *SevenTv { query := ` query GetCosmestics($list: [ObjectID!]) { cosmetics(list: $list) { badges { id kind name tooltip tag __typename } __typename } }` s.Query = GqlQuery{ OperationName: "GetCosmestics", Query: query, } data, err := json.Marshal(s.Query) if err != nil { logger.Error().Msg(err.Error()) } res := doPostRequest("https://7tv.io/v3/gql", data) logger.Trace().Msg(string(res)) err = json.Unmarshal(res, &s.Badges) if err != nil { logger.Error().Msg(err.Error()) } return s } func (s *SevenTv) getPaints() (string, error) { query := ` query GetCosmestics($list: [ObjectID!]) { cosmetics(list: $list) { paints { id kind name function color angle shape image_url repeat stops { at color __typename } shadows { x_offset y_offset radius color __typename } __typename } } }` s.Query = GqlQuery{ OperationName: "GetCosmestics", Query: query, } data, err := json.Marshal(s.Query) if err != nil { return "", err } logger.Trace().Msg(string(data)) res := doPostRequest("https://7tv.io/v3/gql", data) return string(res), nil } // Returns the costmetrics that a user has. // userId, needs to be a 7TV ULID, call getUserID first. func (s *SevenTv) getUserCosmetics(userId string) (string, error) { query := ` query GetUserCosmetics($id: ObjectID!) { user(id: $id) { id cosmetics { id kind selected __typename } __typename } }` s.Query = GqlQuery{ OperationName: "GetUserCosmetics", Variables: Variables{ ID: userId, }, Query: query, } data, err := json.Marshal(s.Query) if err != nil { return "", err } logger.Trace().Msg(string(data)) res := doPostRequest("https://7tv.io/v3/gql", data) return string(res), nil } func (s *Chatterino) getBadges() *Chatterino { res := doGetRequest("https://api.chatterino.com/badges") err := json.Unmarshal(res, &s) if err != nil { logger.Error().Msg(err.Error()) } return s } func (s *Bttv) getBadges() *Bttv { res := doGetRequest("https://api.betterttv.net/3/cached/badges/twitch") err := json.Unmarshal(res, &s) if err != nil { logger.Error().Msg(err.Error()) } return s } func (s *FrankerFz) getBadges() *FrankerFz { res := doGetRequest("https://api.frankerfacez.com/v1/badges/ids") err := json.Unmarshal(res, &s) if err != nil { logger.Error().Msg(err.Error()) } return s } func (s *Chatty) getBadges() *Chatty { res := doGetRequest("https://tduva.com/res/badges") err := json.Unmarshal(res, &s) if err != nil { logger.Error().Msg(err.Error()) } return s } func (s *DankChat) getBadges() *DankChat { res := doGetRequest("https://flxrs.com/api/badges") err := json.Unmarshal(res, &s) if err != nil { logger.Error().Msg(err.Error()) } return s }