2024-01-01 16:52:18 +00:00
|
|
|
// Package timeout contains an actor to timeout users
|
2021-08-19 13:33:56 +00:00
|
|
|
package timeout
|
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"context"
|
2024-04-03 19:00:28 +00:00
|
|
|
"fmt"
|
2022-10-25 16:47:30 +00:00
|
|
|
"regexp"
|
2022-02-08 19:41:24 +00:00
|
|
|
"strconv"
|
2021-08-19 13:33:56 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/helpers"
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/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 (
|
2024-04-05 10:19:22 +00:00
|
|
|
botTwitchClient func() *twitch.Client
|
2022-10-25 16:47:30 +00:00
|
|
|
formatMessage plugins.MsgFormatter
|
|
|
|
ptrStringEmpty = func(v string) *string { return &v }("")
|
|
|
|
|
|
|
|
timeoutChatcommandRegex = regexp.MustCompile(`^/timeout +([^\s]+) +([0-9]+) +(.+)$`)
|
2022-02-08 19:41:24 +00:00
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2021-08-19 13:33:56 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
2024-04-05 10:19:22 +00:00
|
|
|
botTwitchClient = args.GetTwitchClient
|
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",
|
2022-10-25 16:47:30 +00:00
|
|
|
Optional: false,
|
2022-02-08 19:41:24 +00:00
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
2021-09-22 13:36:45 +00:00
|
|
|
},
|
|
|
|
})
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2022-10-25 16:47:30 +00:00
|
|
|
args.RegisterMessageModFunc("/timeout", handleChatCommand)
|
|
|
|
|
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
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
|
2022-02-08 19:41:24 +00:00
|
|
|
reason, err := formatMessage(attrs.MustString("reason", ptrStringEmpty), m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing reason template")
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
return false, errors.Wrap(
|
2024-04-05 10:19:22 +00:00
|
|
|
botTwitchClient().BanUser(
|
2024-01-01 16:52:18 +00:00
|
|
|
context.Background(),
|
2022-10-25 16:47:30 +00:00
|
|
|
plugins.DeriveChannel(m, eventData),
|
|
|
|
plugins.DeriveUser(m, eventData),
|
|
|
|
attrs.MustDuration("duration", nil),
|
|
|
|
reason,
|
|
|
|
),
|
|
|
|
"executing timeout",
|
2021-08-19 13:33:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) IsAsync() bool { return false }
|
|
|
|
func (actor) Name() string { return actorName }
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
|
|
|
if err = attrs.ValidateSchema(
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "duration", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeDuration}),
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "reason", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
2024-04-08 13:56:12 +00:00
|
|
|
fieldcollection.MustHaveNoUnknowFields,
|
2024-04-03 19:00:28 +00:00
|
|
|
helpers.SchemaValidateTemplateField(tplValidator, "reason"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
if attrs.MustDuration("duration", nil) < time.Second {
|
|
|
|
return errors.New("duration must be greater or equal one second")
|
2022-10-31 16:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|
2022-10-25 16:47:30 +00:00
|
|
|
|
|
|
|
func handleChatCommand(m *irc.Message) error {
|
|
|
|
channel := plugins.DeriveChannel(m, nil)
|
|
|
|
|
|
|
|
matches := timeoutChatcommandRegex.FindStringSubmatch(m.Trailing())
|
|
|
|
if matches == nil {
|
|
|
|
return errors.New("timeout message does not match required format")
|
|
|
|
}
|
|
|
|
|
|
|
|
duration, err := strconv.ParseInt(matches[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parsing timeout duration")
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:19:22 +00:00
|
|
|
if err = botTwitchClient().BanUser(context.Background(), channel, matches[1], time.Duration(duration)*time.Second, matches[3]); err != nil {
|
2022-10-25 16:47:30 +00:00
|
|
|
return errors.Wrap(err, "executing timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugins.ErrSkipSendingMessage
|
|
|
|
}
|