refactor commmand structure, add command name

This commit is contained in:
lyx0 2021-12-13 19:06:50 +01:00
parent 1f88396f3d
commit 01968697f6
2 changed files with 18 additions and 16 deletions

View file

@ -9,13 +9,14 @@ import (
) )
type logCommand struct { type logCommand struct {
LoginName string `json:"login_name"` CommandName string `json:"command_name"`
Channel string `json:"channel"` LoginName string `json:"login_name"`
UserID string `json:"user_id"` Channel string `json:"channel"`
Text string `json:"message"` 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") logrus.Info("logging command")
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel() 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") collection := nb.MongoClient.Database("nourybot").Collection("commandLogs")
_, err := collection.InsertOne(ctx, &logCommand{ _, err := collection.InsertOne(ctx, &logCommand{
LoginName: name, LoginName: name,
Channel: channel, Channel: channel,
UserID: id, UserID: id,
Text: text, Text: text,
CommandName: commandName,
}) })
if err != nil { if err != nil {
logrus.Info("failed to log message") logrus.Info("failed to log command")
return err return err
} }
return nil return nil
} }
func InsertCommand(nb *bot.Bot, name, channel, id, text string) { func InsertCommand(nb *bot.Bot, commandName, name, channel, id, text string) {
err := (&logCommand{}).insert(nb, name, channel, id, text) err := (&logCommand{}).insert(nb, commandName, name, channel, id, text)
if err != nil { if err != nil {
logrus.Info("failed to log message") logrus.Info("failed to log command")
} }
} }

View file

@ -18,9 +18,6 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
utils.CommandUsed() 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 is the actual command name without the prefix.
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:]) 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 channel
target := message.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 { switch commandName {
case "": case "":
if msgLen == 1 { if msgLen == 1 {