[shield] Add shield mode actor

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-02-04 13:59:18 +01:00
parent 58f8cb7704
commit 897b97a833
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
6 changed files with 132 additions and 0 deletions

View file

@ -0,0 +1,67 @@
package shield
import (
"context"
"github.com/go-irc/irc"
"github.com/pkg/errors"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
const actorName = "shield"
var botTwitchClient *twitch.Client
func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient()
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
args.RegisterActorDocumentation(plugins.ActionDocumentation{
Description: "Update shield mode for the given channel",
Name: "Update Shield Mode",
Type: actorName,
Fields: []plugins.ActionDocumentationField{
{
Default: "false",
Description: "Whether the shield-mode should be enabled or disabled",
Key: "enable",
Name: "Enable",
Optional: false,
SupportTemplate: false,
Type: plugins.ActionDocumentationFieldTypeBool,
},
},
})
return nil
}
type actor struct{}
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
ptrBoolFalse := func(v bool) *bool { return &v }(false)
return false, errors.Wrap(
botTwitchClient.UpdateShieldMode(
context.Background(),
plugins.DeriveChannel(m, eventData),
attrs.MustBool("enable", ptrBoolFalse),
),
"configuring shield mode",
)
}
func (a actor) IsAsync() bool { return false }
func (a actor) Name() string { return actorName }
func (a actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) (err error) {
if _, err = attrs.Bool("enable"); err != nil {
return errors.New("enable must be boolean")
}
return nil
}

View file

@ -138,3 +138,38 @@ func (c *Client) UnbanUser(channel, username string) error {
"executing unban request", "executing unban request",
) )
} }
// UpdateShieldMode activates or deactivates the Shield Mode in the given channel
func (c *Client) UpdateShieldMode(ctx context.Context, channel string, enable bool) error {
botID, _, err := c.GetAuthorizedUser()
if err != nil {
return errors.Wrap(err, "getting bot user-id")
}
channelID, err := c.GetIDForUsername(strings.TrimLeft(channel, "#@"))
if err != nil {
return errors.Wrap(err, "getting channel user-id")
}
body := new(bytes.Buffer)
if err = json.NewEncoder(body).Encode(map[string]bool{
"is_active": enable,
}); err != nil {
return errors.Wrap(err, "encoding payload")
}
return errors.Wrap(
c.request(clientRequestOpts{
AuthType: authTypeBearerToken,
Context: ctx,
Method: http.MethodPut,
OKStatus: http.StatusOK,
Body: body,
URL: fmt.Sprintf(
"https://api.twitch.tv/helix/moderation/shield_mode?broadcaster_id=%s&moderator_id=%s",
channelID, botID,
),
}),
"executing update request",
)
}

View file

@ -16,6 +16,7 @@ const (
ScopeModeratorManageBannedUsers = "moderator:manage:banned_users" ScopeModeratorManageBannedUsers = "moderator:manage:banned_users"
ScopeModeratorManageChatMessages = "moderator:manage:chat_messages" ScopeModeratorManageChatMessages = "moderator:manage:chat_messages"
ScopeModeratorManageChatSettings = "moderator:manage:chat_settings" ScopeModeratorManageChatSettings = "moderator:manage:chat_settings"
ScopeModeratorManageShieldMode = "moderator:manage:shield_mode"
ScopeModeratorManageShoutouts = "moderator:manage:shoutouts" ScopeModeratorManageShoutouts = "moderator:manage:shoutouts"
ScopeModeratorReadFollowers = "moderator:read:followers" ScopeModeratorReadFollowers = "moderator:read:followers"
ScopeUserManageChatColor = "user:manage:chat_color" ScopeUserManageChatColor = "user:manage:chat_color"

View file

@ -23,6 +23,7 @@ import (
"github.com/Luzifer/twitch-bot/v3/internal/actors/quotedb" "github.com/Luzifer/twitch-bot/v3/internal/actors/quotedb"
"github.com/Luzifer/twitch-bot/v3/internal/actors/raw" "github.com/Luzifer/twitch-bot/v3/internal/actors/raw"
"github.com/Luzifer/twitch-bot/v3/internal/actors/respond" "github.com/Luzifer/twitch-bot/v3/internal/actors/respond"
"github.com/Luzifer/twitch-bot/v3/internal/actors/shield"
"github.com/Luzifer/twitch-bot/v3/internal/actors/shoutout" "github.com/Luzifer/twitch-bot/v3/internal/actors/shoutout"
"github.com/Luzifer/twitch-bot/v3/internal/actors/timeout" "github.com/Luzifer/twitch-bot/v3/internal/actors/timeout"
"github.com/Luzifer/twitch-bot/v3/internal/actors/variables" "github.com/Luzifer/twitch-bot/v3/internal/actors/variables"
@ -59,6 +60,7 @@ var (
quotedb.Register, quotedb.Register,
raw.Register, raw.Register,
respond.Register, respond.Register,
shield.Register,
shoutout.Register, shoutout.Register,
timeout.Register, timeout.Register,
variables.Register, variables.Register,

View file

@ -20,6 +20,7 @@ var (
twitch.ScopeModeratorManageBannedUsers, twitch.ScopeModeratorManageBannedUsers,
twitch.ScopeModeratorManageChatMessages, twitch.ScopeModeratorManageChatMessages,
twitch.ScopeModeratorManageChatSettings, twitch.ScopeModeratorManageChatSettings,
twitch.ScopeModeratorManageShieldMode,
twitch.ScopeModeratorManageShoutouts, twitch.ScopeModeratorManageShoutouts,
// Chat Scopes // Chat Scopes

View file

@ -344,6 +344,19 @@ Send a whisper (requires a verified bot!)
to: "" to: ""
``` ```
## Shoutout
Perform a Twitch-native shoutout
```yaml
- type: shoutout
attributes:
# User to give the shoutout to
# Optional: false
# Type: string (Supports Templating)
user: ""
```
## Timeout User ## Timeout User
Timeout user from chat Timeout user from chat
@ -360,3 +373,16 @@ Timeout user from chat
# Type: string (Supports Templating) # Type: string (Supports Templating)
reason: "" reason: ""
``` ```
## Update Shield Mode
Update shield mode for the given channel
```yaml
- type: shield
attributes:
# Whether the shield-mode should be enabled or disabled
# Optional: false
# Type: bool
enable: false
```