/
channel.go
81 lines (68 loc) · 2.42 KB
/
channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package twitch
import (
"context"
"strconv"
"golang.org/x/oauth2"
)
// ChannelModerator is a channel's moderator.
type ChannelModerator struct {
ID IDStr `json:"user_id"`
Name string `json:"user_name"`
}
// GetChannelModerators gets the channel's moderators.
//
// GET https://api.twitch.tv/helix/moderation/moderators
func (t *Twitch) GetChannelModerators(ctx context.Context, id int64, userToken *oauth2.Token) (mods []*ChannelModerator, newToken *oauth2.Token, err error) {
cli := t.clientForUser(ctx, userToken, setToken(&newToken))
url := helixRoot + "/moderation/moderators?broadcaster_id=" + strconv.FormatInt(id, 10)
mods, err = paginate[*ChannelModerator](ctx, cli, url, 100, 500)
return mods, newToken, err
}
// 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 (t *Twitch) ModifyChannel(ctx context.Context, broadcasterID int64, userToken *oauth2.Token, title *string, gameID *int64) (newToken *oauth2.Token, err error) {
if title == nil && gameID == nil {
return nil, ErrBadRequest
}
if title != nil && *title == "" {
return nil, ErrBadRequest
}
if userToken == nil || userToken.AccessToken == "" {
return nil, ErrNotAuthorized
}
cli := t.clientForUser(ctx, userToken, setToken(&newToken))
url := helixRoot + "/channels"
body := &struct {
BroadcasterID IDStr `json:"broadcaster_id"`
Title *string `json:"title,omitempty"`
GameID *IDStr `json:"game_id,omitempty"`
}{
BroadcasterID: IDStr(broadcasterID),
Title: title,
GameID: (*IDStr)(gameID),
}
resp, err := cli.Patch(ctx, url, body)
if err != nil {
return newToken, err
}
defer resp.Body.Close()
return newToken, statusToError(resp.StatusCode)
}
// Channel is a channel as exposed by the Helix API.
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"`
}
// GetChannelByID gets a channel using the client's token.
//
// GET https://api.twitch.tv/helix/channels?broadcaster_id<id>
func (t *Twitch) GetChannelByID(ctx context.Context, id int64) (*Channel, error) {
cli := t.helixCli
url := helixRoot + "/channels?broadcaster_id=" + strconv.FormatInt(id, 10)
return fetchFirstFromList[*Channel](ctx, cli, url)
}