2023-02-05 15:08:22 +00:00
|
|
|
package stopexec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2023-02-05 15:08:22 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
const actorName = "stopexec"
|
|
|
|
|
|
|
|
var formatMessage plugins.MsgFormatter
|
|
|
|
|
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Stop Rule Execution on Condition",
|
|
|
|
Name: "Stop Execution",
|
|
|
|
Type: actorName,
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Condition when to stop execution (must evaluate to \"true\" to stop execution)",
|
|
|
|
Key: "when",
|
|
|
|
Name: "When",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type actor struct{}
|
|
|
|
|
2023-03-24 21:32:00 +00:00
|
|
|
func (a actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2023-02-05 15:08:22 +00:00
|
|
|
ptrStringEmpty := func(v string) *string { return &v }("")
|
|
|
|
|
|
|
|
when, err := formatMessage(attrs.MustString("when", ptrStringEmpty), m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing when template")
|
|
|
|
}
|
|
|
|
|
|
|
|
if when == "true" {
|
|
|
|
return false, plugins.ErrStopRuleExecution
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
whenTemplate, err := attrs.String("when")
|
|
|
|
if err != nil || whenTemplate == "" {
|
|
|
|
return errors.New("when must be non-empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tplValidator(whenTemplate); err != nil {
|
|
|
|
return errors.Wrap(err, "validating when template")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|