2021-08-19 13:33:56 +00:00
|
|
|
package whisper
|
2021-05-26 13:36:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-05-26 13:36:03 +00:00
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
var formatMessage plugins.MsgFormatter
|
|
|
|
|
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
|
|
|
args.RegisterActor(func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
return nil
|
2021-05-26 13:36:03 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
type actor struct {
|
2021-06-11 11:52:42 +00:00
|
|
|
WhisperMessage *string `json:"whisper_message" yaml:"whisper_message"`
|
|
|
|
WhisperTo *string `json:"whisper_to" yaml:"whisper_to"`
|
|
|
|
}
|
|
|
|
|
2021-09-02 21:26:39 +00:00
|
|
|
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData plugins.FieldCollection) (preventCooldown bool, err error) {
|
2021-06-11 11:52:42 +00:00
|
|
|
if a.WhisperTo == nil || a.WhisperMessage == nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
to, err := formatMessage(*a.WhisperTo, 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-02 15:09:30 +00:00
|
|
|
msg, err := formatMessage(*a.WhisperMessage, 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
|
|
|
}
|
|
|
|
|
|
|
|
channel := "#tmijs" // As a fallback, copied from tmi.js
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2021-06-11 11:52:42 +00:00
|
|
|
c.WriteMessage(&irc.Message{
|
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{
|
|
|
|
channel,
|
|
|
|
fmt.Sprintf("/w %s %s", to, msg),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
"sending whisper",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (a actor) IsAsync() bool { return false }
|
|
|
|
func (a actor) Name() string { return "whisper" }
|