2021-08-19 13:33:56 +00:00
|
|
|
package delay
|
2021-05-12 16:26:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-05-12 16:26:17 +00:00
|
|
|
"github.com/go-irc/irc"
|
|
|
|
)
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
args.RegisterActor(func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
return nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-05-12 16:26:17 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
type actor struct {
|
2021-06-11 11:52:42 +00:00
|
|
|
Delay time.Duration `json:"delay" yaml:"delay"`
|
|
|
|
DelayJitter time.Duration `json:"delay_jitter" yaml:"delay_jitter"`
|
|
|
|
}
|
2021-05-12 16:26:17 +00:00
|
|
|
|
2021-09-02 21:26:39 +00:00
|
|
|
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData plugins.FieldCollection) (preventCooldown bool, err error) {
|
2021-06-11 11:52:42 +00:00
|
|
|
if a.Delay == 0 && a.DelayJitter == 0 {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
totalDelay := a.Delay
|
|
|
|
if a.DelayJitter > 0 {
|
|
|
|
totalDelay += time.Duration(rand.Int63n(int64(a.DelayJitter))) // #nosec: G404 // It's just time, no need for crypto/rand
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
|
|
|
func (a actor) Name() string { return "delay" }
|