go-twitch/api/chat/update_chat_settings.go

131 lines
4.7 KiB
Go
Raw Normal View History

2024-02-28 10:30:20 -05:00
package chat
import (
"context"
"encoding/json"
"fmt"
2024-02-28 10:30:20 -05:00
"io"
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-02-28 10:30:20 -05:00
)
type UpdateChatSettingsParams struct {
// The ID of the broadcaster whose chat settings you want to update.
BroadcasterID string `url:"broadcaster_id"`
// The ID of a user that has permission to moderate the broadcasters chat room, or the broadcasters ID if theyre making the update.
// This ID must match the user ID in the user access token.
ModeratorID string `url:"moderator_id"`
}
type UpdateChatSettingsRequest struct {
// A Boolean value that determines whether chat messages must contain only emotes.
//
// Set to true if only emotes are allowed; otherwise, false. The default is false.
EmoteMode *bool `json:"emote_mode"`
// A Boolean value that determines whether the broadcaster restricts the chat room to followers only.
//
// Set to true if the broadcaster restricts the chat room to followers only; otherwise, false. The default is true.
//
// To specify how long users must follow the broadcaster before being able to participate in the chat room, see the follower_mode_duration field.
FollowerMode *bool `json:"follower_mode"`
// The length of time, in minutes, that users must follow the broadcaster before being able to participate in the chat room.
// Set only if follower_mode is true.
// Possible values are: 0 (no restriction) through 129600 (3 months).
// The default is 0.
FollowerModeDuration *int `json:"follower_mode_duration"`
// A Boolean value that determines whether the broadcaster adds a short delay before chat messages appear in the chat room.
// This gives chat moderators and bots a chance to remove them before viewers can see the message.
//
// Set to true if the broadcaster applies a delay; otherwise, false.
// The default is false.
//
// To specify the length of the delay, see the non_moderator_chat_delay_duration field.
NonModeratorChatDelay *bool `json:"non_moderator_chat_delay"`
// The amount of time, in seconds, that messages are delayed before appearing in chat. Set only if non_moderator_chat_delay is true.
// Possible values are:
//
// 2 — 2 second delay (recommended)
//
// 4 — 4 second delay
//
// 6 — 6 second delay
NonModeratorChatDelayDuration *int `json:"non_moderator_chat_delay_duration"`
// A Boolean value that determines whether the broadcaster limits how often users in the chat room are allowed to send messages.
// Set to true if the broadcaster applies a wait period between messages; otherwise, false.
// The default is false.
//
// To specify the delay, see the slow_mode_wait_time field.
SlowMode bool `json:"slow_mode"`
// The amount of time, in seconds, that users must wait between sending messages.
// Set only if slow_mode is true.
//
// Possible values are: 3 (3 second delay) through 120 (2 minute delay). The default is 30 seconds.
SlowModeWaitTime *int `json:"slow_mode_wait_time"`
// A Boolean value that determines whether only users that subscribe to the broadcasters channel may talk in the chat room.
//
// Set to true if the broadcaster restricts the chat room to subscribers only; otherwise, false.
// The default is false.
SubscriberMode *bool `json:"subscriber_mode"`
// A Boolean value that determines whether the broadcaster requires users to post only unique messages in the chat room.
//
// Set to true if the broadcaster allows only unique messages; otherwise, false.
// The default is false.
UniqueChatMode *bool `json:"unique_chat_mode"`
}
type UpdateChatSettingsResponse struct {
// The list of chat settings. The list contains a single object with all the settings.
Data []Settings `json:"data"`
}
// Updates the broadcasters chat settings.
//
// Requires a user access token that includes the moderator:manage:chat_settings scope.
func (c *Chat) UpdateChatSettings(ctx context.Context, params *UpdateChatSettingsParams, body *UpdateChatSettingsRequest) (*UpdateChatSettingsResponse, error) {
v, _ := query.Values(body)
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.MethodPatch, endpoint.Make(c.baseUrl, "chat/settings", v), r)
2024-02-28 10:30:20 -05:00
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 update chat settings (%d)", res.StatusCode)
}
2024-02-28 10:30:20 -05:00
var data UpdateChatSettingsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}