Simplify function names

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-12-25 19:39:44 +01:00
parent cad3f982cd
commit 11bd11081d
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D

View File

@ -15,9 +15,8 @@ import (
func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]interface{}) (string, error) { func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]interface{}) (string, error) {
// Create anonymous functions in current context in order to access function variables // Create anonymous functions in current context in order to access function variables
messageFunctions := korvike.GetFunctionMap() messageFunctions := korvike.GetFunctionMap()
messageFunctions["fixUsername"] = func(username string) string { return strings.TrimLeft(username, "@") }
messageFunctions["getArg"] = func(arg int) (string, error) { messageFunctions["arg"] = func(arg int) (string, error) {
msgParts := strings.Split(m.Trailing(), " ") msgParts := strings.Split(m.Trailing(), " ")
if len(msgParts) <= arg { if len(msgParts) <= arg {
return "", errors.New("argument not found") return "", errors.New("argument not found")
@ -26,14 +25,11 @@ func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]
return msgParts[arg], nil return msgParts[arg], nil
} }
messageFunctions["getCounterValue"] = func(name string, _ ...string) int64 { messageFunctions["counterValue"] = func(name string, _ ...string) int64 {
return store.GetCounterValue(name) return store.GetCounterValue(name)
} }
messageFunctions["getTag"] = func(tag string) string { messageFunctions["fixUsername"] = func(username string) string { return strings.TrimLeft(username, "@") }
s, _ := m.GetTag(tag)
return s
}
messageFunctions["group"] = func(idx int) (string, error) { messageFunctions["group"] = func(idx int) (string, error) {
fields := r.matchMessage.FindStringSubmatch(m.Trailing()) fields := r.matchMessage.FindStringSubmatch(m.Trailing())
@ -53,6 +49,11 @@ func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]
return game, err return game, err
} }
messageFunctions["tag"] = func(tag string) string {
s, _ := m.GetTag(tag)
return s
}
// Parse and execute template // Parse and execute template
tpl, err := template. tpl, err := template.
New(tplString). New(tplString).