twitch-bot/msgformatter.go
Knut Ahlers b6d59045d0
Add global variables to be used in templates
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-05-25 16:26:03 +02:00

50 lines
1 KiB
Go

package main
import (
"bytes"
"text/template"
"time"
"github.com/go-irc/irc"
"github.com/pkg/errors"
)
func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]interface{}) (string, error) {
compiledFields := map[string]interface{}{}
if config != nil {
configLock.RLock()
for k, v := range config.Variables {
compiledFields[k] = v
}
compiledFields["permitTimeout"] = int64(config.PermitTimeout / time.Second)
configLock.RUnlock()
}
for k, v := range fields {
compiledFields[k] = v
}
if m != nil {
compiledFields["msg"] = m
compiledFields["username"] = m.User
if m.Command == "PRIVMSG" && len(m.Params) > 0 {
compiledFields["channel"] = m.Params[0]
}
}
// Parse and execute template
tpl, err := template.
New(tplString).
Funcs(tplFuncs.GetFuncMap(m, r, compiledFields)).
Parse(tplString)
if err != nil {
return "", errors.Wrap(err, "parse template")
}
buf := new(bytes.Buffer)
err = tpl.Execute(buf, compiledFields)
return buf.String(), errors.Wrap(err, "execute template")
}