2021-08-19 13:33:56 +00:00
|
|
|
package delay
|
2021-05-12 16:26:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-10-23 13:06:45 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v2/plugins"
|
2021-05-12 16:26:17 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
const actorName = "delay"
|
|
|
|
|
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{}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
var (
|
|
|
|
ptrZeroDuration = func(v time.Duration) *time.Duration { return &v }(0)
|
|
|
|
delay = attrs.MustDuration("delay", ptrZeroDuration)
|
|
|
|
jitter = attrs.MustDuration("jitter", ptrZeroDuration)
|
|
|
|
)
|
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
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (a actor) IsAsync() bool { return false }
|
2021-09-22 13:36:45 +00:00
|
|
|
func (a actor) Name() string { return actorName }
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
func (a actor) Validate(plugins.TemplateValidatorFunc, *plugins.FieldCollection) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|