diff --git a/pkg/db/log_message.go b/pkg/db/log_message.go new file mode 100644 index 0000000..ffaf9e1 --- /dev/null +++ b/pkg/db/log_message.go @@ -0,0 +1,43 @@ +package db + +import ( + "context" + "time" + + "github.com/lyx0/nourybot/cmd/bot" + "github.com/sirupsen/logrus" +) + +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 { + logrus.Info("logging message") + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + collection := nb.MongoClient.Database("nourybot").Collection("logs") + + _, err := collection.InsertOne(ctx, &LogMessage{ + LoginName: name, + Channel: channel, + UserID: id, + Text: text, + }) + if err != nil { + logrus.Info("failed to log message") + return err + } + return nil +} + +func InsertMessage(nb *bot.Bot, name, channel, id, text string) { + err := (&LogMessage{}).Insert(nb, name, channel, id, text) + if err != nil { + logrus.Info("failed to log message") + } +} diff --git a/pkg/handlers/privatemessage.go b/pkg/handlers/privatemessage.go index 978477e..35b9ef6 100644 --- a/pkg/handlers/privatemessage.go +++ b/pkg/handlers/privatemessage.go @@ -3,6 +3,7 @@ package handlers import ( "github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/db" log "github.com/sirupsen/logrus" ) @@ -22,6 +23,8 @@ func PrivateMessage(message twitch.PrivateMessage, nb *bot.Bot) { return } + db.InsertMessage(nb, message.User.Name, message.Channel, message.User.ID, message.Message) + // Thing for #pajlada if message.Channel == "pajlada" && message.Message == "pajaS 🚨 ALERT" && message.User.Name == "pajbot" && message.Action { // log.Info(message.Message)