Add fallback support to group matches

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-09-08 15:09:01 +02:00
parent 9852fc3a91
commit 0a8bf4db0d
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 6 additions and 2 deletions

View file

@ -23,12 +23,16 @@ func init() {
tplFuncs.Register("fixUsername", plugins.GenericTemplateFunctionGetter(func(username string) string { return strings.TrimLeft(username, "@#") })) tplFuncs.Register("fixUsername", plugins.GenericTemplateFunctionGetter(func(username string) string { return strings.TrimLeft(username, "@#") }))
tplFuncs.Register("group", func(m *irc.Message, r *plugins.Rule, fields map[string]interface{}) interface{} { tplFuncs.Register("group", func(m *irc.Message, r *plugins.Rule, fields map[string]interface{}) interface{} {
return func(idx int) (string, error) { return func(idx int, fallback ...string) (string, error) {
fields := r.GetMatchMessage().FindStringSubmatch(m.Trailing()) fields := r.GetMatchMessage().FindStringSubmatch(m.Trailing())
if len(fields) <= idx { if len(fields) <= idx {
return "", errors.New("group not found") return "", errors.New("group not found")
} }
if fields[idx] == "" && len(fallback) > 0 {
return fallback[0], nil
}
return fields[idx], nil return fields[idx], nil
} }
}) })

View file

@ -160,7 +160,7 @@ Additionally there are some functions available in the templates:
- `displayName <username> [fallback]` - Returns the display name the specified user set for themselves - `displayName <username> [fallback]` - Returns the display name the specified user set for themselves
- `fixUsername <username>` - Ensures the username no longer contains the `@` or `#` prefix - `fixUsername <username>` - Ensures the username no longer contains the `@` or `#` prefix
- `followDate <from> <to>` - Looks up when `from` followed `to` - `followDate <from> <to>` - Looks up when `from` followed `to`
- `group <idx>` - Gets matching group specified by index from `match_message` regular expression - `group <idx> [fallback]` - Gets matching group specified by index from `match_message` regular expression, when `fallback` is defined, it is used when group has an empty match
- `recentGame <username> [fallback]` - Returns the last played game name of the specified user (see shoutout example) or the `fallback` if the game could not be fetched. If no fallback was supplied the message will fail and not be sent. - `recentGame <username> [fallback]` - Returns the last played game name of the specified user (see shoutout example) or the `fallback` if the game could not be fetched. If no fallback was supplied the message will fail and not be sent.
- `tag <tagname>` - Takes the message sent to the channel, returns the value of the tag specified - `tag <tagname>` - Takes the message sent to the channel, returns the value of the tag specified
- `toLower <string>` - Converts the given string to lower-case - `toLower <string>` - Converts the given string to lower-case