mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 16:50:01 +00:00
Reorganize template functions, add recentGame
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
e19fe57ec7
commit
b3defa44fd
1 changed files with 22 additions and 18 deletions
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue