2024-01-01 16:52:18 +00:00
|
|
|
// Package punish contains an actor to punish behaviour in a channel
|
|
|
|
// with rising punishments
|
2021-10-03 13:35:58 +00:00
|
|
|
package punish
|
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"context"
|
2024-04-03 19:00:28 +00:00
|
|
|
"fmt"
|
2021-10-03 13:35:58 +00:00
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2023-11-25 19:23:34 +00:00
|
|
|
"gorm.io/gorm"
|
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/database"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-10-03 13:35:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
actorNamePunish = "punish"
|
|
|
|
actorNameResetPunish = "reset-punish"
|
|
|
|
|
|
|
|
oneWeek = 168 * time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-04-05 10:19:22 +00:00
|
|
|
botTwitchClient func() *twitch.Client
|
2024-04-03 19:00:28 +00:00
|
|
|
db database.Connector
|
|
|
|
formatMessage plugins.MsgFormatter
|
2021-10-03 13:35:58 +00:00
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
2021-10-03 13:35:58 +00:00
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
2022-09-10 11:39:07 +00:00
|
|
|
db = args.GetDatabaseConnector()
|
2022-10-22 22:08:02 +00:00
|
|
|
if err := db.DB().AutoMigrate(&punishLevel{}); err != nil {
|
2022-09-10 11:39:07 +00:00
|
|
|
return errors.Wrap(err, "applying schema migration")
|
|
|
|
}
|
|
|
|
|
2023-11-25 19:23:34 +00:00
|
|
|
args.RegisterCopyDatabaseFunc("punish", func(src, target *gorm.DB) error {
|
2024-03-27 23:15:14 +00:00
|
|
|
return database.CopyObjects(src, target, &punishLevel{})
|
2023-11-25 19:23:34 +00:00
|
|
|
})
|
|
|
|
|
2024-04-05 10:19:22 +00:00
|
|
|
botTwitchClient = args.GetTwitchClient
|
2021-10-03 13:35:58 +00:00
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
|
|
|
args.RegisterActor(actorNamePunish, func() plugins.Actor { return &actorPunish{} })
|
|
|
|
args.RegisterActor(actorNameResetPunish, func() plugins.Actor { return &actorResetPunish{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Apply increasing punishments to user",
|
|
|
|
Name: "Punish User",
|
|
|
|
Type: actorNamePunish,
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "168h",
|
|
|
|
Description: "When to lower the punishment level after the last punishment",
|
|
|
|
Key: "cooldown",
|
|
|
|
Name: "Cooldown",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeDuration,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Actions for each punishment level (ban, delete, duration-value i.e. 1m)",
|
|
|
|
Key: "levels",
|
|
|
|
Name: "Levels",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeStringSlice,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Reason why the user was banned / timeouted",
|
|
|
|
Key: "reason",
|
|
|
|
Name: "Reason",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "User to apply the action to",
|
|
|
|
Key: "user",
|
|
|
|
Name: "User",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Unique identifier for this punishment to differentiate between punishments in the same channel",
|
|
|
|
Key: "uuid",
|
|
|
|
Name: "UUID",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
|
|
|
Description: "Reset punishment level for user",
|
|
|
|
Name: "Reset User Punishment",
|
|
|
|
Type: actorNameResetPunish,
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "User to reset the level for",
|
|
|
|
Key: "user",
|
|
|
|
Name: "User",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Unique identifier for this punishment to differentiate between punishments in the same channel",
|
|
|
|
Key: "uuid",
|
|
|
|
Name: "UUID",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: false,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
return nil
|
2021-10-03 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
actorPunish struct{}
|
|
|
|
actorResetPunish struct{}
|
|
|
|
|
|
|
|
levelConfig struct {
|
|
|
|
LastLevel int `json:"last_level"`
|
|
|
|
Executed time.Time `json:"executed"`
|
|
|
|
Cooldown time.Duration `json:"cooldown"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Punish
|
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actorPunish) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
|
2021-10-03 13:35:58 +00:00
|
|
|
var (
|
2024-04-03 19:00:28 +00:00
|
|
|
cooldown = attrs.MustDuration("cooldown", helpers.Ptr(oneWeek))
|
|
|
|
reason = attrs.MustString("reason", helpers.Ptr(""))
|
2021-10-03 13:35:58 +00:00
|
|
|
user = attrs.MustString("user", nil)
|
2024-04-03 19:00:28 +00:00
|
|
|
uuid = attrs.MustString("uuid", helpers.Ptr(""))
|
2021-10-03 13:35:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
levels, err := attrs.StringSlice("levels")
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "getting level config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if user, err = formatMessage(user, m, r, eventData); err != nil {
|
|
|
|
return false, errors.Wrap(err, "preparing user")
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
lvl, err := getPunishment(db, plugins.DeriveChannel(m, eventData), user, uuid)
|
2022-09-10 11:39:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "getting stored punishment")
|
|
|
|
}
|
2021-10-03 13:35:58 +00:00
|
|
|
nLvl := int(math.Min(float64(len(levels)-1), float64(lvl.LastLevel+1)))
|
|
|
|
|
|
|
|
switch lt := levels[nLvl]; lt {
|
|
|
|
case "ban":
|
2024-04-05 10:19:22 +00:00
|
|
|
if err = botTwitchClient().BanUser(
|
2024-01-01 16:52:18 +00:00
|
|
|
context.Background(),
|
2022-10-25 16:47:30 +00:00
|
|
|
plugins.DeriveChannel(m, eventData),
|
|
|
|
strings.TrimLeft(user, "@"),
|
|
|
|
0,
|
|
|
|
reason,
|
|
|
|
); err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing user ban")
|
2021-10-03 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "delete":
|
2023-09-11 17:51:38 +00:00
|
|
|
msgID, ok := m.Tags["id"]
|
2021-10-03 13:35:58 +00:00
|
|
|
if !ok || msgID == "" {
|
|
|
|
return false, errors.New("found no mesage id")
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:19:22 +00:00
|
|
|
if err = botTwitchClient().DeleteMessage(
|
2024-01-01 16:52:18 +00:00
|
|
|
context.Background(),
|
2022-10-25 16:47:30 +00:00
|
|
|
plugins.DeriveChannel(m, eventData),
|
|
|
|
msgID,
|
|
|
|
); err != nil {
|
|
|
|
return false, errors.Wrap(err, "deleting message")
|
|
|
|
}
|
2021-10-03 13:35:58 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
to, err := time.ParseDuration(lt)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "parsing punishment level")
|
|
|
|
}
|
|
|
|
|
2024-04-05 10:19:22 +00:00
|
|
|
if err = botTwitchClient().BanUser(
|
2024-01-01 16:52:18 +00:00
|
|
|
context.Background(),
|
2021-10-03 13:35:58 +00:00
|
|
|
plugins.DeriveChannel(m, eventData),
|
2022-10-25 16:47:30 +00:00
|
|
|
strings.TrimLeft(user, "@"),
|
|
|
|
to,
|
|
|
|
reason,
|
|
|
|
); err != nil {
|
|
|
|
return false, errors.Wrap(err, "executing user ban")
|
|
|
|
}
|
2021-10-03 13:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lvl.Cooldown = cooldown
|
2022-10-22 22:08:02 +00:00
|
|
|
lvl.Executed = time.Now().UTC()
|
2021-10-03 13:35:58 +00:00
|
|
|
lvl.LastLevel = nLvl
|
|
|
|
|
|
|
|
return false, errors.Wrap(
|
2022-10-22 22:08:02 +00:00
|
|
|
setPunishment(db, plugins.DeriveChannel(m, eventData), user, uuid, lvl),
|
2021-10-03 13:35:58 +00:00
|
|
|
"storing punishment level",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actorPunish) IsAsync() bool { return false }
|
|
|
|
func (actorPunish) Name() string { return actorNamePunish }
|
2021-10-03 13:35:58 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actorPunish) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
|
|
|
if err = attrs.ValidateSchema(
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "levels", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeStringSlice}),
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "user", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "cooldown", Type: fieldcollection.SchemaFieldTypeDuration}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "reason", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "uuid", 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, "user"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2022-10-31 16:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 13:35:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actorResetPunish) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *fieldcollection.FieldCollection, attrs *fieldcollection.FieldCollection) (preventCooldown bool, err error) {
|
2021-10-03 13:35:58 +00:00
|
|
|
var (
|
|
|
|
user = attrs.MustString("user", nil)
|
2024-04-03 19:00:28 +00:00
|
|
|
uuid = attrs.MustString("uuid", helpers.Ptr(""))
|
2021-10-03 13:35:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if user, err = formatMessage(user, m, r, eventData); err != nil {
|
|
|
|
return false, errors.Wrap(err, "preparing user")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errors.Wrap(
|
2022-10-22 22:08:02 +00:00
|
|
|
deletePunishment(db, plugins.DeriveChannel(m, eventData), user, uuid),
|
2021-10-03 13:35:58 +00:00
|
|
|
"resetting punishment level",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actorResetPunish) IsAsync() bool { return false }
|
|
|
|
func (actorResetPunish) Name() string { return actorNameResetPunish }
|
2021-10-03 13:35:58 +00:00
|
|
|
|
2024-04-03 19:00:28 +00:00
|
|
|
func (actorResetPunish) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
|
|
|
if err = attrs.ValidateSchema(
|
|
|
|
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "user", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
|
|
|
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "uuid", 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, "user"),
|
|
|
|
); err != nil {
|
|
|
|
return fmt.Errorf("validating attributes: %w", err)
|
2022-10-31 16:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 13:35:58 +00:00
|
|
|
return nil
|
|
|
|
}
|