2024-01-01 16:52:18 +00:00
|
|
|
// Package commercial contains an actor to run commercials in a channel
|
2023-03-14 14:34:08 +00:00
|
|
|
package commercial
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-03 19:00:28 +00:00
|
|
|
"fmt"
|
2023-03-14 14:34:08 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2023-03-14 14:34:08 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
|
2023-03-14 14:34:08 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
actorName = "commercial"
|
|
|
|
|
|
|
|
maxCommercialDuration = 180
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
formatMessage plugins.MsgFormatter
|
|
|
|
permCheckFn plugins.ChannelPermissionCheckFunc
|
|
|
|
tcGetter func(string) (*twitch.Client, error)
|
|
|
|
|
|
|
|
commercialChatcommandRegex = regexp.MustCompile(`^/commercial ([0-9]+)$`)
|
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2023-03-14 14:34:08 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
permCheckFn = args.HasPermissionForChannel
|
|
|
|
tcGetter = args.GetTwitchClientForChannel
|
|
|
|
|
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Start Commercial",
|
|
|
|
Name: "Commercial",
|
|
|
|
Type: actorName,
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Duration of the commercial (must not be longer than 180s and must yield an integer)",
|
|
|
|
Key: "duration",
|
|
|
|
Name: "Duration",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
args.RegisterMessageModFunc("/commercial", handleChatCommand)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type actor struct{}
|
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
|
2023-03-14 14:34:08 +00:00
|
|
|
ptrStringEmpty := func(v string) *string { return &v }("")
|
|
|
|
|
|
|
|
durationStr, err := formatMessage(attrs.MustString("duration", ptrStringEmpty), m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing duration template")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, startCommercial(strings.TrimLeft(plugins.DeriveChannel(m, eventData), "#"), durationStr)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2023-03-14 14:34:08 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
|
|
|
if err = attrs.ValidateSchema(
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "duration", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
2024-04-08 13:56:12 +00:00
|
|
|
fieldcollection.MustHaveNoUnknowFields,
|
2024-04-03 19:00:28 +00:00
|
|
|
helpers.SchemaValidateTemplateField(tplValidator, "duration"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2023-03-14 14:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleChatCommand(m *irc.Message) error {
|
|
|
|
channel := strings.TrimLeft(plugins.DeriveChannel(m, nil), "#")
|
|
|
|
|
|
|
|
matches := commercialChatcommandRegex.FindStringSubmatch(m.Trailing())
|
|
|
|
if matches == nil {
|
|
|
|
return errors.New("ban message does not match required format")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := startCommercial(channel, matches[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugins.ErrSkipSendingMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func startCommercial(channel, durationStr string) error {
|
|
|
|
duration, err := strconv.ParseInt(durationStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parsing duration to integer")
|
|
|
|
}
|
|
|
|
|
|
|
|
if duration > maxCommercialDuration {
|
|
|
|
return errors.New("duration too long")
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := permCheckFn(channel, twitch.ScopeChannelEditCommercial)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "checking for channel permissions")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("channel %q is missing permission %s", channel, twitch.ScopeChannelEditCommercial)
|
|
|
|
}
|
|
|
|
|
|
|
|
tc, err := tcGetter(channel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting channel twitch-client")
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(
|
|
|
|
tc.RunCommercial(context.Background(), channel, duration),
|
|
|
|
"running commercial",
|
|
|
|
)
|
|
|
|
}
|