Add delay-action

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-05-12 18:26:17 +02:00
parent 596f4fad3b
commit ba63d5f8bd
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
3 changed files with 57 additions and 9 deletions

24
action_delay.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"math/rand"
"time"
"github.com/go-irc/irc"
)
func init() {
registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
if r.Delay == 0 && r.DelayJitter == 0 {
return nil
}
totalDelay := r.Delay
if r.DelayJitter > 0 {
totalDelay += time.Duration(rand.Int63n(int64(r.DelayJitter)))
}
time.Sleep(totalDelay)
return nil
})
}

View File

@ -251,12 +251,20 @@ func (r *rule) allowExecuteUserWhitelist(logger *log.Entry, m *irc.Message, even
type ruleAction struct {
Ban *string `json:"ban" yaml:"ban"`
Command []string `json:"command" yaml:"command"`
CounterSet *string `json:"counter_set" yaml:"counter_set"`
CounterStep *int64 `json:"counter_step" yaml:"counter_step"`
Counter *string `json:"counter" yaml:"counter"`
Delay time.Duration `json:"delay" yaml:"delay"`
DelayJitter time.Duration `json:"delay_jitter" yaml:"delay_jitter"`
DeleteMessage *bool `json:"delete_message" yaml:"delete_message"`
Respond *string `json:"respond" yaml:"respond"`
RespondFallback *string `json:"respond_fallback" yaml:"respond_fallback"`
Timeout *time.Duration `json:"timeout" yaml:"timeout"`
}

View File

@ -40,6 +40,10 @@ rules: # See below for examples
# applies templating but MUST result in a parseable integer
counter_step: 1 # Integer, can be negative or positive, default: +1
# Introduce a delay between two actions
- delay: 1m # Duration, how long to wait (fixed)
delay_jitter: 1m # Duration, add random delay to fixed delay between 0 and this value
# Issue a delete on the message caught
- delete_message: true # Bool, set to true to delete
@ -193,6 +197,18 @@ The example was dumped using this action:
match_message: '^!followage'
```
### Respond to a message after random delay
```yaml
- actions:
# Respond after 30-40s
- delay: 30s
delay_jitter: 10s
- respond: 'Hey {{ .username }}'
match_channels: ['#mychannel']
match_message: '^Hi'
```
### Send a notification on successful permit
```yaml