Repository: nicklaw5/helix
Pull request #237 (2025-03-23 19:13:38)

fix: user warning incorrect url and body


Patch:
diff --git a/moderation.go b/moderation.go
index d2c61f7..60e8165 100644
--- a/moderation.go
+++ b/moderation.go
@@ -427,6 +427,11 @@ func (c *Client) GetModeratedChannels(params *GetModeratedChannelsParams) (*GetM
 	return moderatedChannels, nil
 }
 
+type SendModeratorWarnMessageRequestBody struct {
+	UserID string `json:"user_id"`
+	Reason string `json:"reason"`
+}
+
 type SendModeratorWarnChatMessageParams struct {
 	// The ID of the broadcaster whose chat room the message will be sent to
 	BroadcasterID string `query:"broadcaster_id"`
@@ -434,11 +439,8 @@ type SendModeratorWarnChatMessageParams struct {
 	// 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"`
+	// A list that contains information about the warning.
+	Body SendModeratorWarnMessageRequestBody `json:"data"`
 }
 
 type ModeratorWarnChatMessage struct {
@@ -467,11 +469,17 @@ func (c *Client) SendModeratorWarnMessage(params *SendModeratorWarnChatMessagePa
 	if params.ModeratorID == "" {
 		return nil, errors.New("error: moderator id must be specified")
 	}
-	if params.UserID == "" {
+	if params.Body.UserID == "" {
 		return nil, errors.New("error: user id must be specified")
 	}
+	if params.Body.Reason == "" {
+		return nil, errors.New("error: reason must be specified")
+	}
+	if len(params.Body.Reason) > 500 {
+		return nil, errors.New("error: reason must be less than 500 characters")
+	}
 
-	resp, err := c.postAsJSON("moderation/warnings", &ManyModeratorWarnChatMessages{}, params)
+	resp, err := c.postAsJSON("/moderation/warnings", &ManyModeratorWarnChatMessages{}, params)
 	if err != nil {
 		return nil, err
 	}


Test patch:
diff --git a/moderation_test.go b/moderation_test.go
index 7241dc8..c702be8 100644
--- a/moderation_test.go
+++ b/moderation_test.go
@@ -1134,8 +1134,10 @@ func TestSendModeratorWarnMessage(t *testing.T) {
 			&SendModeratorWarnChatMessageParams{
 				BroadcasterID: "1234",
 				ModeratorID:   "5678",
-				UserID:        "9876",
-				Reason:        "Test warning message",
+				Body: SendModeratorWarnMessageRequestBody{
+					UserID: "9876",
+					Reason: "Test warning message",
+				},
 			},
 			`{"data": [{"broadcaster_id": "1234", "moderator_id": "5678", "user_id": "9876", "reason": "Test warning message"}]}`,
 			"",
@@ -1146,7 +1148,9 @@ func TestSendModeratorWarnMessage(t *testing.T) {
 			&SendModeratorWarnChatMessageParams{
 				BroadcasterID: "1234",
 				ModeratorID:   "5678",
-				Reason:        "Test warning message",
+				Body: SendModeratorWarnMessageRequestBody{
+					Reason: "Test warning message",
+				},
 			},
 			"",
 			"error: user id must be specified",
@@ -1155,9 +1159,11 @@ func TestSendModeratorWarnMessage(t *testing.T) {
 			http.StatusOK,
 			&Options{ClientID: "my-client-id", UserAccessToken: "invalid-access-token"},
 			&SendModeratorWarnChatMessageParams{
-				UserID:      "1234",
 				ModeratorID: "5678",
-				Reason:      "Test warning message",
+				Body: SendModeratorWarnMessageRequestBody{
+					UserID: "1234",
+					Reason: "Test warning message",
+				},
 			},
 			"",
 			"error: broadcaster id must be specified",
@@ -1166,21 +1172,52 @@ func TestSendModeratorWarnMessage(t *testing.T) {
 			http.StatusOK,
 			&Options{ClientID: "my-client-id", UserAccessToken: "invalid-access-token"},
 			&SendModeratorWarnChatMessageParams{
-				UserID:        "1234",
 				BroadcasterID: "12345",
-				Reason:        "Test warning message",
+				Body: SendModeratorWarnMessageRequestBody{
+					UserID: "1234",
+					Reason: "Test warning message",
+				},
 			},
 			"",
 			"error: moderator id must be specified",
 		},
+		{
+			http.StatusOK,
+			&Options{ClientID: "my-client-id", UserAccessToken: "invalid-access-token"},
+			&SendModeratorWarnChatMessageParams{
+				BroadcasterID: "12345",
+				ModeratorID:   "5678",
+				Body: SendModeratorWarnMessageRequestBody{
+					UserID: "1234",
+				},
+			},
+			"",
+			"error: reason must be specified",
+		},
+		{
+			http.StatusOK,
+			&Options{ClientID: "my-client-id", UserAccessToken: "invalid-access-token"},
+			&SendModeratorWarnChatMessageParams{
+				BroadcasterID: "12345",
+				ModeratorID:   "5678",
+				Body: SendModeratorWarnMessageRequestBody{
+					UserID: "1234",
+					Reason: "Test warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning messageTest warning message",
+				},
+			},
+			"",
+			"error: reason must be less than 500 characters",
+		},
 		{
 			http.StatusUnauthorized,
 			&Options{ClientID: "my-client-id", UserAccessToken: "invalid-access-token"},
 			&SendModeratorWarnChatMessageParams{
 				BroadcasterID: "1234",
 				ModeratorID:   "5678",
-				UserID:        "9876",
-				Reason:        "Test warning message",
+				Body: SendModeratorWarnMessageRequestBody{
+					UserID: "9876",
+					Reason: "Test warning message",
+				},
 			},
 			`{"error":"Unauthorized","status":401,"message":"Invalid OAuth token"}`,
 			"",
@@ -1220,12 +1257,12 @@ func TestSendModeratorWarnMessage(t *testing.T) {
 			t.Errorf("expected moderator id to be %s, got %s", testCase.params.ModeratorID, warning.ModeratorID)
 		}
 
-		if warning.UserID != testCase.params.UserID {
-			t.Errorf("expected user id to be %s, got %s", testCase.params.UserID, warning.UserID)
+		if warning.UserID != testCase.params.Body.UserID {
+			t.Errorf("expected user id to be %s, got %s", testCase.params.Body.UserID, warning.UserID)
 		}
 
-		if warning.Reason != testCase.params.Reason {
-			t.Errorf("expected reason to be %s, got %s", testCase.params.Reason, warning.Reason)
+		if warning.Reason != testCase.params.Body.Reason {
+			t.Errorf("expected reason to be %s, got %s", testCase.params.Body.Reason, warning.Reason)
 		}
 	}
 
@@ -1244,8 +1281,10 @@ func TestSendModeratorWarnMessage(t *testing.T) {
 	_, err := c.SendModeratorWarnMessage(&SendModeratorWarnChatMessageParams{
 		BroadcasterID: "1234",
 		ModeratorID:   "5678",
-		UserID:        "9876",
-		Reason:        "Test warning message",
+		Body: SendModeratorWarnMessageRequestBody{
+			UserID: "9876",
+			Reason: "Test warning message",
+		},
 	})
 	if err == nil {
 		t.Error("expected error but got nil")