go-twitch/api/chat/get_global_emotes.go

92 lines
3.0 KiB
Go

package chat
import (
"context"
"encoding/json"
"fmt"
"net/http"
"go.fifitido.net/twitch/api/endpoint"
)
type GetGlobalEmotesResponse struct {
// The list of global emotes.
Data []GlobalEmote `json:"data"`
// A templated URL. Use the values from the id, format, scale, and theme_mode fields to replace the like-named placeholder strings
// in the templated URL to create a CDN (content delivery network) URL that you use to fetch the emote.
// For information about what the template looks like and how to use it to fetch emotes.
// See Emote CDN URL format: https://dev.twitch.tv/docs/irc/emotes#cdn-template
// You should use this template instead of using the URLs in the images object.
Template string `json:"template"`
}
type GlobalEmote struct {
// An ID that identifies this emote.
ID string `json:"id"`
// The name of the emote. This is the name that viewers type in the chat window to get the emote to appear.
Name string `json:"name"`
// The image URLs for the emote. These image URLs always provide a static, non-animated emote image with a light background.
//
// NOTE: You should use the templated URL in the template field to fetch the image instead of using these URLs.
Images EmoteImages `json:"images"`
// The formats that the emote is available in.
// For example, if the emote is available only as a static PNG, the array contains only static.
// But if the emote is available as a static PNG and an animated GIF, the array contains static and animated.
// The possible formats are:
//
// animated — An animated GIF is available for this emote.
//
// static — A static PNG file is available for this emote.
Formats []EmoteFormat `json:"format"`
// The sizes that the emote is available in.
// For example, if the emote is available in small and medium sizes, the array contains 1.0 and 2.0.
// Possible sizes are:
//
// 1.0 — A small version (28px x 28px) is available.
//
// 2.0 — A medium version (56px x 56px) is available.
//
// 3.0 — A large version (112px x 112px) is available.
Scales []EmoteScale `json:"scale"`
// The background themes that the emote is available in. Possible themes are:
//
// dark, light
ThemeMode []EmoteThemeMode `json:"theme_mode"`
}
// Gets the list of global emotes. Global emotes are Twitch-created emotes that users can use in any Twitch chat.
//
// Learn More: https://dev.twitch.tv/docs/irc/emotes
//
// Requires an app access token or user access token.
func (c *Chat) GetGlobalEmotes(ctx context.Context) (*GetGlobalEmotesResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(c.baseUrl, "chat/emotes/global"), nil)
if err != nil {
return nil, err
}
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to get global emotes (%d)", res.StatusCode)
}
var data GetGlobalEmotesResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}