2024-01-01 16:52:18 +00:00
|
|
|
// Package ban contains actors to ban/unban users in a channel
|
2021-08-19 13:33:56 +00:00
|
|
|
package ban
|
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-12-10 01:28:44 +00:00
|
|
|
"net/http"
|
2022-10-25 16:47:30 +00:00
|
|
|
"regexp"
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-12-10 01:28:44 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-09-11 17:51:38 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2021-11-23 13:46:50 +00:00
|
|
|
|
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 = "ban"
|
|
|
|
|
2021-12-10 01:28:44 +00:00
|
|
|
var (
|
2022-10-25 16:47:30 +00:00
|
|
|
botTwitchClient *twitch.Client
|
|
|
|
formatMessage plugins.MsgFormatter
|
|
|
|
|
|
|
|
banChatcommandRegex = regexp.MustCompile(`^/ban +([^\s]+) +(.+)$`)
|
2021-12-10 01:28:44 +00:00
|
|
|
)
|
2021-11-23 13:46:50 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
// Register provides the plugins.RegisterFunc
|
|
|
|
func Register(args plugins.RegistrationArguments) (err error) {
|
2022-10-25 16:47:30 +00:00
|
|
|
botTwitchClient = args.GetTwitchClient()
|
2021-11-23 13:46:50 +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: "Ban user from chat",
|
|
|
|
Name: "Ban User",
|
|
|
|
Type: "ban",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Reason why the user was banned",
|
|
|
|
Key: "reason",
|
|
|
|
Name: "Reason",
|
2022-10-25 16:47:30 +00:00
|
|
|
Optional: false,
|
2021-11-23 13:46:50 +00:00
|
|
|
SupportTemplate: true,
|
2021-09-22 13:36:45 +00:00
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
2022-03-30 22:24:26 +00:00
|
|
|
Description: "Executes a ban of an user in the specified channel",
|
2021-12-10 01:28:44 +00:00
|
|
|
HandlerFunc: handleAPIBan,
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Module: "ban",
|
2022-03-30 22:24:26 +00:00
|
|
|
Name: "Ban User",
|
2021-12-10 01:28:44 +00:00
|
|
|
Path: "/{channel}/{user}",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Reason to add to the ban",
|
|
|
|
Name: "reason",
|
2022-10-25 16:47:30 +00:00
|
|
|
Required: true,
|
2021-12-10 01:28:44 +00:00
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RequiresWriteAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Channel to ban the user in",
|
|
|
|
Name: "channel",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "User to ban",
|
|
|
|
Name: "user",
|
|
|
|
},
|
|
|
|
},
|
2024-01-01 16:52:18 +00:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("registering API route: %w", err)
|
|
|
|
}
|
2021-12-10 01:28:44 +00:00
|
|
|
|
2022-10-25 16:47:30 +00:00
|
|
|
args.RegisterMessageModFunc("/ban", 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-01-01 16:52:18 +00:00
|
|
|
func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
ptrStringEmpty := func(v string) *string { return &v }("")
|
|
|
|
|
2021-11-23 13:46:50 +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(
|
2022-10-25 16:47:30 +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),
|
|
|
|
0,
|
|
|
|
reason,
|
|
|
|
),
|
|
|
|
"executing ban",
|
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-09-22 13:36:45 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) (err error) {
|
2022-10-31 16:26:53 +00:00
|
|
|
reasonTemplate, err := attrs.String("reason")
|
|
|
|
if err != nil || reasonTemplate == "" {
|
|
|
|
return errors.New("reason must be non-empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tplValidator(reasonTemplate); err != nil {
|
|
|
|
return errors.Wrap(err, "validating reason template")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-10 01:28:44 +00:00
|
|
|
|
|
|
|
func handleAPIBan(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
vars = mux.Vars(r)
|
|
|
|
channel = vars["channel"]
|
|
|
|
user = vars["user"]
|
|
|
|
reason = r.FormValue("reason")
|
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err := botTwitchClient.BanUser(r.Context(), channel, user, 0, reason); err != nil {
|
2021-12-10 01:28:44 +00:00
|
|
|
http.Error(w, errors.Wrap(err, "issuing ban").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
2022-10-25 16:47:30 +00:00
|
|
|
|
|
|
|
func handleChatCommand(m *irc.Message) error {
|
|
|
|
channel := plugins.DeriveChannel(m, nil)
|
|
|
|
|
|
|
|
matches := banChatcommandRegex.FindStringSubmatch(m.Trailing())
|
|
|
|
if matches == nil {
|
|
|
|
return errors.New("ban message does not match required format")
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err := botTwitchClient.BanUser(context.Background(), channel, matches[1], 0, matches[2]); err != nil {
|
2022-10-25 16:47:30 +00:00
|
|
|
return errors.Wrap(err, "executing ban")
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugins.ErrSkipSendingMessage
|
|
|
|
}
|