Reorganize template functions, add recentGame

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-12-24 13:19:58 +01:00
parent e19fe57ec7
commit b3defa44fd
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -12,15 +12,12 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func formatMessage(tplString string, m *irc.Message, fields map[string]interface{}) (string, error) { var messageFunctions = korvike.GetFunctionMap()
fm := korvike.GetFunctionMap()
fm["getArg"] = tplGetMessageArg
fm["getCounterValue"] = tplGetCounterValue
fm["getTag"] = tplGetTagFromMessage
func formatMessage(tplString string, m *irc.Message, fields map[string]interface{}) (string, error) {
tpl, err := template. tpl, err := template.
New(tplString). New(tplString).
Funcs(fm). Funcs(messageFunctions).
Parse(tplString) Parse(tplString)
if err != nil { if err != nil {
return "", errors.Wrap(err, "parse template") return "", errors.Wrap(err, "parse template")
@ -40,11 +37,8 @@ func formatMessage(tplString string, m *irc.Message, fields map[string]interface
return buf.String(), errors.Wrap(err, "execute template") return buf.String(), errors.Wrap(err, "execute template")
} }
func tplGetCounterValue(name string, _ ...string) int64 { func init() {
return store.GetCounterValue(name) messageFunctions["getArg"] = func(m *irc.Message, params ...int) (string, error) {
}
func tplGetMessageArg(m *irc.Message, params ...int) (string, error) {
msgParts := strings.Split(m.Trailing(), " ") msgParts := strings.Split(m.Trailing(), " ")
if len(msgParts) < params[0]+1 { if len(msgParts) < params[0]+1 {
return "", errors.New("argument not found") return "", errors.New("argument not found")
@ -53,7 +47,17 @@ func tplGetMessageArg(m *irc.Message, params ...int) (string, error) {
return msgParts[params[0]], nil return msgParts[params[0]], nil
} }
func tplGetTagFromMessage(m *irc.Message, params ...string) string { messageFunctions["getCounterValue"] = func(name string, _ ...string) int64 {
return store.GetCounterValue(name)
}
messageFunctions["getTag"] = func(m *irc.Message, params ...string) string {
s, _ := m.GetTag(params[0]) s, _ := m.GetTag(params[0])
return s return s
} }
messageFunctions["recentGame"] = func(username string, _ ...string) (string, error) {
game, _, err := twitch.getRecentStreamInfo(username)
return game, err
}
}