[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" }}
< true
< false
```
### `channelCounter`
@ -100,6 +100,19 @@ Example:
< #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`
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: 43%
< Your int this hour: 37%
```
### `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 {
badges := twitch.ParseBadgeLevels(m)
return badges.Has(badge)
}
}, plugins.TemplateFuncDocumentation{
Description: "Checks whether bot has the given badge in the current channel",
Syntax: "botHasBadge <badge>",
Description: "Checks whether chatter writing the current line has the given badge in the current channel",
Syntax: "chatterHasBadge <badge>",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ botHasBadge "moderator" }}`,
Template: `{{ chatterHasBadge "moderator" }}`,
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 (
"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).
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":
// WHISPER (Twitch Commands)
// 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) {
go handleMessage(i.c, m, eventTypeWhisper, nil)
}

View File

@ -60,10 +60,9 @@ var (
config *configFile
configLock = new(sync.RWMutex)
botUserstate = newTwitchUserStateStore()
cronService *cron.Cron
ircHdl *ircHandler
router = mux.NewRouter()
cronService *cron.Cron
ircHdl *ircHandler
router = mux.NewRouter()
runID = uuid.Must(uuid.NewV4()).String()

View File

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