mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
refactor commmand structure, add command name
This commit is contained in:
parent
1f88396f3d
commit
01968697f6
2 changed files with 18 additions and 16 deletions
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue