Skip to content

Commit

Permalink
[timeout] [#15] Allow timeout reason to be set
Browse files Browse the repository at this point in the history
fixes #15

Signed-off-by: Knut Ahlers <knut@ahlers.me>
  • Loading branch information
Luzifer committed Feb 8, 2022
1 parent 4892f05 commit 1d317ab
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions internal/actors/timeout/actor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package timeout

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/go-irc/irc"
Expand All @@ -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{
Expand All @@ -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,
},
},
})

Expand All @@ -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",
Expand Down

0 comments on commit 1d317ab

Please sign in to comment.