From 650e7f450f446d68077a370aaa01ce157802fa87 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Wed, 17 Aug 2022 14:45:35 +0200 Subject: [PATCH] pass in a command to the insert method instead of name, text --- cmd/bot/command.go | 9 ++++++++- internal/data/commands.go | 6 ++---- internal/data/models.go | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/bot/command.go b/cmd/bot/command.go index b84c8fc..ed5ddc9 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/internal/data" "github.com/lyx0/nourybot/pkg/common" ) @@ -22,7 +23,13 @@ func (app *Application) AddCommand(name string, message twitch.PrivateMessage) { // e.g. ()addcommand dank FeelsDankMan // | part1 snip ^ part2 | text := message.Message[prefixLength+len(name) : len(message.Message)] - err := app.Models.Commands.Insert(name, text) + command := &data.Command{ + Name: name, + Text: text, + Category: "uncategorized", + Level: 0, + } + err := app.Models.Commands.Insert(command) // app.Logger.Infow("Message splits", // "Command Name:", name, diff --git a/internal/data/commands.go b/internal/data/commands.go index a38ec73..977374f 100644 --- a/internal/data/commands.go +++ b/internal/data/commands.go @@ -97,9 +97,7 @@ func (c CommandModel) SetLevel(name string, level int) error { } // Insert adds a command into the database. -func (c CommandModel) Insert(name, text string) error { - perms := 0 - category := "uncategorized" +func (c CommandModel) Insert(command *Command) error { query := ` INSERT into commands(name, text, category, level) VALUES ($1, $2, $3, $4) @@ -108,7 +106,7 @@ func (c CommandModel) Insert(name, text string) error { RETURNING id; ` - args := []interface{}{name, text, category, perms} + args := []interface{}{command.Name, command.Text, command.Category, command.Level} result, err := c.DB.Exec(query, args...) if err != nil { diff --git a/internal/data/models.go b/internal/data/models.go index ca44f29..d51ba34 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -30,7 +30,7 @@ type Models struct { } Commands interface { Get(name string) (*Command, error) - Insert(name, text string) error + Insert(command *Command) error SetLevel(name string, level int) error SetCategory(name, category string) error Delete(name string) error