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"
)
func formatMessage(tplString string, m *irc.Message, fields map[string]interface{}) (string, error) {
fm := korvike.GetFunctionMap()
fm["getArg"] = tplGetMessageArg
fm["getCounterValue"] = tplGetCounterValue
fm["getTag"] = tplGetTagFromMessage
var messageFunctions = korvike.GetFunctionMap()
func formatMessage(tplString string, m *irc.Message, fields map[string]interface{}) (string, error) {
tpl, err := template.
New(tplString).
Funcs(fm).
Funcs(messageFunctions).
Parse(tplString)
if err != nil {
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")
}
func tplGetCounterValue(name string, _ ...string) int64 {
return store.GetCounterValue(name)
}
func tplGetMessageArg(m *irc.Message, params ...int) (string, error) {
func init() {
messageFunctions["getArg"] = func(m *irc.Message, params ...int) (string, error) {
msgParts := strings.Split(m.Trailing(), " ")
if len(msgParts) < params[0]+1 {
return "", errors.New("argument not found")
@ -53,7 +47,17 @@ func tplGetMessageArg(m *irc.Message, params ...int) (string, error) {
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])
return s
}
messageFunctions["recentGame"] = func(username string, _ ...string) (string, error) {
game, _, err := twitch.getRecentStreamInfo(username)
return game, err
}
}