twitch-bot/internal/actors/whisper/actor.go

87 lines
2.3 KiB
Go
Raw Normal View History

package whisper
import (
"github.com/go-irc/irc"
"github.com/pkg/errors"
"github.com/Luzifer/twitch-bot/v2/pkg/twitch"
"github.com/Luzifer/twitch-bot/v2/plugins"
)
const actorName = "whisper"
var (
botTwitchClient *twitch.Client
formatMessage plugins.MsgFormatter
)
func Register(args plugins.RegistrationArguments) error {
botTwitchClient = args.GetTwitchClient()
formatMessage = args.FormatMessage
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
args.RegisterActorDocumentation(plugins.ActionDocumentation{
Description: "Send a whisper (requires a verified bot!)",
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,
},
},
})
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) {
to, err := formatMessage(attrs.MustString("to", nil), m, r, eventData)
if err != nil {
return false, errors.Wrap(err, "preparing whisper receiver")
}
msg, err := formatMessage(attrs.MustString("message", nil), m, r, eventData)
if err != nil {
return false, errors.Wrap(err, "preparing whisper message")
}
return false, errors.Wrap(
botTwitchClient.SendWhisper(to, msg),
"sending whisper",
)
}
func (a actor) IsAsync() bool { return false }
func (a actor) Name() string { return actorName }
func (a actor) Validate(attrs *plugins.FieldCollection) (err error) {
if v, err := attrs.String("to"); err != nil || v == "" {
return errors.New("to must be non-empty string")
}
if v, err := attrs.String("message"); err != nil || v == "" {
return errors.New("message must be non-empty string")
}
return nil
}