2021-12-13 18:29:53 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lyx0/nourybot/cmd/bot"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-12-13 18:53:52 +01:00
|
|
|
type logMessage struct {
|
2021-12-13 18:29:53 +01:00
|
|
|
LoginName string `json:"login_name"`
|
|
|
|
Channel string `json:"channel"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
Text string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2021-12-13 18:53:52 +01:00
|
|
|
func (l *logMessage) insert(nb *bot.Bot, name, channel, id, text string) error {
|
2021-12-13 18:29:53 +01:00
|
|
|
logrus.Info("logging message")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-12-13 18:53:52 +01:00
|
|
|
collection := nb.MongoClient.Database("nourybot").Collection("messageLogs")
|
2021-12-13 18:29:53 +01:00
|
|
|
|
2021-12-13 18:53:52 +01:00
|
|
|
_, err := collection.InsertOne(ctx, &logMessage{
|
2021-12-13 18:29:53 +01:00
|
|
|
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) {
|
2021-12-13 18:53:52 +01:00
|
|
|
err := (&logMessage{}).insert(nb, name, channel, id, text)
|
2021-12-13 18:29:53 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Info("failed to log message")
|
|
|
|
}
|
|
|
|
}
|