2022-04-17 14:54:12 +00:00
|
|
|
package customevent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
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-01-01 16:52:18 +00:00
|
|
|
func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.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-01-01 16:52:18 +00:00
|
|
|
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) (err error) {
|
2022-04-17 14:54:12 +00:00
|
|
|
if v, err := attrs.String("fields"); err != nil || v == "" {
|
|
|
|
return errors.New("fields is expected to be non-empty string")
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
for _, field := range []string{"fields", "schedule_in"} {
|
|
|
|
if err = tplValidator(attrs.MustString(field, ptrStringEmpty)); err != nil {
|
|
|
|
return errors.Wrapf(err, "validating %s template", field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 14:54:12 +00:00
|
|
|
return nil
|
|
|
|
}
|