2021-08-19 13:33:56 +00:00
|
|
|
package timeout
|
|
|
|
|
|
|
|
import (
|
2022-02-08 19:41:24 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-08-19 13:33:56 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
"github.com/pkg/errors"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-08-19 13:33:56 +00:00
|
|
|
)
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
const actorName = "timeout"
|
|
|
|
|
2022-02-08 19:41:24 +00:00
|
|
|
var (
|
|
|
|
formatMessage plugins.MsgFormatter
|
|
|
|
ptrStringEmpty = func(v string) *string { return &v }("")
|
|
|
|
)
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
2022-02-08 19:41:24 +00:00
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Timeout user from chat",
|
|
|
|
Name: "Timeout User",
|
|
|
|
Type: "timeout",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Duration of the timeout",
|
|
|
|
Key: "duration",
|
|
|
|
Name: "Duration",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeDuration,
|
|
|
|
},
|
2022-02-08 19:41:24 +00:00
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Reason why the user was timed out",
|
|
|
|
Key: "reason",
|
|
|
|
Name: "Reason",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
2021-09-22 13:36:45 +00:00
|
|
|
},
|
|
|
|
})
|
2021-08-19 13:33:56 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type actor struct{}
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2022-02-08 19:41:24 +00:00
|
|
|
cmd := []string{
|
|
|
|
"/timeout",
|
|
|
|
plugins.DeriveUser(m, eventData),
|
|
|
|
strconv.FormatInt(int64(attrs.MustDuration("duration", nil)/time.Second), 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
reason, err := formatMessage(attrs.MustString("reason", ptrStringEmpty), m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing reason template")
|
|
|
|
}
|
|
|
|
|
|
|
|
if reason != "" {
|
|
|
|
cmd = append(cmd, reason)
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
return false, errors.Wrap(
|
|
|
|
c.WriteMessage(&irc.Message{
|
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{
|
2021-09-02 21:26:39 +00:00
|
|
|
plugins.DeriveChannel(m, eventData),
|
2022-02-08 19:41:24 +00:00
|
|
|
strings.Join(cmd, " "),
|
2021-08-19 13:33:56 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
"sending timeout",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a actor) IsAsync() bool { return false }
|
2021-09-22 13:36:45 +00:00
|
|
|
func (a actor) Name() string { return actorName }
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func (a actor) Validate(attrs *plugins.FieldCollection) (err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
if v, err := attrs.Duration("duration"); err != nil || v < time.Second {
|
|
|
|
return errors.New("duration must be of type duration greater or equal one second")
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|