twitch-bot/internal/template/userstate/actor.go
Knut Ahlers c78356f68f
Lint: Update linter config, improve code quality
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-01-01 17:52:39 +01:00

52 lines
1.4 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()
// Register provides the plugins.RegisterFunc
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
}