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() {
|
2020-12-25 18:31:07 +00:00
|
|
|
registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
|
2020-12-21 00:32:39 +00:00
|
|
|
if r.Counter == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-25 23:31:49 +00:00
|
|
|
counterName, err := formatMessage(*r.Counter, m, ruleDef, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "preparing response")
|
|
|
|
}
|
|
|
|
|
2021-03-06 00:35:20 +00:00
|
|
|
if r.CounterSet != nil {
|
2021-03-06 00:42:40 +00:00
|
|
|
parseValue, err := formatMessage(*r.CounterSet, m, ruleDef, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "execute counter value template")
|
|
|
|
}
|
|
|
|
|
|
|
|
counterValue, err := strconv.ParseInt(parseValue, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parse counter value")
|
|
|
|
}
|
|
|
|
|
2021-03-06 00:35:20 +00:00
|
|
|
return errors.Wrap(
|
2021-03-06 00:42:40 +00:00
|
|
|
store.UpdateCounter(counterName, counterValue, true),
|
2021-03-06 00:35:20 +00:00
|
|
|
"set counter",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
var counterStep int64 = 1
|
|
|
|
if r.CounterStep != nil {
|
|
|
|
counterStep = *r.CounterStep
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(
|
2020-12-25 23:31:49 +00:00
|
|
|
store.UpdateCounter(counterName, counterStep, false),
|
2020-12-21 00:32:39 +00:00
|
|
|
"update counter",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|