twitch

package
v0.0.0-...-9d56c50 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 24 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 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 ErrDeadToken = errors.New("twitch: dead token")
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)
	SendChatMessage(ctx context.Context, broadcasterID int64, senderID int64, senderToken *oauth2.Token, message string) (newToken *oauth2.Token, err error)
	GetConduits(ctx context.Context) ([]*Conduit, error)
	CreateConduit(ctx context.Context, shardCount int) (*Conduit, error)
	UpdateConduit(ctx context.Context, id string, shardCount int) (*Conduit, error)
	DeleteConduit(ctx context.Context, id string) error
	UpdateShards(ctx context.Context, conduitID string, shards []*Shard) error
	GetSubscriptions(ctx context.Context) ([]*eventsub.Subscription, error)
	DeleteSubscription(ctx context.Context, id string) error
	CreateChatSubscription(ctx context.Context, conduitID string, broadcasterID int64, botID int64) 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.IDStr `json:"user_id"`
	Duration int64       `json:"duration,omitempty"`
	Reason   string      `json:"reason"`
}

type Category

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

type Channel

type Channel struct {
	ID     idstr.IDStr `json:"broadcaster_id"`
	Name   string      `json:"broadcaster_name"`
	Game   string      `json:"game_name"`
	GameID idstr.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.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 Conduit

type Conduit struct {
	ID         string `json:"id"`
	ShardCount int    `json:"shard_count"`
}
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 ModeratedChannel

type ModeratedChannel struct {
	ID    idstr.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 Shard

type Shard struct {
	ID        idstr.IDStr        `json:"id"`
	Transport eventsub.Transport `json:"transport"`
}

type Stream

type Stream struct {
	ID          idstr.IDStr `json:"id"`
	GameID      idstr.IDStr `json:"game_id"`
	Title       string      `json:"title"`
	StartedAt   time.Time   `json:"started_at"`
	ViewerCount int         `json:"viewer_count"`
	UserID      idstr.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, ban *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) CreateChatSubscription

func (t *Twitch) CreateChatSubscription(ctx context.Context, conduitID string, broadcasterID int64, botID int64) error

func (*Twitch) CreateConduit

func (t *Twitch) CreateConduit(ctx context.Context, shardCount int) (*Conduit, error)

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

func (t *Twitch) DeleteConduit(ctx context.Context, id string) error

func (*Twitch) DeleteSubscription

func (t *Twitch) DeleteSubscription(ctx context.Context, id string) error

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

func (t *Twitch) GetConduits(ctx context.Context) ([]*Conduit, error)

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

func (t *Twitch) GetSubscriptions(ctx context.Context) ([]*eventsub.Subscription, error)

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

func (t *Twitch) SendChatMessage(ctx context.Context, broadcasterID int64, senderID int64, senderToken *oauth2.Token, message string) (newToken *oauth2.Token, err error)

SendChatMessage sends a chat message as the given user.

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

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

func (t *Twitch) UpdateConduit(ctx context.Context, id string, shardCount int) (*Conduit, error)

func (*Twitch) UpdateShards

func (t *Twitch) UpdateShards(ctx context.Context, conduitID string, shards []*Shard) error

func (*Twitch) Validate

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

type User

type User struct {
	ID          idstr.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.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