twitch

package
v0.0.0-...-fb49ba0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package twitch implements a Twitch API client. The client makes full use of OAuth tokens and requires them where needed.

Index

Constants

This section is empty.

Variables

View Source
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
View Source
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.

View Source
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 BanRequest struct {
	UserID   IDStr  `json:"user_id"`
	Duration int64  `json:"duration,omitempty"`
	Reason   string `json:"reason"`
}

type Category

type Category struct {
	ID   IDStr  `json:"id"`
	Name string `json:"name"`
}

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

type ChannelModerator struct {
	ID   IDStr  `json:"user_id"`
	Name string `json:"user_name"`
}

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 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) AsInt64

func (v IDStr) AsInt64() int64

AsInt64 returns the ID as an int64.

func (IDStr) MarshalJSON

func (v IDStr) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for IDStr.

func (*IDStr) UnmarshalJSON

func (v *IDStr) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for IDStr.

type ModeratedChannel

type ModeratedChannel struct {
	ID    IDStr  `json:"broadcaster_id"`
	Login string `json:"broadcaster_login"`
	Name  string `json:"broadcaster_name"`
}

type Option

type Option func(*Twitch)

Option sets an option on the Twitch client.

type Stream

type Stream struct {
	ID          IDStr     `json:"id"`
	GameID      IDStr     `json:"game_id"`
	Title       string    `json:"title"`
	StartedAt   time.Time `json:"started_at"`
	ViewerCount int       `json:"viewer_count"`
	UserID      IDStr     `json:"user_id"`
}

type Twitch

type Twitch struct {
	// contains filtered or unexported fields
}

Twitch is the Twitch API client.

func New

func New(clientID, clientSecret, redirectURL string, cli *http.Client) *Twitch

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.

POST https://api.twitch.tv/helix/chat/announcements

func (*Twitch) AuthCodeURL

func (t *Twitch) AuthCodeURL(state string, scopes []string) string

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.

POST https://api.twitch.tv/helix/moderation/bans

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.

DELETE https://api.twitch.tv/helix/moderation/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.

DELETE https://api.twitch.tv/helix/moderation/chat

func (*Twitch) Exchange

func (t *Twitch) Exchange(ctx context.Context, code string) (*oauth2.Token, error)

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

func (t *Twitch) GetChannelByID(ctx context.Context, id int64) (*Channel, error)

GetChannelByID gets a channel using the client's token.

GET https://api.twitch.tv/helix/channels?broadcaster_id<id>

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.

GET https://api.twitch.tv/helix/moderation/moderators

func (*Twitch) GetGameByID

func (t *Twitch) GetGameByID(ctx context.Context, id int64) (*Category, error)

GetGameByID queries for a game by ID.

GET https://api.twitch.tv/helix/games?id=<id>

func (*Twitch) GetGameByName

func (t *Twitch) GetGameByName(ctx context.Context, name string) (*Category, error)

GetGameByName queries for a game by name. The name must match exactly.

GET https://api.twitch.tv/helix/games?name=<name>

func (t *Twitch) GetGameLinks(ctx context.Context, twitchCategory int64) ([]GameLink, error)

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.

GET https://api.twitch.tv/helix/moderation/channels

func (*Twitch) GetStreamByUserID

func (t *Twitch) GetStreamByUserID(ctx context.Context, id int64) (*Stream, error)

GetStreamByUserID gets the current stream by user ID.

GET https://api.twitch.tv/helix/streams?user_id=<id>

func (*Twitch) GetStreamByUsername

func (t *Twitch) GetStreamByUsername(ctx context.Context, username string) (*Stream, error)

GetStreamByUsername gets the current stream by username.

GET https://api.twitch.tv/helix/streams?user_login=<username>

func (*Twitch) GetUserByID

func (t *Twitch) GetUserByID(ctx context.Context, id int64) (*User, error)

GetUserByID gets the Twitch user for the specified UD.

GET https://api.twitch.tv/helix/users?id=<id>

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.

GET https://api.twitch.tv/helix/users

func (*Twitch) GetUserByUsername

func (t *Twitch) GetUserByUsername(ctx context.Context, username string) (*User, error)

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.

PATCH https://api.twitch.tv/helix/channels

func (*Twitch) SearchCategories

func (t *Twitch) SearchCategories(ctx context.Context, query string) ([]*Category, error)

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.

PUT https://api.twitch.tv/helix/chat/color

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.

DELETE https://api.twitch.tv/helix/moderation/bans

func (*Twitch) UpdateChatSettings

func (t *Twitch) UpdateChatSettings(ctx context.Context, broadcasterID int64, modID int64, modToken *oauth2.Token, patch *ChatSettingsPatch) (newToken *oauth2.Token, err error)

UpdateChatSettings updates the current chat settings.

PATCH https://api.twitch.tv/helix/chat/settings

func (*Twitch) Validate

func (t *Twitch) Validate(ctx context.Context, tok *oauth2.Token) (*Validation, *oauth2.Token, error)

type User

type User struct {
	ID          IDStr  `json:"id"`
	Name        string `json:"login"`
	DisplayName string `json:"display_name,omitempty"`
}

User is a Twitch user.

func (User) DispName

func (u User) DispName() string

DispName returns the display name for the user if provided, otherwise the username.

type Validation

type Validation struct {
	UserID IDStr    `json:"user_id"`
	Name   string   `json:"name"`
	Scopes []string `json:"scopes"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL