[templating] Add function chatterHasBadge

and fix `botHasBadge` which previously did what `chatterHasBadge` does
now which was fully incorrect behavior

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-10-26 18:39:20 +02:00
parent ba21a372b5
commit 1d35bd3a4f
Signed by: luzifer
GPG Key ID: D91C3E91E4CAD6F5
7 changed files with 76 additions and 26 deletions

View File

@ -84,7 +84,7 @@ Example:
``` ```
# {{ botHasBadge "moderator" }} # {{ botHasBadge "moderator" }}
< true < false
``` ```
### `channelCounter` ### `channelCounter`
@ -100,6 +100,19 @@ Example:
< #example:test < #example:test
``` ```
### `chatterHasBadge`
Checks whether chatter writing the current line has the given badge in the current channel
Syntax: `chatterHasBadge <badge>`
Example:
```
# {{ chatterHasBadge "moderator" }}
< true
```
### `counterRank` ### `counterRank`
Returns the rank of the given counter and the total number of counters in given counter prefix Returns the rank of the given counter and the total number of counters in given counter prefix
@ -415,7 +428,7 @@ Example:
``` ```
# Your int this hour: {{ printf "%.0f" (mulf (seededRandom (list "int" .username (now | date "2006-01-02 15") | join ":")) 100) }}% # Your int this hour: {{ printf "%.0f" (mulf (seededRandom (list "int" .username (now | date "2006-01-02 15") | join ":")) 100) }}%
< Your int this hour: 43% < Your int this hour: 37%
``` ```
### `streamUptime` ### `streamUptime`

View File

@ -30,16 +30,16 @@ func init() {
}, },
}) })
tplFuncs.Register("botHasBadge", func(m *irc.Message, r *plugins.Rule, fields *plugins.FieldCollection) interface{} { tplFuncs.Register("chatterHasBadge", func(m *irc.Message, r *plugins.Rule, fields *plugins.FieldCollection) interface{} {
return func(badge string) bool { return func(badge string) bool {
badges := twitch.ParseBadgeLevels(m) badges := twitch.ParseBadgeLevels(m)
return badges.Has(badge) return badges.Has(badge)
} }
}, plugins.TemplateFuncDocumentation{ }, plugins.TemplateFuncDocumentation{
Description: "Checks whether bot has the given badge in the current channel", Description: "Checks whether chatter writing the current line has the given badge in the current channel",
Syntax: "botHasBadge <badge>", Syntax: "chatterHasBadge <badge>",
Example: &plugins.TemplateFuncDocumentationExample{ Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ botHasBadge "moderator" }}`, Template: `{{ chatterHasBadge "moderator" }}`,
ExpectedOutput: "true", ExpectedOutput: "true",
}, },
}) })

View File

@ -0,0 +1,51 @@
// Package userstate traces the bot state and provides template
// functions based on it
package userstate
import (
"github.com/Luzifer/twitch-bot/v3/plugins"
"github.com/pkg/errors"
"gopkg.in/irc.v4"
)
var userState = newTwitchUserStateStore()
func Register(args plugins.RegistrationArguments) error {
if err := args.RegisterRawMessageHandler(rawMessageHandler); err != nil {
return errors.Wrap(err, "registering raw message handler")
}
args.RegisterTemplateFunction("botHasBadge", func(m *irc.Message, _ *plugins.Rule, fields *plugins.FieldCollection) interface{} {
return func(badge string) bool {
state := userState.Get(plugins.DeriveChannel(m, fields))
if state == nil {
return false
}
return state.Badges.Has(badge)
}
}, plugins.TemplateFuncDocumentation{
Description: "Checks whether bot has the given badge in the current channel",
Syntax: "botHasBadge <badge>",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ botHasBadge "moderator" }}`,
ExpectedOutput: "false",
},
})
return nil
}
func rawMessageHandler(m *irc.Message) error {
if m.Command != "USERSTATE" {
return nil
}
state, err := parseTwitchUserState(m)
if err != nil {
return errors.Wrap(err, "parsing state")
}
userState.Set(plugins.DeriveChannel(m, nil), state)
return nil
}

View File

@ -1,4 +1,4 @@
package main package userstate
import ( import (
"strings" "strings"

15
irc.go
View File

@ -181,11 +181,6 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
// Announces Twitch-specific events to the channel (for example, a users subscription notification). // Announces Twitch-specific events to the channel (for example, a users subscription notification).
i.handleTwitchUsernotice(m) i.handleTwitchUsernotice(m)
case "USERSTATE":
// USERSTATE (Twitch Tags)
// Sends user-state data when a user joins a channel or sends a PRIVMSG to a channel.
i.handleTwitchUserstate(m)
case "WHISPER": case "WHISPER":
// WHISPER (Twitch Commands) // WHISPER (Twitch Commands)
// Delivers whisper-messages received // Delivers whisper-messages received
@ -486,16 +481,6 @@ func (i ircHandler) handleTwitchUsernotice(m *irc.Message) {
} }
} }
func (i ircHandler) handleTwitchUserstate(m *irc.Message) {
state, err := parseTwitchUserState(m)
if err != nil {
log.WithError(err).Error("Unable to parse bot user-state")
return
}
botUserstate.Set(plugins.DeriveChannel(m, nil), state)
}
func (i ircHandler) handleTwitchWhisper(m *irc.Message) { func (i ircHandler) handleTwitchWhisper(m *irc.Message) {
go handleMessage(i.c, m, eventTypeWhisper, nil) go handleMessage(i.c, m, eventTypeWhisper, nil)
} }

View File

@ -60,7 +60,6 @@ var (
config *configFile config *configFile
configLock = new(sync.RWMutex) configLock = new(sync.RWMutex)
botUserstate = newTwitchUserStateStore()
cronService *cron.Cron cronService *cron.Cron
ircHdl *ircHandler ircHdl *ircHandler
router = mux.NewRouter() router = mux.NewRouter()

View File

@ -49,6 +49,7 @@ import (
"github.com/Luzifer/twitch-bot/v3/internal/template/strings" "github.com/Luzifer/twitch-bot/v3/internal/template/strings"
"github.com/Luzifer/twitch-bot/v3/internal/template/subscriber" "github.com/Luzifer/twitch-bot/v3/internal/template/subscriber"
twitchFns "github.com/Luzifer/twitch-bot/v3/internal/template/twitch" twitchFns "github.com/Luzifer/twitch-bot/v3/internal/template/twitch"
"github.com/Luzifer/twitch-bot/v3/internal/template/userstate"
"github.com/Luzifer/twitch-bot/v3/pkg/database" "github.com/Luzifer/twitch-bot/v3/pkg/database"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch" "github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins" "github.com/Luzifer/twitch-bot/v3/plugins"
@ -95,6 +96,7 @@ var (
strings.Register, strings.Register,
subscriber.Register, subscriber.Register,
twitchFns.Register, twitchFns.Register,
userstate.Register,
// API-only modules // API-only modules
customevent.Register, customevent.Register,