90 lines
2.9 KiB
Go
90 lines
2.9 KiB
Go
package chat
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
|
||
"github.com/google/go-querystring/query"
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
"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 broadcaster’s 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 cursor’s 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 broadcaster’s 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 broadcaster’s 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 that’s connected to the broadcaster’s chat room.
|
||
UserID string `json:"user_id"`
|
||
|
||
// The user’s login name.
|
||
UserLogin string `json:"user_login"`
|
||
|
||
// The user’s display name.
|
||
UserName string `json:"user_name"`
|
||
}
|
||
|
||
// Gets the list of users that are connected to the broadcaster’s 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)
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(c.baseUrl, "chat/chatters", v), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res, err := c.client.Do(req)
|
||
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)
|
||
}
|
||
|
||
var response GetChattersResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &response, nil
|
||
}
|