2021-08-19 13:33:56 +00:00
|
|
|
package respond
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-03 10:35:15 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2020-12-21 00:32:39 +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-06-11 11:52:42 +00:00
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
type actor struct {
|
2021-06-11 11:52:42 +00:00
|
|
|
Respond *string `json:"respond" yaml:"respond"`
|
|
|
|
RespondAsReply *bool `json:"respond_as_reply" yaml:"respond_as_reply"`
|
|
|
|
RespondFallback *string `json:"respond_fallback" yaml:"respond_fallback"`
|
2021-09-03 10:35:15 +00:00
|
|
|
ToChannel *string `json:"to_channel" yaml:"to_channel"`
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
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.Respond == nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
msg, err := formatMessage(*a.Respond, m, r, eventData)
|
2021-06-11 11:52:42 +00:00
|
|
|
if err != nil {
|
|
|
|
if a.RespondFallback == nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing response")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-09-02 15:09:30 +00:00
|
|
|
if msg, err = formatMessage(*a.RespondFallback, m, r, eventData); err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing response fallback")
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-06-02 13:31:16 +00:00
|
|
|
|
2021-09-03 10:35:15 +00:00
|
|
|
toChannel := plugins.DeriveChannel(m, eventData)
|
|
|
|
if a.ToChannel != nil {
|
|
|
|
toChannel = fmt.Sprintf("#%s", strings.TrimLeft(*a.ToChannel, "#"))
|
|
|
|
}
|
|
|
|
|
2021-06-11 11:52:42 +00:00
|
|
|
ircMessage := &irc.Message{
|
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{
|
2021-09-03 10:35:15 +00:00
|
|
|
toChannel,
|
2021-06-11 11:52:42 +00:00
|
|
|
msg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-02 21:26:39 +00:00
|
|
|
if a.RespondAsReply != nil && *a.RespondAsReply && m != nil {
|
2021-06-11 11:52:42 +00:00
|
|
|
id, ok := m.GetTag("id")
|
|
|
|
if ok {
|
|
|
|
if ircMessage.Tags == nil {
|
|
|
|
ircMessage.Tags = make(irc.Tags)
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
ircMessage.Tags["reply-parent-msg-id"] = irc.TagValue(id)
|
2021-06-02 13:31:16 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2021-06-02 13:31:16 +00:00
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2021-06-11 11:52:42 +00:00
|
|
|
c.WriteMessage(ircMessage),
|
|
|
|
"sending response",
|
|
|
|
)
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (a actor) IsAsync() bool { return false }
|
|
|
|
func (a actor) Name() string { return "respond" }
|