mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-14 02:52:42 +00:00
52 lines
1.3 KiB
Go
52 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
|
||
|
}
|