2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
korvike "github.com/Luzifer/korvike/functions"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-12-24 12:19:58 +00:00
|
|
|
var messageFunctions = korvike.GetFunctionMap()
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2020-12-24 12:19:58 +00:00
|
|
|
func formatMessage(tplString string, m *irc.Message, fields map[string]interface{}) (string, error) {
|
2020-12-21 00:32:39 +00:00
|
|
|
tpl, err := template.
|
|
|
|
New(tplString).
|
2020-12-24 12:19:58 +00:00
|
|
|
Funcs(messageFunctions).
|
2020-12-21 00:32:39 +00:00
|
|
|
Parse(tplString)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "parse template")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fields == nil {
|
|
|
|
fields = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
fields["msg"] = m
|
|
|
|
fields["permitTimeout"] = int64(*&config.PermitTimeout / time.Second)
|
|
|
|
fields["username"] = m.User
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = tpl.Execute(buf, fields)
|
|
|
|
|
|
|
|
return buf.String(), errors.Wrap(err, "execute template")
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:19:58 +00:00
|
|
|
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")
|
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2020-12-24 12:19:58 +00:00
|
|
|
return msgParts[params[0]], nil
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 12:19:58 +00:00
|
|
|
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
|
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2020-12-24 12:30:42 +00:00
|
|
|
messageFunctions["recentGame"] = func(username string, v ...string) (string, error) {
|
2020-12-24 12:19:58 +00:00
|
|
|
game, _, err := twitch.getRecentStreamInfo(username)
|
2020-12-24 12:30:42 +00:00
|
|
|
if err != nil && len(v) > 0 {
|
|
|
|
return v[0], nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:19:58 +00:00
|
|
|
return game, err
|
|
|
|
}
|
2020-12-24 13:13:26 +00:00
|
|
|
|
|
|
|
messageFunctions["fixUsername"] = func(username string) string { return strings.TrimLeft(username, "@") }
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|