-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.go
71 lines (57 loc) · 1.9 KB
/
actor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package delay
import (
"math/rand"
"time"
"github.com/Luzifer/twitch-bot/plugins"
"github.com/go-irc/irc"
)
const actorName = "delay"
func Register(args plugins.RegistrationArguments) error {
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,
},
},
})
return nil
}
type actor struct{}
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData plugins.FieldCollection, attrs plugins.FieldCollection) (preventCooldown bool, err error) {
var (
ptrZeroDuration = func(v time.Duration) *time.Duration { return &v }(0)
delay = attrs.MustDuration("delay", ptrZeroDuration)
jitter = attrs.MustDuration("jitter", ptrZeroDuration)
)
if delay == 0 && jitter == 0 {
return false, nil
}
totalDelay := delay
if jitter > 0 {
totalDelay += time.Duration(rand.Int63n(int64(jitter))) // #nosec: G404 // It's just time, no need for crypto/rand
}
time.Sleep(totalDelay)
return false, nil
}
func (a actor) IsAsync() bool { return false }
func (a actor) Name() string { return actorName }
func (a actor) Validate(attrs plugins.FieldCollection) (err error) { return nil }