2024-01-01 16:52:18 +00:00
|
|
|
// Package delay contains an actor to delay rule execution
|
2021-08-19 13:33:56 +00:00
|
|
|
package delay
|
2021-05-12 16:26:17 +00:00
|
|
|
|
|
|
|
import (
|
2024-04-08 13:56:12 +00:00
|
|
|
"fmt"
|
2021-05-12 16:26:17 +00:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2021-11-25 22:48:16 +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"
|
2021-05-12 16:26:17 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
const actorName = "delay"
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2021-08-19 13:33:56 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Delay next action",
|
|
|
|
Name: "Delay",
|
|
|
|
Type: "delay",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Static delay to wait",
|
|
|
|
Key: "delay",
|
|
|
|
Name: "Delay",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeDuration,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Dynamic jitter to add to the static delay (the added extra delay will be between 0 and this value)",
|
|
|
|
Key: "jitter",
|
|
|
|
Name: "Jitter",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeDuration,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-08-19 13:33:56 +00:00
|
|
|
|
|
|
|
return nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-05-12 16:26:17 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type actor struct{}
|
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Execute(_ *irc.Client, _ *irc.Message, _ *plugins.Rule, _ *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
var (
|
2024-04-03 19:00:28 +00:00
|
|
|
delay = attrs.MustDuration("delay", helpers.Ptr(time.Duration(0)))
|
|
|
|
jitter = attrs.MustDuration("jitter", helpers.Ptr(time.Duration(0)))
|
2021-09-22 13:36:45 +00:00
|
|
|
)
|
2021-05-12 16:26:17 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if delay == 0 && jitter == 0 {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
totalDelay := delay
|
|
|
|
if jitter > 0 {
|
|
|
|
totalDelay += time.Duration(rand.Int63n(int64(jitter))) // #nosec: G404 // It's just time, no need for crypto/rand
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(totalDelay)
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-05-12 16:26:17 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2024-04-08 13:56:12 +00:00
|
|
|
func (actor) Validate(_ plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
|
|
|
if err = attrs.ValidateSchema(
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "delay", Type: fieldcollection.SchemaFieldTypeDuration}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "jitter", Type: fieldcollection.SchemaFieldTypeDuration}),
|
|
|
|
fieldcollection.MustHaveNoUnknowFields,
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
|
|
|
}
|
2022-10-31 16:26:53 +00:00
|
|
|
return nil
|
|
|
|
}
|