go-twitch/api/moderation/get_banned_users.go

110 lines
3.7 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 moderation
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
"go.fifitido.net/twitch/api/types"
)
type GetBannedUsersParams struct {
// The ID of the broadcaster whose list of banned users you want to get. This ID must match the user ID in the access token.
BroadcasterID string `url:"broadcaster_id"`
// A list of user IDs used to filter the results.
// To specify more than one ID, include this parameter for each user you want to get.
// For example, user_id=1234&user_id=5678.
// You may specify a maximum of 100 IDs.
//
// The returned list includes only those users that were banned or put in a timeout.
// The list is returned in the same order that you specified the IDs.
UserID []string `url:"user_id,omitempty"`
// 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 100 items per page.
// The default is 20.
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"`
// The cursor used to get the previous page of results. The Pagination object in the response contains the cursors value.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
Before *types.Cursor `url:"before,omitempty"`
}
type GetBannedUsersResponse struct {
// The list of banned users. The list contains a single object that contains all the banned users.
Data []GetBannedUsersResponseData `json:"data"`
// Contains information about the pagination in the response.
// The object is empty if there are no more pages of results.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
Pagination types.Pagination `json:"pagination"`
}
type GetBannedUsersResponseData struct {
// The ID of the banned user.
UserID string `json:"user_id"`
// The banned users login name.
UserLogin string `json:"user_login"`
// The banned users display name.
UserName string `json:"user_name"`
// The UTC date and time (in RFC3339 format) of when the timeout expires, or an empty string if the user is permanently banned.
ExpiresAt time.Time `json:"expires_at"`
// The UTC date and time (in RFC3339 format) of when the user was banned.
CreatedAt time.Time `json:"created_at"`
// The reason the user was banned or put in a timeout if the moderator provided one.
Reason string `json:"reason"`
// The ID of the moderator that banned the user or put them in a timeout.
ModeratorID string `json:"moderator_id"`
// The moderators login name.
ModeratorLogin string `json:"moderator_login"`
// The moderators display name.
ModeratorName string `json:"moderator_name"`
}
// Gets all users that the broadcaster banned or put in a timeout.
//
// Requires a user access token that includes the moderation:read or moderator:manage:banned_users scope.
func (m *Moderation) GetBannedUsers(ctx context.Context, params *GetBannedUsersParams) (*GetBannedUsersResponse, error) {
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(m.baseUrl, "moderation/banned", v), nil)
if err != nil {
return nil, err
}
res, err := m.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 banned users (%d)", res.StatusCode)
}
var data GetBannedUsersResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}