go-twitch/api/moderation/update_shield_mode_status.go

75 lines
2.3 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"
"io"
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
)
type UpdateShieldModeStatusParams struct {
// The ID of the broadcaster whose Shield Mode you want to activate or deactivate.
BroadcasterID string `url:"broadcaster_id"`
// The ID of the broadcaster or a user that is one of the broadcasters moderators. This ID must match the user ID in the access token.
ModeratorID string `url:"moderator_id"`
}
type UpdateShieldModeStatusRequest struct {
// A Boolean value that determines whether to activate Shield Mode. Set to true to activate Shield Mode; otherwise, false to deactivate Shield Mode.
IsActive bool `json:"is_active"`
}
type UpdateShieldModeStatusResponse struct {
// A list that contains a single object with the broadcasters updated Shield Mode status.
Data []ShieldModeStatus `json:"data"`
}
// Activates or deactivates the broadcasters Shield Mode.
//
// Twitchs Shield Mode feature is like a panic button that broadcasters can push to protect themselves from chat abuse coming from one or more accounts.
// When activated, Shield Mode applies the overrides that the broadcaster configured in the Twitch UX.
// If the broadcaster hasnt configured Shield Mode, it applies default overrides.
//
// Requires a user access token that includes the moderator:manage:shield_mode scope.
func (m *Moderation) UpdateShieldModeStatus(ctx context.Context, params *UpdateShieldModeStatusParams) (*UpdateShieldModeStatusResponse, error) {
v, _ := query.Values(params)
r, w := io.Pipe()
go func() {
if err := json.NewEncoder(w).Encode(UpdateShieldModeStatusRequest{IsActive: true}); err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(m.baseUrl, "moderation/shield_mode", v), r)
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 update shield mode status (%d)", res.StatusCode)
}
var data UpdateShieldModeStatusResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}