mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-08 08:10:08 +00:00
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"text/template"
|
|
"time"
|
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
|
"github.com/go-irc/irc"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// Compile-time assertion
|
|
var _ plugins.MsgFormatter = formatMessage
|
|
|
|
func formatMessage(tplString string, m *irc.Message, r *plugins.Rule, fields plugins.FieldCollection) (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"] = plugins.DeriveUser(m, fields)
|
|
compiledFields["channel"] = plugins.DeriveChannel(m, fields)
|
|
|
|
// 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")
|
|
}
|