From 6ba03a688b131736a0237a9e13c319796db59418 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Mon, 13 Dec 2021 18:53:52 +0100 Subject: [PATCH] add command logging, change Insert function to private --- pkg/db/log_command.go | 43 ++++++++++++++++++++++++++++++++++ pkg/db/log_message.go | 10 ++++---- pkg/handlers/command.go | 3 +++ pkg/handlers/privatemessage.go | 1 + 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 pkg/db/log_command.go diff --git a/pkg/db/log_command.go b/pkg/db/log_command.go new file mode 100644 index 0000000..4025497 --- /dev/null +++ b/pkg/db/log_command.go @@ -0,0 +1,43 @@ +package db + +import ( + "context" + "time" + + "github.com/lyx0/nourybot/cmd/bot" + "github.com/sirupsen/logrus" +) + +type logCommand struct { + LoginName string `json:"login_name"` + Channel string `json:"channel"` + UserID string `json:"user_id"` + Text string `json:"message"` +} + +func (l *logCommand) insert(nb *bot.Bot, name, channel, id, text string) error { + logrus.Info("logging command") + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + collection := nb.MongoClient.Database("nourybot").Collection("commandLogs") + + _, err := collection.InsertOne(ctx, &logCommand{ + LoginName: name, + Channel: channel, + UserID: id, + Text: text, + }) + if err != nil { + logrus.Info("failed to log message") + return err + } + return nil +} + +func InsertCommand(nb *bot.Bot, name, channel, id, text string) { + err := (&logCommand{}).insert(nb, name, channel, id, text) + if err != nil { + logrus.Info("failed to log message") + } +} diff --git a/pkg/db/log_message.go b/pkg/db/log_message.go index ffaf9e1..b6234f6 100644 --- a/pkg/db/log_message.go +++ b/pkg/db/log_message.go @@ -8,21 +8,21 @@ import ( "github.com/sirupsen/logrus" ) -type LogMessage struct { +type logMessage struct { LoginName string `json:"login_name"` Channel string `json:"channel"` UserID string `json:"user_id"` Text string `json:"message"` } -func (l *LogMessage) Insert(nb *bot.Bot, name, channel, id, text string) error { +func (l *logMessage) insert(nb *bot.Bot, name, channel, id, text string) error { logrus.Info("logging message") ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() - collection := nb.MongoClient.Database("nourybot").Collection("logs") + collection := nb.MongoClient.Database("nourybot").Collection("messageLogs") - _, err := collection.InsertOne(ctx, &LogMessage{ + _, err := collection.InsertOne(ctx, &logMessage{ LoginName: name, Channel: channel, UserID: id, @@ -36,7 +36,7 @@ func (l *LogMessage) Insert(nb *bot.Bot, name, channel, id, text string) error { } func InsertMessage(nb *bot.Bot, name, channel, id, text string) { - err := (&LogMessage{}).Insert(nb, name, channel, id, text) + err := (&logMessage{}).insert(nb, name, channel, id, text) if err != nil { logrus.Info("failed to log message") } diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index ecb14a7..ab5ca77 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -18,6 +18,9 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) { utils.CommandUsed() + // Adds the message to the database if it invoked a command. + db.InsertCommand(nb, message.User.Name, message.Channel, message.User.ID, message.Message) + // commandName is the actual command name without the prefix. commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:]) diff --git a/pkg/handlers/privatemessage.go b/pkg/handlers/privatemessage.go index 35b9ef6..a343a39 100644 --- a/pkg/handlers/privatemessage.go +++ b/pkg/handlers/privatemessage.go @@ -23,6 +23,7 @@ func PrivateMessage(message twitch.PrivateMessage, nb *bot.Bot) { return } + // General message logging. Not in use currently. db.InsertMessage(nb, message.User.Name, message.Channel, message.User.ID, message.Message) // Thing for #pajlada