2021-09-10 17:52:30 +00:00
|
|
|
package modchannel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-10-23 13:06:45 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v2/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v2/plugins"
|
2021-09-10 17:52:30 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
const actorName = "modchannel"
|
|
|
|
|
2021-09-10 17:52:30 +00:00
|
|
|
var (
|
|
|
|
formatMessage plugins.MsgFormatter
|
2022-04-23 15:24:49 +00:00
|
|
|
tcGetter func(string) (*twitch.Client, error)
|
2022-10-31 16:26:53 +00:00
|
|
|
|
|
|
|
ptrStringEmpty = func(s string) *string { return &s }("")
|
2021-09-10 17:52:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
2022-04-23 15:24:49 +00:00
|
|
|
tcGetter = args.GetTwitchClientForChannel
|
2021-09-10 17:52:30 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Update stream information",
|
|
|
|
Name: "Modify Stream",
|
|
|
|
Type: "modchannel",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Channel to update",
|
|
|
|
Key: "channel",
|
|
|
|
Name: "Channel",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
2022-10-01 13:05:21 +00:00
|
|
|
Description: "Category / Game to set (use `@1234` format to pass an explicit ID)",
|
2021-09-22 13:36:45 +00:00
|
|
|
Key: "game",
|
|
|
|
Name: "Game",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Stream title to set",
|
|
|
|
Key: "title",
|
|
|
|
Name: "Title",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-09-10 17:52:30 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type actor struct{}
|
2021-09-10 17:52:30 +00:00
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
var (
|
2022-10-31 16:26:53 +00:00
|
|
|
game = attrs.MustString("game", ptrStringEmpty)
|
|
|
|
title = attrs.MustString("title", ptrStringEmpty)
|
2021-09-22 13:36:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if game == "" && title == "" {
|
2021-09-10 17:52:30 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
var updGame, updTitle *string
|
2021-09-10 17:52:30 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
channel, err := formatMessage(attrs.MustString("channel", nil), m, r, eventData)
|
2021-09-10 17:52:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "parsing channel")
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if game != "" {
|
|
|
|
parsedGame, err := formatMessage(game, m, r, eventData)
|
2021-09-10 17:52:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "parsing game")
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
updGame = &parsedGame
|
2021-09-10 17:52:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if title != "" {
|
|
|
|
parsedTitle, err := formatMessage(title, m, r, eventData)
|
2021-09-10 17:52:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "parsing title")
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
updTitle = &parsedTitle
|
2021-09-10 17:52:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 15:24:49 +00:00
|
|
|
twitchClient, err := tcGetter(strings.TrimLeft(channel, "#"))
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "getting Twitch client")
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:52:30 +00:00
|
|
|
return false, errors.Wrap(
|
2021-09-22 13:36:45 +00:00
|
|
|
twitchClient.ModifyChannelInformation(context.Background(), strings.TrimLeft(channel, "#"), updGame, updTitle),
|
2021-09-10 17:52:30 +00:00
|
|
|
"updating channel info",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a actor) IsAsync() bool { return false }
|
2021-09-22 13:36:45 +00:00
|
|
|
func (a actor) Name() string { return actorName }
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
func (a actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) (err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
if v, err := attrs.String("channel"); err != nil || v == "" {
|
|
|
|
return errors.New("channel must be non-empty string")
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:26:53 +00:00
|
|
|
for _, field := range []string{"channel", "game", "title"} {
|
|
|
|
if err = tplValidator(attrs.MustString(field, ptrStringEmpty)); err != nil {
|
|
|
|
return errors.Wrapf(err, "validating %s template", field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
|
|
|
}
|