go-twitch/api/moderation/ban_user.go

102 lines
3.1 KiB
Go
Raw Normal View History

2024-03-03 15:14:59 -05:00
package moderation
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"time"
"github.com/google/go-querystring/query"
)
type BanUserParams struct {
// The ID of the broadcaster whose chat room the user is being banned from.
BroadcasterID string `url:"broadcaster_id"`
// The ID of the broadcaster or a user that has permission to moderate the broadcasters chat room.
// This ID must match the user ID in the user access token.
ModeratorID string `url:"moderator_id"`
}
type BanUserRequest struct {
// The ID of the user to ban or put in a timeout.
UserID string `json:"user_id"`
// To ban a user indefinitely, dont include this field.
//
// To put a user in a timeout, include this field and specify the timeout period, in seconds.
// The minimum timeout is 1 second and the maximum is 1,209,600 seconds (2 weeks).
Duration *int `json:"duration"`
// The reason the youre banning the user or putting them in a timeout.
// The text is user defined and is limited to a maximum of 500 characters.
Reason *string `json:"reason"`
}
type BanUserResponse struct {
// Identifies the user and type of ban.
Data []BanUserResponseData `json:"data"`
}
type BanUserResponseData struct {
// The broadcaster whose chat room the user was banned from chatting in.
BroadcasterID string `json:"broadcaster_id"`
// The moderator that banned or put the user in the timeout.
ModeratorID string `json:"moderator_id"`
// The user that was banned or put in a timeout.
UserID string `json:"user_id"`
// The UTC date and time (in RFC3339 format) that the ban or timeout was placed.
CreatedAt time.Time `json:"created_at"`
// The UTC date and time (in RFC3339 format) that the timeout will end. Is null if the user was banned instead of being put in a timeout.
EndTime *time.Time `json:"end_time"`
}
// Bans a user from participating in the specified broadcasters chat room or puts them in a timeout.
//
// For information about banning or putting users in a timeout, see Ban a User and Timeout a User.
//
// If the user is currently in a timeout, you can call this endpoint to change the duration of the timeout or ban them altogether.
// If the user is currently banned, you cannot call this method to put them in a timeout instead.
//
// To remove a ban or end a timeout, see Unban user.
//
// Requires a user access token that includes the moderator:manage:banned_users scope.
func (m *Moderation) BanUser(ctx context.Context, params *BanUserParams, body *BanUserRequest) (*BanUserResponse, error) {
v, _ := query.Values(params)
endpoint := m.baseUrl.ResolveReference(&url.URL{Path: "moderation/bans", RawQuery: v.Encode()})
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(body); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), r)
if err != nil {
return nil, err
}
res, err := m.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var data BanUserResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}