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 {
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()
@ -27,17 +28,18 @@ func (l *logCommand) insert(nb *bot.Bot, name, channel, id, text string) error {
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")
}
}

View file

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