2021-12-13 18:53:52 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lyx0/nourybot/cmd/bot"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type logCommand struct {
|
2021-12-13 19:06:50 +01:00
|
|
|
CommandName string `json:"command_name"`
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-13 19:06:50 +01:00
|
|
|
func (l *logCommand) insert(nb *bot.Bot, commandName, name, channel, id, text string) error {
|
2021-12-13 18:53:52 +01:00
|
|
|
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{
|
2021-12-13 19:06:50 +01:00
|
|
|
LoginName: name,
|
|
|
|
Channel: channel,
|
|
|
|
UserID: id,
|
|
|
|
Text: text,
|
|
|
|
CommandName: commandName,
|
2021-12-13 18:53:52 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-12-13 19:06:50 +01:00
|
|
|
logrus.Info("failed to log command")
|
2021-12-13 18:53:52 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-13 19:26:36 +01:00
|
|
|
// InsertCommand is called on every message that invokes a command
|
|
|
|
// and inserts the message and command details into the database.
|
2021-12-13 19:06:50 +01:00
|
|
|
func InsertCommand(nb *bot.Bot, commandName, name, channel, id, text string) {
|
|
|
|
err := (&logCommand{}).insert(nb, commandName, name, channel, id, text)
|
2021-12-13 18:53:52 +01:00
|
|
|
if err != nil {
|
2021-12-13 19:06:50 +01:00
|
|
|
logrus.Info("failed to log command")
|
2021-12-13 18:53:52 +01:00
|
|
|
}
|
|
|
|
}
|