Repository: nicklaw5/helix
Pull request #89 (2021-05-15 21:37:51)

Add Manage Held AutoMod Messages feature
Twitch changelog : 2021‑05‑12


Patch:
diff --git a/README.md b/README.md
index 8451d54..228b45b 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,7 @@ looking for the Twitch API docs, see the [Twitch Developer website](https://dev.
 - [x] Get Games
 - [ ] Get Hype Train Events
 - [ ] Check AutoMod Status
+- [x] Manage Held AutoMod Messages
 - [ ] Get Banned Events
 - [x] Get Banned Users
 - [ ] Get Moderators
@@ -62,9 +63,9 @@ looking for the Twitch API docs, see the [Twitch Developer website](https://dev.
 - [x] Get Polls         {beta}
 - [x] Create Poll       {beta}
 - [x] End Poll          {beta}
-- [ ] Get Predictions   {beta}
-- [ ] Create Prediction {beta}
-- [ ] End Prediction    {beta}
+- [x] Get Predictions   {beta}
+- [x] Create Prediction {beta}
+- [x] End Prediction    {beta}
 - [ ] Search Categories
 - [ ] Search Channels
 - [ ] Get Stream Key
diff --git a/docs/moderation_docs.md b/docs/moderation_docs.md
index ee27212..d8ea2e7 100644
--- a/docs/moderation_docs.md
+++ b/docs/moderation_docs.md
@@ -25,3 +25,29 @@ if err != nil {
 
 fmt.Printf("%+v\n", resp)
 ```
+
+## Manage Held AutoMod Messages
+
+This is an example of how to manage held automod message in a channel.
+
+```go
+client, err := helix.NewClient(&helix.Options{
+    ClientID: "your-client-id",
+    UserAccessToken: "your-user-access-token",
+})
+if err != nil {
+    // handle error
+}
+
+resp, err := client.ModerateHeldMessage(&helix.HeldMessageModerationParams{
+    UserID : "145328278",
+    MsgID  : "19fe2618-df5f-45d3-a210-aeda6f6c6d9e",
+    Action : "ALLOW",
+})
+if err != nil {
+    // handle error
+}
+
+fmt.Printf("%+v\n", resp)
+```
+
diff --git a/moderation_automod.go b/moderation_automod.go
new file mode 100644
index 0000000..b577c7b
--- /dev/null
+++ b/moderation_automod.go
@@ -0,0 +1,27 @@
+package helix
+
+// HeldMessageModerationResponse ...
+type HeldMessageModerationResponse struct {
+	ResponseCommon
+}
+
+// HeldMessageModerationParams ...
+type HeldMessageModerationParams struct {
+	UserID string `query:"user_id"`
+	MsgID  string `query:"msg_id"`
+	Action string `query:"action"` // Must be "ALLOW" or "DENY".
+}
+
+// ModerateHeldMessage ...
+// Required scope: moderator:manage:automod
+func (c *Client) ModerateHeldMessage(params *HeldMessageModerationParams) (*HeldMessageModerationResponse, error) {
+	resp, err := c.postAsJSON("/moderation/automod/message", nil, params)
+	if err != nil {
+		return nil, err
+	}
+
+	moderation := &HeldMessageModerationResponse{}
+	resp.HydrateResponseCommon(&moderation.ResponseCommon)
+
+	return moderation, nil
+}


Test patch:
diff --git a/moderation_automod_test.go b/moderation_automod_test.go
new file mode 100644
index 0000000..b292c73
--- /dev/null
+++ b/moderation_automod_test.go
@@ -0,0 +1,84 @@
+package helix
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestModerateHeldMessage(t *testing.T) {
+	t.Parallel()
+
+	testCases := []struct {
+		statusCode  int
+		options     *Options
+		Params      *HeldMessageModerationParams
+		respBody    string
+	}{
+		{
+			http.StatusBadRequest,
+			&Options{ClientID: "my-client-id"},
+			&HeldMessageModerationParams{UserID: ""},
+			`{"error":"Bad Request","status":400,"message":"Missing required parameter \"user_id\""}`,
+		},
+		{
+			http.StatusOK,
+			&Options{ClientID: "my-client-id"},
+			&HeldMessageModerationParams{
+				UserID : "145328278",
+				MsgID  : "19fe2618-df5f-45d3-a210-aeda6f6c6d9e",
+				Action : "145328278",
+			},
+			``,
+		},
+	}
+
+	for _, testCase := range testCases {
+		c := newMockClient(testCase.options, newMockHandler(testCase.statusCode, testCase.respBody, nil))
+
+		resp, err := c.ModerateHeldMessage(testCase.Params)
+		if err != nil {
+			t.Error(err)
+		}
+
+		if resp.StatusCode != testCase.statusCode {
+			t.Errorf("expected status code to be %d, got %d", testCase.statusCode, resp.StatusCode)
+		}
+
+		if resp.StatusCode == http.StatusBadRequest {
+			if resp.Error != "Bad Request" {
+				t.Errorf("expected error to be %s, got %s", "Bad Request", resp.Error)
+			}
+
+			if resp.ErrorStatus != http.StatusBadRequest {
+				t.Errorf("expected error status to be %d, got %d", http.StatusBadRequest, resp.ErrorStatus)
+			}
+
+			expectedErrMsg := "Missing required parameter \"user_id\""
+			if resp.ErrorMessage != expectedErrMsg {
+				t.Errorf("expected error message to be %s, got %s", expectedErrMsg, resp.ErrorMessage)
+			}
+
+			continue
+		}
+	}
+
+	// Test with HTTP Failure
+	options := &Options{
+		ClientID: "my-client-id",
+		HTTPClient: &badMockHTTPClient{
+			newMockHandler(0, "", nil),
+		},
+	}
+	c := &Client{
+		opts: options,
+	}
+
+	_, err := c.ModerateHeldMessage(&HeldMessageModerationParams{})
+	if err == nil {
+		t.Error("expected error but got nil")
+	}
+
+	if err.Error() != "Failed to execute API request: Oops, that's bad :(" {
+		t.Error("expected error does match return error")
+	}
+}