2024-01-01 16:52:18 +00:00
|
|
|
// Package shoutout contains an actor to create a Twitch native
|
|
|
|
// shoutout
|
2023-01-20 11:13:59 +00:00
|
|
|
package shoutout
|
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"context"
|
2024-04-03 19:00:28 +00:00
|
|
|
"fmt"
|
2023-01-20 11:13:59 +00:00
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2023-01-20 11:13:59 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
|
2023-01-20 11:13:59 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
const actorName = "shoutout"
|
|
|
|
|
|
|
|
var (
|
2024-04-05 10:19:22 +00:00
|
|
|
botTwitchClient func() *twitch.Client
|
2023-01-20 11:13:59 +00:00
|
|
|
formatMessage plugins.MsgFormatter
|
|
|
|
ptrStringEmpty = func(v string) *string { return &v }("")
|
|
|
|
|
|
|
|
shoutoutChatcommandRegex = regexp.MustCompile(`^/shoutout +([^\s]+)$`)
|
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2023-01-20 11:13:59 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
2024-04-05 10:19:22 +00:00
|
|
|
botTwitchClient = args.GetTwitchClient
|
2023-01-20 11:13:59 +00:00
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Perform a Twitch-native shoutout",
|
|
|
|
Name: "Shoutout",
|
|
|
|
Type: actorName,
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "User to give the shoutout to",
|
|
|
|
Key: "user",
|
|
|
|
Name: "User",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
args.RegisterMessageModFunc("/shoutout", handleChatCommand)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type actor struct{}
|
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
|
2023-01-20 11:13:59 +00:00
|
|
|
user, err := formatMessage(attrs.MustString("user", ptrStringEmpty), m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing user template")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errors.Wrap(
|
2024-04-05 10:19:22 +00:00
|
|
|
botTwitchClient().SendShoutout(
|
2024-01-01 16:52:18 +00:00
|
|
|
context.Background(),
|
2023-01-20 11:13:59 +00:00
|
|
|
plugins.DeriveChannel(m, eventData),
|
|
|
|
user,
|
|
|
|
),
|
|
|
|
"executing shoutout",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2023-01-20 11:13:59 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
|
|
|
if err = attrs.ValidateSchema(
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "user", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
2024-04-08 13:56:12 +00:00
|
|
|
fieldcollection.MustHaveNoUnknowFields,
|
2024-04-03 19:00:28 +00:00
|
|
|
helpers.SchemaValidateTemplateField(tplValidator, "user"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2023-01-20 11:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleChatCommand(m *irc.Message) error {
|
|
|
|
channel := plugins.DeriveChannel(m, nil)
|
|
|
|
|
|
|
|
matches := shoutoutChatcommandRegex.FindStringSubmatch(m.Trailing())
|
|
|
|
if matches == nil {
|
|
|
|
return errors.New("shoutout message does not match required format")
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:19:22 +00:00
|
|
|
if err := botTwitchClient().SendShoutout(context.Background(), channel, matches[1]); err != nil {
|
2023-01-20 11:13:59 +00:00
|
|
|
return errors.Wrap(err, "executing shoutout")
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugins.ErrSkipSendingMessage
|
|
|
|
}
|