[timeout] [#15] Allow timeout reason to be set

fixes #15

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-02-08 20:41:24 +01:00
parent 4892f05c2a
commit 1d317ab27b
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -1,7 +1,8 @@
package timeout
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/go-irc/irc"
@ -12,7 +13,14 @@ import (
const actorName = "timeout"
var (
formatMessage plugins.MsgFormatter
ptrStringEmpty = func(v string) *string { return &v }("")
)
func Register(args plugins.RegistrationArguments) error {
formatMessage = args.FormatMessage
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })
args.RegisterActorDocumentation(plugins.ActionDocumentation{
@ -30,6 +38,15 @@ func Register(args plugins.RegistrationArguments) error {
SupportTemplate: false,
Type: plugins.ActionDocumentationFieldTypeDuration,
},
{
Default: "",
Description: "Reason why the user was timed out",
Key: "reason",
Name: "Reason",
Optional: true,
SupportTemplate: true,
Type: plugins.ActionDocumentationFieldTypeString,
},
},
})
@ -39,12 +56,27 @@ func Register(args plugins.RegistrationArguments) error {
type actor struct{}
func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
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)
}
return false, errors.Wrap(
c.WriteMessage(&irc.Message{
Command: "PRIVMSG",
Params: []string{
plugins.DeriveChannel(m, eventData),
fmt.Sprintf("/timeout %s %d", plugins.DeriveUser(m, eventData), attrs.MustDuration("duration", nil)/time.Second),
strings.Join(cmd, " "),
},
}),
"sending timeout",