diff --git a/internal/actors/timeout/actor.go b/internal/actors/timeout/actor.go index 2e856e1..fd5c13b 100644 --- a/internal/actors/timeout/actor.go +++ b/internal/actors/timeout/actor.go @@ -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",