mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 08:40:01 +00:00
Add fallback support to group matches
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
9852fc3a91
commit
0a8bf4db0d
2 changed files with 6 additions and 2 deletions
|
@ -23,12 +23,16 @@ func init() {
|
|||
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{} {
|
||||
return func(idx int) (string, error) {
|
||||
return func(idx int, fallback ...string) (string, error) {
|
||||
fields := r.GetMatchMessage().FindStringSubmatch(m.Trailing())
|
||||
if len(fields) <= idx {
|
||||
return "", errors.New("group not found")
|
||||
}
|
||||
|
||||
if fields[idx] == "" && len(fallback) > 0 {
|
||||
return fallback[0], nil
|
||||
}
|
||||
|
||||
return fields[idx], nil
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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
|
||||
- `fixUsername <username>` - Ensures the username no longer contains the `@` or `#` prefix
|
||||
- `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.
|
||||
- `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
|
||||
|
|
Loading…
Reference in a new issue