twitch-bot/internal/template/userstate/actor.go
Knut Ahlers 1d35bd3a4f
[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>
2023-10-26 19:07:17 +02:00

51 lines
1.3 KiB
Go

// 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
}