2024-01-01 16:52:18 +00:00
|
|
|
// Package whisper contains an actor to send whispers
|
2021-08-19 13:33:56 +00:00
|
|
|
package whisper
|
2021-05-26 13:36:03 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"context"
|
2024-04-03 19:00:28 +00:00
|
|
|
"fmt"
|
2024-01-01 16:52:18 +00:00
|
|
|
|
2021-05-26 13:36:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2021-11-25 22:48:16 +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"
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-05-26 13:36:03 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
const actorName = "whisper"
|
|
|
|
|
2022-10-25 16:47:30 +00:00
|
|
|
var (
|
|
|
|
botTwitchClient *twitch.Client
|
|
|
|
formatMessage plugins.MsgFormatter
|
|
|
|
)
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2021-08-19 13:33:56 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
2022-10-25 16:47:30 +00:00
|
|
|
botTwitchClient = args.GetTwitchClient()
|
2021-08-19 13:33:56 +00:00
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
2024-04-03 19:00:28 +00:00
|
|
|
Description: "Send a whisper",
|
2021-09-22 13:36:45 +00:00
|
|
|
Name: "Send Whisper",
|
|
|
|
Type: "whisper",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Message to whisper to the user",
|
|
|
|
Key: "message",
|
|
|
|
Name: "Message",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "User to send the message to",
|
|
|
|
Key: "to",
|
|
|
|
Name: "To User",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type actor struct{}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
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) {
|
2021-09-22 13:36:45 +00:00
|
|
|
to, err := formatMessage(attrs.MustString("to", nil), m, r, eventData)
|
2021-06-11 11:52:42 +00:00
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing whisper receiver")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
msg, err := formatMessage(attrs.MustString("message", nil), m, r, eventData)
|
2021-06-11 11:52:42 +00:00
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing whisper message")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2024-01-01 16:52:18 +00:00
|
|
|
botTwitchClient.SendWhisper(context.Background(), to, msg),
|
2021-06-11 11:52:42 +00:00
|
|
|
"sending whisper",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2021-09-22 13:36:45 +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: "message", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "to", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
helpers.SchemaValidateTemplateField(tplValidator, "message", "to"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2022-10-31 16:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
|
|
|
}
|