2021-08-19 13:33:56 +00:00
|
|
|
package timeout
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
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) {
|
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),
|
2021-09-22 13:36:45 +00:00
|
|
|
fmt.Sprintf("/timeout %s %d", plugins.DeriveUser(m, eventData), attrs.MustDuration("duration", nil)/time.Second),
|
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
|
|
|
}
|