add simple logging

This commit is contained in:
lyx0 2021-12-13 18:29:53 +01:00
parent 9fd63759fa
commit e713b78b05
2 changed files with 46 additions and 0 deletions

43
pkg/db/log_message.go Normal file
View file

@ -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")
}
}

View file

@ -3,6 +3,7 @@ package handlers
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/db"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -22,6 +23,8 @@ func PrivateMessage(message twitch.PrivateMessage, nb *bot.Bot) {
return return
} }
db.InsertMessage(nb, message.User.Name, message.Channel, message.User.ID, message.Message)
// Thing for #pajlada // Thing for #pajlada
if message.Channel == "pajlada" && message.Message == "pajaS 🚨 ALERT" && message.User.Name == "pajbot" && message.Action { if message.Channel == "pajlada" && message.Message == "pajaS 🚨 ALERT" && message.User.Name == "pajbot" && message.Action {
// log.Info(message.Message) // log.Info(message.Message)