Documentation ¶
Overview ¶
Package twitch implements a Twitch API client. The client makes full use of OAuth tokens and requires them where needed.
Index ¶
- Variables
- type API
- type BanRequest
- type Category
- type Channel
- type ChannelModerator
- type ChatSettingsPatch
- type GameLink
- type GameLinkType
- type IDStr
- type ModeratedChannel
- type Option
- type Stream
- type Twitch
- func (t *Twitch) Announce(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, ...) (newToken *oauth2.Token, err error)
- func (t *Twitch) AuthCodeURL(state string, scopes []string) string
- func (t *Twitch) Ban(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, ...) (newToken *oauth2.Token, err error)
- func (t *Twitch) ClearChat(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token) (newToken *oauth2.Token, err error)
- func (t *Twitch) DeleteChatMessage(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, ...) (newToken *oauth2.Token, err error)
- func (t *Twitch) Exchange(ctx context.Context, code string) (*oauth2.Token, error)
- func (t *Twitch) GetChannelByID(ctx context.Context, id int64) (*Channel, error)
- func (t *Twitch) GetChannelModerators(ctx context.Context, id int64, userToken *oauth2.Token) (mods []*ChannelModerator, newToken *oauth2.Token, err error)
- func (t *Twitch) GetGameByID(ctx context.Context, id int64) (*Category, error)
- func (t *Twitch) GetGameByName(ctx context.Context, name string) (*Category, error)
- func (t *Twitch) GetGameLinks(ctx context.Context, twitchCategory int64) ([]GameLink, error)
- func (t *Twitch) GetModeratedChannels(ctx context.Context, modID int64, modToken *oauth2.Token) (channels []*ModeratedChannel, newToken *oauth2.Token, err error)
- func (t *Twitch) GetStreamByUserID(ctx context.Context, id int64) (*Stream, error)
- func (t *Twitch) GetStreamByUsername(ctx context.Context, username string) (*Stream, error)
- func (t *Twitch) GetUserByID(ctx context.Context, id int64) (*User, error)
- func (t *Twitch) GetUserByToken(ctx context.Context, userToken *oauth2.Token) (user *User, newToken *oauth2.Token, err error)
- func (t *Twitch) GetUserByUsername(ctx context.Context, username string) (*User, error)
- func (t *Twitch) ModifyChannel(ctx context.Context, broadcasterID int64, userToken *oauth2.Token, ...) (newToken *oauth2.Token, err error)
- func (t *Twitch) SearchCategories(ctx context.Context, query string) ([]*Category, error)
- func (t *Twitch) SetChatColor(ctx context.Context, userID int64, userToken *oauth2.Token, color string) (newToken *oauth2.Token, err error)
- func (t *Twitch) Unban(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, ...) (newToken *oauth2.Token, err error)
- func (t *Twitch) UpdateChatSettings(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, ...) (newToken *oauth2.Token, err error)
- func (t *Twitch) Validate(ctx context.Context, tok *oauth2.Token) (*Validation, *oauth2.Token, error)
- type User
- type Validation
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("twitch: not found") ErrNotAuthorized = errors.New("twitch: not authorized") ErrBadRequest = errors.New("twitch: bad request") ErrServerError = errors.New("twitch: server error") ErrUnknown = errors.New("twitch: unknown error") ErrDeadToken = errors.New("twitch: oauth token is dead") )
Twitch API errors.
- 200 -> nil
- 400 -> ErrBadRequest
- 404 -> ErrNotFound
- 401 or 403 -> ErrNotAuthorized
- 5xx -> ErrServerError
- Otherwise -> ErrUnknown
var BotScopes = slices.Concat(UserScopes, []string{
"channel:moderate",
"chat:read",
"chat:edit",
"whispers:read",
"whispers:edit",
"moderator:manage:announcements",
"moderator:manage:banned_users",
"moderator:manage:chat_messages",
"moderator:read:chat_settings",
"moderator:manage:chat_settings",
"user:manage:chat_color",
"user:bot",
"user:read:chat",
"user:write:chat",
"user:read:moderated_channels",
"user:manage:whispers",
})
BotScopes are scopes which should be granted for the bot's account.
var UserScopes = []string{
"moderation:read",
"user:read:broadcast",
"channel:read:subscriptions",
"channel:read:editors",
"channel:manage:broadcast",
"channel:bot",
}
UserScopes should be granted for end users.
Functions ¶
This section is empty.
Types ¶
type API ¶
type API interface { // Auth AuthCodeURL(state string, scopes []string) string Exchange(ctx context.Context, code string) (*oauth2.Token, error) Validate(ctx context.Context, tok *oauth2.Token) (*Validation, *oauth2.Token, error) // Helix GetUserByToken(ctx context.Context, userToken *oauth2.Token) (user *User, newToken *oauth2.Token, err error) GetUserByUsername(ctx context.Context, username string) (*User, error) GetUserByID(ctx context.Context, id int64) (*User, error) GetChannelModerators(ctx context.Context, id int64, userToken *oauth2.Token) (mods []*ChannelModerator, newToken *oauth2.Token, err error) SearchCategories(ctx context.Context, query string) ([]*Category, error) ModifyChannel(ctx context.Context, broadcasterID int64, userToken *oauth2.Token, title *string, gameID *int64) (newToken *oauth2.Token, err error) GetGameByName(ctx context.Context, name string) (*Category, error) GetGameByID(ctx context.Context, id int64) (*Category, error) GetStreamByUserID(ctx context.Context, id int64) (*Stream, error) GetStreamByUsername(ctx context.Context, username string) (*Stream, error) GetChannelByID(ctx context.Context, id int64) (*Channel, error) Ban(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, req *BanRequest) (newToken *oauth2.Token, err error) Unban(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, userID int64) (newToken *oauth2.Token, err error) UpdateChatSettings(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, patch *ChatSettingsPatch) (newToken *oauth2.Token, err error) SetChatColor(ctx context.Context, userID int64, userToken *oauth2.Token, color string) (newToken *oauth2.Token, err error) DeleteChatMessage(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, id string) (newToken *oauth2.Token, err error) ClearChat(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token) (newToken *oauth2.Token, err error) Announce(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, message string, color string) (newToken *oauth2.Token, err error) GetModeratedChannels(ctx context.Context, modID int64, modToken *oauth2.Token) (channels []*ModeratedChannel, newToken *oauth2.Token, err error) // IGDB GetGameLinks(ctx context.Context, twitchCategory int64) ([]GameLink, error) }
API covers the main API methods for Twitch.
type BanRequest ¶
type Channel ¶
type Channel struct { ID IDStr `json:"broadcaster_id"` Name string `json:"broadcaster_name"` Game string `json:"game_name"` GameID IDStr `json:"game_id"` Title string `json:"title"` }
Channel is a channel as exposed by the Helix API.
type ChannelModerator ¶
ChannelModerator is a channel's moderator.
type ChatSettingsPatch ¶
type ChatSettingsPatch struct { EmoteMode *bool `json:"emote_mode,omitempty"` FollowerMode *bool `json:"follower_mode,omitempty"` FollowerModeDuration *int64 `json:"follower_mode_duration,omitempty"` NonModeratorChatDelay *bool `json:"non_moderator_chat_delay,omitempty"` NonModeratorChatDelayDuration *int64 `json:"non_moderator_chat_delay_duration,omitempty"` SlowMode *bool `json:"slow_mode,omitempty"` SlowModeWaitTime *int64 `json:"slow_mode_wait_time,omitempty"` SubscriberMode *bool `json:"subscriber_mode,omitempty"` UniqueChatMode *bool `json:"unique_chat_mode,omitempty"` }
type GameLink ¶
type GameLink struct { Type GameLinkType `json:"category"` URL string `json:"url"` }
type GameLinkType ¶
type GameLinkType uint8
const ( GameLinkSteam GameLinkType GameLinkEpic GameLinkGOG GameLinkItch GameLinkOfficial )
func (GameLinkType) String ¶
func (i GameLinkType) String() string
type IDStr ¶
type IDStr int64
IDStr is an int64 that is represented as a string in JSON, but can be parsed as either a string or a raw integer.
https://stackoverflow.com/a/31625512
func (IDStr) MarshalJSON ¶
MarshalJSON implements json.Marshaler for IDStr.
func (*IDStr) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler for IDStr.
type ModeratedChannel ¶
type Twitch ¶
type Twitch struct {
// contains filtered or unexported fields
}
Twitch is the Twitch API client.
func New ¶
New creates a new Twitch client. A client ID, client secret, and redirect URL are required; if not provided, New will panic.
func (*Twitch) Announce ¶
func (t *Twitch) Announce(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, message string, color string) (newToken *oauth2.Token, err error)
Announce makes an announcement in chat.
func (*Twitch) AuthCodeURL ¶
AuthCodeURL returns a URL a user can visit to grant permission for the client, and callback to a page with the code to exchange for a token.
state should be randomly generated, i.e. a random UUID which is then mapped back through some other lookup.
extraScopes can be specified to request more scopes than the defaults.
func (*Twitch) Ban ¶
func (t *Twitch) Ban(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, req *BanRequest) (newToken *oauth2.Token, err error)
Ban bans a user in a channel as a particular moderator. A duration of zero will cause a permanent ban.
func (*Twitch) ClearChat ¶
func (t *Twitch) ClearChat(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token) (newToken *oauth2.Token, err error)
ClearChat deletes all messages in chat.
func (*Twitch) DeleteChatMessage ¶
func (t *Twitch) DeleteChatMessage(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, id string) (newToken *oauth2.Token, err error)
DeleteChatMessage deletes a message from chat.
func (*Twitch) Exchange ¶
Exchange provides the Twitch OAuth server with the code and exchanges it for an OAuth token for the user who provided the code.
func (*Twitch) GetChannelByID ¶
GetChannelByID gets a channel using the client's token.
func (*Twitch) GetChannelModerators ¶
func (t *Twitch) GetChannelModerators(ctx context.Context, id int64, userToken *oauth2.Token) (mods []*ChannelModerator, newToken *oauth2.Token, err error)
GetChannelModerators gets the channel's moderators.
func (*Twitch) GetGameByID ¶
GetGameByID queries for a game by ID.
func (*Twitch) GetGameByName ¶
GetGameByName queries for a game by name. The name must match exactly.
GET https://api.twitch.tv/helix/games?name=<name>
func (*Twitch) GetGameLinks ¶
GetGameLinks gets a Twitch game's links to other services. Results are returned in this order, with unknown matches removed:
- Steam
- Epic
- GOG
func (*Twitch) GetModeratedChannels ¶
func (t *Twitch) GetModeratedChannels(ctx context.Context, modID int64, modToken *oauth2.Token) (channels []*ModeratedChannel, newToken *oauth2.Token, err error)
GetModeratedChannels gets the channels the user moderates.
func (*Twitch) GetStreamByUserID ¶
GetStreamByUserID gets the current stream by user ID.
func (*Twitch) GetStreamByUsername ¶
GetStreamByUsername gets the current stream by username.
GET https://api.twitch.tv/helix/streams?user_login=<username>
func (*Twitch) GetUserByID ¶
GetUserByID gets the Twitch user for the specified UD.
func (*Twitch) GetUserByToken ¶
func (t *Twitch) GetUserByToken(ctx context.Context, userToken *oauth2.Token) (user *User, newToken *oauth2.Token, err error)
GetUserByToken gets the Twitch user for the specified token.
func (*Twitch) GetUserByUsername ¶
GetUserByUsername gets the Twitch user for the specified username.
GET https://api.twitch.tv/helix/users?login=<username>
func (*Twitch) ModifyChannel ¶
func (t *Twitch) ModifyChannel(ctx context.Context, broadcasterID int64, userToken *oauth2.Token, title *string, gameID *int64) (newToken *oauth2.Token, err error)
ModifyChannel modifies a channel. Either or both of the title and game ID must be provided. The title must not be empty. If zero, the game will be unset.
func (*Twitch) SearchCategories ¶
SearchCategories searches for categories that match the specified query.
GET https://api.twitch.tv/helix/search/categories?query=<query>
func (*Twitch) SetChatColor ¶
func (t *Twitch) SetChatColor(ctx context.Context, userID int64, userToken *oauth2.Token, color string) (newToken *oauth2.Token, err error)
SetChatColor sets the chat color for a user.
func (*Twitch) Unban ¶
func (t *Twitch) Unban(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, userID int64) (newToken *oauth2.Token, err error)
Unban unbans a user from a channel.
type User ¶
type User struct { ID IDStr `json:"id"` Name string `json:"login"` DisplayName string `json:"display_name,omitempty"` }
User is a Twitch user.