-
Notifications
You must be signed in to change notification settings - Fork 89
/
moderation.go
484 lines (388 loc) · 13.9 KB
/
moderation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package helix
import "errors"
// ExpiresAt must be parsed manually since an empty string means perma ban
type Ban struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
ExpiresAt Time `json:"expires_at"`
}
type ManyBans struct {
Bans []Ban `json:"data"`
Pagination Pagination `json:"pagination"`
}
type BannedUsersResponse struct {
ResponseCommon
Data ManyBans
}
// BroadcasterID must match the auth tokens user_id
type BannedUsersParams struct {
BroadcasterID string `query:"broadcaster_id"`
// Filter by provided UserIDs
UserID []string `query:"user_id"`
After string `query:"after"`
Before string `query:"before"`
}
// GetBannedUsers returns all banned and timed-out users in a channel.
//
// Required scope: moderation:read
func (c *Client) GetBannedUsers(params *BannedUsersParams) (*BannedUsersResponse, error) {
resp, err := c.get("/moderation/banned", &ManyBans{}, params)
if err != nil {
return nil, err
}
bans := &BannedUsersResponse{}
resp.HydrateResponseCommon(&bans.ResponseCommon)
bans.Data.Bans = resp.Data.(*ManyBans).Bans
bans.Data.Pagination = resp.Data.(*ManyBans).Pagination
return bans, nil
}
type BanUserParams struct {
BroadcasterID string `query:"broadcaster_id"`
ModeratorId string `query:"moderator_id"`
Body BanUserRequestBody `json:"data"`
}
type BanUserRequestBody struct {
Duration int `json:"duration,omitempty"` // optional
Reason string `json:"reason"` // required
UserId string `json:"user_id"` // required
}
type BanUserResponse struct {
ResponseCommon
Data ManyBanUser
}
type ManyBanUser struct {
Bans []BanUser `json:"data"`
}
type BanUser struct {
BoardcasterId string `json:"broadcaster_id"`
CreatedAt Time `json:"created_at"`
EndTime Time `json:"end_time"`
ModeratorId string `json:"moderator_id"`
UserId string `json:"user_id"`
}
// BanUser Bans a user from participating in a broadcaster’s chat room, or puts them in a timeout.
// Required scope: moderator:manage:banned_users
func (c *Client) BanUser(params *BanUserParams) (*BanUserResponse, error) {
resp, err := c.postAsJSON("/moderation/bans", &ManyBanUser{}, params)
if err != nil {
return nil, err
}
banResp := &BanUserResponse{}
resp.HydrateResponseCommon(&banResp.ResponseCommon)
banResp.Data.Bans = resp.Data.(*ManyBanUser).Bans
return banResp, nil
}
type UnbanUserParams struct {
BroadcasterID string `query:"broadcaster_id"`
ModeratorID string `query:"moderator_id"`
UserID string `query:"user_id"`
}
type UnbanUserResponse struct {
ResponseCommon
}
// UnbanUser Removes the ban or timeout that was placed on the specified user
// Required scope: moderator:manage:banned_users
func (c *Client) UnbanUser(params *UnbanUserParams) (*UnbanUserResponse, error) {
resp, err := c.delete("/moderation/bans", nil, params)
if err != nil {
return nil, err
}
unbanResp := &UnbanUserResponse{}
resp.HydrateResponseCommon(&unbanResp.ResponseCommon)
return unbanResp, nil
}
type BlockedTermsParams struct {
// Required
BroadcasterID string `query:"broadcaster_id"`
ModeratorID string `query:"moderator_id"`
// Optional
After string `query:"after"`
First int `query:"first"`
}
type BlockedTermsResponse struct {
ResponseCommon
Data ManyBlockedTerms
}
type ManyBlockedTerms struct {
Terms []BlockedTerm `json:"data"`
Pagination Pagination `json:"pagination"`
}
type BlockedTerm struct {
BroadcasterID string `json:"broadcaster_id"`
CreatedAt Time `json:"created_at"`
ExpiresAt Time `json:"expires_at"`
ID string `json:"id"`
ModeratorID string `json:"moderator_id"`
Text string `json:"text"`
UpdatedAt Time `json:"updated_at"`
}
// GetBlockedTerms Gets the broadcaster’s list of non-private, blocked words or phrases.
// These are the terms that the broadcaster or moderator added manually, or that were denied by AutoMod.
// Required scope: moderator:read:blocked_terms
func (c *Client) GetBlockedTerms(params *BlockedTermsParams) (*BlockedTermsResponse, error) {
if params.BroadcasterID == "" || params.ModeratorID == "" {
return nil, errors.New("broadcaster id and moderator id must be provided")
}
resp, err := c.get("/moderation/blocked_terms", &ManyBlockedTerms{}, params)
if err != nil {
return nil, err
}
blockedTermsResp := &BlockedTermsResponse{}
resp.HydrateResponseCommon(&blockedTermsResp.ResponseCommon)
blockedTermsResp.Data.Terms = resp.Data.(*ManyBlockedTerms).Terms
blockedTermsResp.Data.Pagination = resp.Data.(*ManyBlockedTerms).Pagination
return blockedTermsResp, nil
}
type AddBlockedTermParams struct {
BroadcasterID string `query:"broadcaster_id"`
ModeratorID string `query:"moderator_id"`
Text string `json:"text"`
}
type AddBlockedTermResponse struct {
ResponseCommon
Data ManyAddBlockedTerms
}
type ManyAddBlockedTerms struct {
Terms []BlockedTerm `json:"data"`
}
// AddBlockedTerm Adds a word or phrase to the broadcaster’s list of blocked terms.
// These are the terms that broadcasters don’t want used in their chat room.
// Required scope: moderator:manage:blocked_terms
func (c *Client) AddBlockedTerm(params *AddBlockedTermParams) (*AddBlockedTermResponse, error) {
if params.BroadcasterID == "" || params.ModeratorID == "" {
return nil, errors.New("broadcaster id and moderator id must be provided")
}
if len(params.Text) < 2 || len(params.Text) > 500 {
return nil, errors.New("the term len must be between 2 and 500")
}
resp, err := c.postAsJSON("/moderation/blocked_terms", &ManyAddBlockedTerms{}, params)
if err != nil {
return nil, err
}
addTermResp := &AddBlockedTermResponse{}
resp.HydrateResponseCommon(&addTermResp.ResponseCommon)
addTermResp.Data.Terms = resp.Data.(*ManyAddBlockedTerms).Terms
return addTermResp, nil
}
type RemoveBlockedTermParams struct {
BroadcasterID string `json:"broadcaster_id"`
ModeratorID string `json:"moderator_id"`
ID string `json:"id"`
}
type RemoveBlockedTermResponse struct {
ResponseCommon
}
// RemoveBlockedTerm Removes the word or phrase that the broadcaster is blocking users from using in their chat room.
// Required scope: moderator:manage:blocked_terms
func (c *Client) RemoveBlockedTerm(params *RemoveBlockedTermParams) (*RemoveBlockedTermResponse, error) {
if params.BroadcasterID == "" || params.ModeratorID == "" {
return nil, errors.New("broadcaster id and moderator id must be provided")
}
if params.ID == "" {
return nil, errors.New("id must be provided")
}
resp, err := c.delete("/moderation/blocked_terms", nil, params)
if err != nil {
return nil, err
}
blockedTermResp := &RemoveBlockedTermResponse{}
resp.HydrateResponseCommon(&blockedTermResp.ResponseCommon)
return blockedTermResp, nil
}
type DeleteChatMessageParams struct {
BroadcasterID string `query:"broadcaster_id"`
ModeratorID string `query:"moderator_id"`
MessageID string `query:"message_id"`
}
type DeleteChatMessageResponse struct {
ResponseCommon
}
// DeleteChatMessage Removes a single chat message from the broadcaster’s chat room.
// Required scope: moderator:manage:chat_messages
func (c *Client) DeleteChatMessage(params *DeleteChatMessageParams) (*DeleteChatMessageResponse, error) {
if params.BroadcasterID == "" || params.ModeratorID == "" {
return nil, errors.New("broadcaster id and moderator id must be provided")
}
if params.MessageID == "" {
return nil, errors.New("message id must be provided")
}
resp, err := c.delete("/moderation/chat", nil, params)
if err != nil {
return nil, err
}
deletedMessageResp := &DeleteChatMessageResponse{}
resp.HydrateResponseCommon(&deletedMessageResp.ResponseCommon)
return deletedMessageResp, nil
}
type DeleteAllChatMessagesParams struct {
BroadcasterID string `query:"broadcaster_id"`
ModeratorID string `query:"moderator_id"`
}
type DeleteAllChatMessagesResponse struct {
ResponseCommon
}
// DeleteAllChatMessages Removes all chat messages from the broadcaster’s chat room.
// Required scope: moderator:manage:chat_messages
func (c *Client) DeleteAllChatMessages(params *DeleteAllChatMessagesParams) (*DeleteAllChatMessagesResponse, error) {
if params.BroadcasterID == "" || params.ModeratorID == "" {
return nil, errors.New("broadcaster id and moderator id must be provided")
}
resp, err := c.delete("/moderation/chat", nil, params)
if err != nil {
return nil, err
}
deletedMessagesResp := &DeleteAllChatMessagesResponse{}
resp.HydrateResponseCommon(&deletedMessagesResp.ResponseCommon)
return deletedMessagesResp, nil
}
type GetModeratorsParams struct {
// Required
BroadcasterID string `query:"broadcaster_id"`
// Optional
UserIDs []string `query:"user_id"` // Limit 100
After string `query:"after"`
First int `query:"first"`
}
type Moderator struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
}
type ManyModerators struct {
Moderators []Moderator `json:"data"`
Pagination Pagination `json:"pagination"`
}
type ModeratorsResponse struct {
ResponseCommon
Data ManyModerators
}
type AddChannelModeratorParams struct {
UserID string `query:"user_id"`
BroadcasterID string `query:"broadcaster_id"`
}
type AddChannelModeratorResponse struct {
ResponseCommon
}
type RemoveChannelModeratorParams struct {
UserID string `query:"user_id"`
BroadcasterID string `query:"broadcaster_id"`
}
type RemoveChannelModeratorResponse struct {
ResponseCommon
}
// GetModerators Gets all users allowed to moderate the broadcaster’s chat room.
// Required scope: moderation:read
func (c *Client) GetModerators(params *GetModeratorsParams) (*ModeratorsResponse, error) {
if params.BroadcasterID == "" {
return nil, errors.New("broadcaster id must be provided")
}
resp, err := c.get("/moderation/moderators", &ManyModerators{}, params)
if err != nil {
return nil, err
}
moderators := &ModeratorsResponse{}
resp.HydrateResponseCommon(&moderators.ResponseCommon)
moderators.Data.Moderators = resp.Data.(*ManyModerators).Moderators
moderators.Data.Pagination = resp.Data.(*ManyModerators).Pagination
return moderators, nil
}
func (c *Client) AddChannelModerator(params *AddChannelModeratorParams) (*AddChannelModeratorResponse, error) {
resp, err := c.post("/moderation/moderators", nil, params)
if err != nil {
return nil, err
}
moderators := &AddChannelModeratorResponse{}
resp.HydrateResponseCommon(&moderators.ResponseCommon)
return moderators, nil
}
func (c *Client) RemoveChannelModerator(params *RemoveChannelModeratorParams) (*RemoveChannelModeratorResponse, error) {
resp, err := c.delete("/moderation/moderators", nil, params)
if err != nil {
return nil, err
}
moderators := &RemoveChannelModeratorResponse{}
resp.HydrateResponseCommon(&moderators.ResponseCommon)
return moderators, nil
}
// `UserID` must match the user ID in the User-Access token
type GetModeratedChannelsParams struct {
// Required
UserID string `query:"user_id"`
// Optional
After string `query:"after"`
First int `query:"first"`
}
type ModeratedChannel struct {
BroadcasterID string `json:"broadcaster_id"`
BroadcasterLogin string `json:"broadcaster_login"`
BroadcasterName string `json:"broadcaster_name"`
}
type ManyModeratedChannels struct {
ModeratedChannels []ModeratedChannel `json:"data"`
Pagination Pagination `json:"pagination"`
}
type GetModeratedChannelsResponse struct {
ResponseCommon
Data ManyModeratedChannels
}
// GetModeratedChannels Gets a list of channels that the specified user has moderator privileges in.
// Required scope: user:read:moderated_channels
func (c *Client) GetModeratedChannels(params *GetModeratedChannelsParams) (*GetModeratedChannelsResponse, error) {
if params.UserID == "" {
return nil, errors.New("user id is required")
}
resp, err := c.get("/moderation/channels", &ManyModeratedChannels{}, params)
if err != nil {
return nil, err
}
moderatedChannels := &GetModeratedChannelsResponse{}
resp.HydrateResponseCommon(&moderatedChannels.ResponseCommon)
moderatedChannels.Data.ModeratedChannels = resp.Data.(*ManyModeratedChannels).ModeratedChannels
moderatedChannels.Data.Pagination = resp.Data.(*ManyModeratedChannels).Pagination
return moderatedChannels, nil
}
type SendModeratorWarnChatMessageParams struct {
// The ID of the broadcaster whose chat room the message will be sent to
BroadcasterID string `query:"broadcaster_id"`
// The ID of the twitch user who requested the warning.
ModeratorID string `query:"moderator_id"`
// The ID of the user sent the WARN message
UserID string `json:"user_id"`
// The warn message to send.
Reason string `json:"reason"`
}
type ModeratorWarnChatMessage struct {
BroadcasterID string `json:"broadcaster_id"`
ModeratorID string `json:"moderator_id"`
UserID string `json:"user_id"`
Reason string `json:"reason"`
}
type ManyModeratorWarnChatMessages struct {
Warnings []ModeratorWarnChatMessage `json:"data"`
}
type SendModeratorWarnChatResponse struct {
ResponseCommon
Data ManyModeratorWarnChatMessages
}
// SendModeratorWarnMessage Sends a warning message to a user in the broadcaster’s chat.
// Required moderator:manage:warnings
func (c *Client) SendModeratorWarnMessage(params *SendModeratorWarnChatMessageParams) (*SendModeratorWarnChatResponse, error) {
if params.BroadcasterID == "" {
return nil, errors.New("error: broadcaster id must be specified")
}
if params.ModeratorID == "" {
return nil, errors.New("error: moderator id must be specified")
}
if params.UserID == "" {
return nil, errors.New("error: user id must be specified")
}
resp, err := c.postAsJSON("moderation/warnings", &ManyModeratorWarnChatMessages{}, params)
if err != nil {
return nil, err
}
messageResponse := &SendModeratorWarnChatResponse{}
resp.HydrateResponseCommon(&messageResponse.ResponseCommon)
messageResponse.Data.Warnings = resp.Data.(*ManyModeratorWarnChatMessages).Warnings
return messageResponse, nil
}