From 9ec111749062dbd70676336d73a816fbeb976b68 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 1 Jan 2023 14:41:57 +0100 Subject: [PATCH] [log] Add `log`-actor Signed-off-by: Knut Ahlers --- internal/actors/log/actor.go | 72 ++++++++++++++++++++++++++++++++++++ plugins_core.go | 2 + wiki/Actors.md | 13 +++++++ 3 files changed, 87 insertions(+) create mode 100644 internal/actors/log/actor.go diff --git a/internal/actors/log/actor.go b/internal/actors/log/actor.go new file mode 100644 index 0000000..3b21ea8 --- /dev/null +++ b/internal/actors/log/actor.go @@ -0,0 +1,72 @@ +package log + +import ( + "github.com/go-irc/irc" + "github.com/pkg/errors" + + "github.com/sirupsen/logrus" + + "github.com/Luzifer/twitch-bot/v3/plugins" +) + +var ( + formatMessage plugins.MsgFormatter + ptrStringEmpty = func(v string) *string { return &v }("") +) + +func Register(args plugins.RegistrationArguments) error { + formatMessage = args.FormatMessage + + args.RegisterActor("log", func() plugins.Actor { return &actor{} }) + + args.RegisterActorDocumentation(plugins.ActionDocumentation{ + Description: "Print info log-line to bot log", + Name: "Log output", + Type: "log", + + Fields: []plugins.ActionDocumentationField{ + { + Default: "", + Description: "Messsage to log into bot-log", + Key: "message", + Name: "Message", + Optional: false, + SupportTemplate: true, + Type: plugins.ActionDocumentationFieldTypeString, + }, + }, + }) + + return nil +} + +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) { + message, err := formatMessage(attrs.MustString("message", ptrStringEmpty), m, r, eventData) + if err != nil { + return false, errors.Wrap(err, "executing message template") + } + + logrus.WithFields(logrus.Fields{ + "channel": plugins.DeriveChannel(m, eventData), + "rule": r.UUID, + "username": plugins.DeriveUser(m, eventData), + }).Info(message) + return false, nil +} + +func (a actor) IsAsync() bool { return true } +func (a actor) Name() string { return "log" } + +func (a actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *plugins.FieldCollection) (err error) { + if v, err := attrs.String("message"); err != nil || v == "" { + return errors.New("message must be non-empty string") + } + + if err = tplValidator(attrs.MustString("message", ptrStringEmpty)); err != nil { + return errors.Wrap(err, "validating message template") + } + + return nil +} diff --git a/plugins_core.go b/plugins_core.go index 4302dbb..13b4959 100644 --- a/plugins_core.go +++ b/plugins_core.go @@ -16,6 +16,7 @@ import ( "github.com/Luzifer/twitch-bot/v3/internal/actors/delay" deleteactor "github.com/Luzifer/twitch-bot/v3/internal/actors/delete" "github.com/Luzifer/twitch-bot/v3/internal/actors/filesay" + logActor "github.com/Luzifer/twitch-bot/v3/internal/actors/log" "github.com/Luzifer/twitch-bot/v3/internal/actors/modchannel" "github.com/Luzifer/twitch-bot/v3/internal/actors/nuke" "github.com/Luzifer/twitch-bot/v3/internal/actors/punish" @@ -50,6 +51,7 @@ var ( delay.Register, deleteactor.Register, filesay.Register, + logActor.Register, modchannel.Register, nuke.Register, punish.Register, diff --git a/wiki/Actors.md b/wiki/Actors.md index 695dbd7..37e70c0 100644 --- a/wiki/Actors.md +++ b/wiki/Actors.md @@ -104,6 +104,19 @@ Takes the content of an URL and pastes it to the current channel source: "" ``` +## Log output + +Print info log-line to bot log + +```yaml +- type: log + attributes: + # Messsage to log into bot-log + # Optional: false + # Type: string (Supports Templating) + message: "" +``` + ## Modify Counter Update counter values