go-twitch/api/chat/get_chatters.go

91 lines
3.0 KiB
Go
Raw Normal View History

2024-02-28 10:30:20 -05:00
package chat
import (
"context"
"encoding/json"
"fmt"
2024-02-28 10:30:20 -05:00
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/types"
)
type GetChattersParams struct {
// The ID of the broadcaster whose list of chatters you want to get.
BroadcasterID string `url:"broadcaster_id"`
// The ID of the broadcaster or one of the broadcasters moderators.
// This ID must match the user ID in the user access token.
ModeratorID string `url:"moderator_id"`
// The maximum number of items to return per page in the response.
// The minimum page size is 1 item per page and the maximum is 1,000.
// The default is 100.
First *int `url:"first,omitempty"`
// The cursor used to get the next page of results. The Pagination object in the response contains the cursors value.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
After *types.Cursor `url:"after,omitempty"`
}
type GetChattersResponse struct {
// The list of users that are connected to the broadcasters chat room.
// The list is empty if no users are connected to the chat room.
Data []Chatter `json:"data"`
// Contains the information used to page through the list of results. The object is empty if there are no more pages left to page through.
// Read more: https://dev.twitch.tv/docs/api/guide#pagination
Pagination types.Pagination `json:"pagination"`
// The total number of users that are connected to the broadcasters chat room.
// As you page through the list, the number of users may change as users join and leave the chat room.
Total int `json:"total"`
}
type Chatter struct {
// The ID of a user thats connected to the broadcasters chat room.
UserID string `json:"user_id"`
// The users login name.
UserLogin string `json:"user_login"`
// The users display name.
UserName string `json:"user_name"`
}
// Gets the list of users that are connected to the broadcasters chat session.
//
// NOTE: There is a delay between when users join and leave a chat and when the list is updated accordingly.
//
// To determine whether a user is a moderator or VIP, use the Get Moderators and Get VIPs endpoints. You can check the roles of up to 100 users.
//
// Requires a user access token that includes the moderator:read:chatters scope.
func (c *Chat) GetChatters(ctx context.Context, params *GetChattersParams) (*GetChattersResponse, error) {
v, _ := query.Values(params)
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "chat/chatters", RawQuery: v.Encode()})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := c.client.Do(req)
2024-02-28 10:30:20 -05:00
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 chatters (%d)", res.StatusCode)
}
2024-02-28 10:30:20 -05:00
var response GetChattersResponse
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
2024-02-28 10:30:20 -05:00
return nil, err
}
return &response, nil
}