mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 00:30:02 +00:00
Knut Ahlers
1d35bd3a4f
and fix `botHasBadge` which previously did what `chatterHasBadge` does now which was fully incorrect behavior Signed-off-by: Knut Ahlers <knut@ahlers.me>
51 lines
1.3 KiB
Go
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
|
|
}
|