twitch-bot/internal/actors/ban/actor.go
Knut Ahlers f3ad4a1332
[ban] Enable templating for ban reason
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-11-23 14:46:50 +01:00

76 lines
1.8 KiB
Go

package ban
import (
"strings"
"github.com/go-irc/irc"
"github.com/pkg/errors"
"github.com/Luzifer/twitch-bot/plugins"
)
const actorName = "ban"
var formatMessage plugins.MsgFormatter
func Register(args plugins.RegistrationArguments) error {
formatMessage = args.FormatMessage
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
args.RegisterActorDocumentation(plugins.ActionDocumentation{
Description: "Ban user from chat",
Name: "Ban User",
Type: "ban",
Fields: []plugins.ActionDocumentationField{
{
Default: "",
Description: "Reason why the user was banned",
Key: "reason",
Name: "Reason",
Optional: true,
SupportTemplate: true,
Type: plugins.ActionDocumentationFieldTypeString,
},
},
})
return nil
}
type actor struct{}
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
ptrStringEmpty := func(v string) *string { return &v }("")
reason, err := formatMessage(attrs.MustString("reason", ptrStringEmpty), m, r, eventData)
if err != nil {
return false, errors.Wrap(err, "executing reason template")
}
cmd := []string{
"/ban",
plugins.DeriveUser(m, eventData),
}
if reason != "" {
cmd = append(cmd, reason)
}
return false, errors.Wrap(
c.WriteMessage(&irc.Message{
Command: "PRIVMSG",
Params: []string{
plugins.DeriveChannel(m, eventData),
strings.Join(cmd, " "),
},
}),
"sending ban",
)
}
func (a actor) IsAsync() bool { return false }
func (a actor) Name() string { return actorName }
func (a actor) Validate(attrs *plugins.FieldCollection) (err error) { return nil }