From d3a993cda522d1bcbab73f4f077dcf853d8f5430 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 26 Dec 2020 02:01:13 +0100 Subject: [PATCH] Introduce channelCounter function Signed-off-by: Knut Ahlers --- msgformatter.go | 11 ++++++++++- wiki/Home.md | 5 +++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/msgformatter.go b/msgformatter.go index 29d2efe..4fb37de 100644 --- a/msgformatter.go +++ b/msgformatter.go @@ -25,6 +25,15 @@ func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string] return msgParts[arg], nil } + messageFunctions["channelCounter"] = func(name string) (string, error) { + channel, ok := fields["channel"].(string) + if !ok { + return "", errors.New("channel not available") + } + + return strings.Join([]string{channel, name}, ":"), nil + } + messageFunctions["counterValue"] = func(name string, _ ...string) int64 { return store.GetCounterValue(name) } @@ -41,7 +50,7 @@ func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string] } messageFunctions["recentGame"] = func(username string, v ...string) (string, error) { - game, _, err := twitch.getRecentStreamInfo(username) + game, _, err := twitch.getRecentStreamInfo(strings.TrimLeft(username, "#")) if err != nil && len(v) > 0 { return v[0], nil } diff --git a/wiki/Home.md b/wiki/Home.md index 6a1493c..29ca924 100644 --- a/wiki/Home.md +++ b/wiki/Home.md @@ -68,6 +68,7 @@ There are certain variables available in the strings with templating enabled: Additionally there are some functions available in the templates: - `arg ` - Takes the message sent to the channel, splits by space and returns the Nth element +- `channelCounter ` - Wraps the counter name into a channel specific counter name including the channel name - `counterValue ` - Returns the current value of the counter which identifier was supplied - `fixUsername ` - Ensures the username no longer contains the `@` prefix - `group ` - Gets matching group specified by index from `match_message` regular expression @@ -80,8 +81,8 @@ Additionally there are some functions available in the templates: ```yaml - actions: - - counter: '{{ recentGame "mychannel" }}' - - respond: 'I already died {{ counterValue (recentGame "mychannel") }} times in {{ recentGame "mychannel" }}' + - counter: '{{ channelCounter (recentGame .channel) }}' + - respond: 'I already died {{ counterValue (channelCounter (recentGame .channel)) }} times in {{ recentGame .channel }}' cooldown: 60s enable_on: [broadcaster, moderator] match_channels: ['#mychannel']