2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-06 00:42:40 +00:00
|
|
|
"strconv"
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-06-11 11:52:42 +00:00
|
|
|
registerAction(func() Actor { return &ActorCounter{} })
|
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
type ActorCounter struct {
|
|
|
|
CounterSet *string `json:"counter_set" yaml:"counter_set"`
|
|
|
|
CounterStep *int64 `json:"counter_step" yaml:"counter_step"`
|
|
|
|
Counter *string `json:"counter" yaml:"counter"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a ActorCounter) Execute(c *irc.Client, m *irc.Message, r *Rule) error {
|
|
|
|
if a.Counter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-25 23:31:49 +00:00
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
counterName, err := formatMessage(*a.Counter, m, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "preparing response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.CounterSet != nil {
|
|
|
|
parseValue, err := formatMessage(*a.CounterSet, m, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "execute counter value template")
|
2021-03-06 00:35:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
counterValue, err := strconv.ParseInt(parseValue, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parse counter value")
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(
|
2021-06-11 11:52:42 +00:00
|
|
|
store.UpdateCounter(counterName, counterValue, true),
|
|
|
|
"set counter",
|
2020-12-21 00:32:39 +00:00
|
|
|
)
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var counterStep int64 = 1
|
|
|
|
if a.CounterStep != nil {
|
|
|
|
counterStep = *a.CounterStep
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(
|
|
|
|
store.UpdateCounter(counterName, counterStep, false),
|
|
|
|
"update counter",
|
|
|
|
)
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
|
|
|
func (a ActorCounter) IsAsync() bool { return false }
|
|
|
|
func (a ActorCounter) Name() string { return "counter" }
|