go-twitch/api/moderation/add_blocked_term.go

80 lines
2.3 KiB
Go
Raw Normal View History

2024-03-03 15:14:59 -05:00
package moderation
import (
"context"
"encoding/json"
"fmt"
2024-03-03 15:14:59 -05:00
"io"
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 15:14:59 -05:00
)
type AddBlockedTermParams struct {
// The ID of the broadcaster that owns the list of blocked terms.
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 AddBlockedTermRequest struct {
// The word or phrase to block from being used in the broadcasters chat room.
// The term must contain a minimum of 2 characters and may contain up to a maximum of 500 characters.
//
// Terms may include a wildcard character (*).
// The wildcard character must appear at the beginning or end of a word or set of characters.
// For example, *foo or foo*.
//
// If the blocked term already exists, the response contains the existing blocked term.
Text string `json:"text"`
}
type AddBlockedTermResponse struct {
// A list that contains the single blocked term that the broadcaster added.
Data []BlockedTerm `json:"data"`
}
// Adds a word or phrase to the broadcasters list of blocked terms.
// These are the terms that the broadcaster doesnt want used in their chat room.
//
// Requires a user access token that includes the moderator:manage:blocked_terms scope.
func (m *Moderation) AddBlockedTerm(ctx context.Context, params *AddBlockedTermParams, body *AddBlockedTermRequest) (*AddBlockedTermResponse, error) {
v, _ := query.Values(params)
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.Make(m.baseUrl, "moderation/blocked_terms", v), r)
2024-03-03 15:14:59 -05:00
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 add blocked term (%d)", res.StatusCode)
}
2024-03-03 15:14:59 -05:00
var data AddBlockedTermResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}