From 01968697f6bda0e7336f48378a09b7bf83b74250 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Mon, 13 Dec 2021 19:06:50 +0100 Subject: [PATCH] refactor commmand structure, add command name --- pkg/db/log_command.go | 28 +++++++++++++++------------- pkg/handlers/command.go | 6 +++--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/pkg/db/log_command.go b/pkg/db/log_command.go index 4025497..11d5c32 100644 --- a/pkg/db/log_command.go +++ b/pkg/db/log_command.go @@ -9,13 +9,14 @@ import ( ) type logCommand struct { - LoginName string `json:"login_name"` - Channel string `json:"channel"` - UserID string `json:"user_id"` - Text string `json:"message"` + CommandName string `json:"command_name"` + 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 { +func (l *logCommand) insert(nb *bot.Bot, commandName, name, channel, id, text string) error { logrus.Info("logging command") ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() @@ -23,21 +24,22 @@ func (l *logCommand) insert(nb *bot.Bot, name, channel, id, text string) error { collection := nb.MongoClient.Database("nourybot").Collection("commandLogs") _, err := collection.InsertOne(ctx, &logCommand{ - LoginName: name, - Channel: channel, - UserID: id, - Text: text, + LoginName: name, + Channel: channel, + UserID: id, + Text: text, + CommandName: commandName, }) if err != nil { - logrus.Info("failed to log message") + logrus.Info("failed to log command") return err } return nil } -func InsertCommand(nb *bot.Bot, name, channel, id, text string) { - err := (&logCommand{}).insert(nb, name, channel, id, text) +func InsertCommand(nb *bot.Bot, commandName, name, channel, id, text string) { + err := (&logCommand{}).insert(nb, commandName, name, channel, id, text) if err != nil { - logrus.Info("failed to log message") + logrus.Info("failed to log command") } } diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index ab5ca77..2afdb70 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -18,9 +18,6 @@ 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:]) @@ -36,6 +33,9 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) { // target channel target := message.Channel + // Logs the message to the database if it invoked a command. + db.InsertCommand(nb, commandName, message.User.Name, message.Channel, message.User.ID, message.Message) + switch commandName { case "": if msgLen == 1 {