2022-04-17 14:54:12 +00:00
|
|
|
package customevent
|
|
|
|
|
|
|
|
import (
|
2024-04-03 19:00:28 +00:00
|
|
|
"fmt"
|
2022-04-17 14:54:12 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2022-04-17 14:54:12 +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"
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2022-04-17 14:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2022-04-17 14:54:12 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-10-31 14:20:41 +00:00
|
|
|
delayRaw, err := formatMessage(attrs.MustString("schedule_in", ptrStringEmpty), m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing schedule_in template")
|
|
|
|
}
|
|
|
|
|
2022-04-17 14:54:12 +00:00
|
|
|
return false, errors.Wrap(
|
2022-12-26 17:48:46 +00:00
|
|
|
triggerOrStoreEvent(plugins.DeriveChannel(m, eventData), strings.NewReader(fd), delayRaw),
|
2022-04-17 14:54:12 +00:00
|
|
|
"triggering event",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2022-04-17 14:54:12 +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}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "schedule_in", 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", "schedule_in"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2022-10-31 16:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 14:54:12 +00:00
|
|
|
return nil
|
|
|
|
}
|