twitch-bot/action_counter.go
Knut Ahlers cc8160ec67
Fix: Allow templating to read user input
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-03-06 01:42:40 +01:00

48 lines
1,004 B
Go

package main
import (
"strconv"
"github.com/go-irc/irc"
"github.com/pkg/errors"
)
func init() {
registerAction(func(c *irc.Client, m *irc.Message, ruleDef *rule, r *ruleAction) error {
if r.Counter == nil {
return nil
}
counterName, err := formatMessage(*r.Counter, m, ruleDef, nil)
if err != nil {
return errors.Wrap(err, "preparing response")
}
if r.CounterSet != nil {
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")
}
return errors.Wrap(
store.UpdateCounter(counterName, counterValue, true),
"set counter",
)
}
var counterStep int64 = 1
if r.CounterStep != nil {
counterStep = *r.CounterStep
}
return errors.Wrap(
store.UpdateCounter(counterName, counterStep, false),
"update counter",
)
})
}