add command logging, change Insert function to private

This commit is contained in:
lyx0 2021-12-13 18:53:52 +01:00
parent e713b78b05
commit 6ba03a688b
4 changed files with 52 additions and 5 deletions

43
pkg/db/log_command.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 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")
}
}

View file

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

View file

@ -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:])

View file

@ -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