2024-01-01 16:52:18 +00:00
|
|
|
// Package eventmod contains an actor to modify event data during rule
|
|
|
|
// execution by adding fields (template variables)
|
2023-02-20 18:41:03 +00:00
|
|
|
package eventmod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-04-03 19:00:28 +00:00
|
|
|
"fmt"
|
2023-02-20 18:41:03 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2023-02-20 18:41:03 +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-02-20 18:41:03 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
const actorName = "eventmod"
|
|
|
|
|
|
|
|
var formatMessage plugins.MsgFormatter
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2023-02-20 18:41:03 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Add custom fields to the event to be used as template variables later on",
|
|
|
|
Name: "Add Fields to Event",
|
|
|
|
Type: actorName,
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Fields to set in the event (must produce valid JSON: `map[string]any`)",
|
|
|
|
Key: "fields",
|
|
|
|
Name: "Fields",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
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-02-20 18:41:03 +00:00
|
|
|
ptrStringEmpty := func(v string) *string { return &v }("")
|
|
|
|
|
|
|
|
fd, err := formatMessage(attrs.MustString("fields", ptrStringEmpty), m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing fields template")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fd == "" {
|
|
|
|
return false, errors.New("fields template evaluated to empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := map[string]any{}
|
|
|
|
if err = json.Unmarshal([]byte(fd), &fields); err != nil {
|
|
|
|
return false, errors.Wrap(err, "parsing fields")
|
|
|
|
}
|
|
|
|
|
|
|
|
eventData.SetFromData(fields)
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2023-02-20 18:41:03 +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: "fields", 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, "fields"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2023-02-20 18:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|