go-twitch/api/chat/get_chatters.go

85 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package chat
import (
"context"
"encoding/json"
"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
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var response GetChattersResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return &response, nil
}