diff --git a/cmd/bot/command.go b/cmd/bot/command.go index 47c5348..fd7d0ad 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -28,6 +28,7 @@ func (app *Application) AddCommand(name string, message twitch.PrivateMessage) { Text: text, Category: "uncategorized", Level: 0, + Help: "", } err := app.Models.Commands.Insert(command) diff --git a/internal/commands/help.go b/internal/commands/help.go new file mode 100644 index 0000000..7f891a9 --- /dev/null +++ b/internal/commands/help.go @@ -0,0 +1,3 @@ +package commands + + diff --git a/internal/data/commands.go b/internal/data/commands.go index 6c5f51f..c06db42 100644 --- a/internal/data/commands.go +++ b/internal/data/commands.go @@ -11,6 +11,7 @@ type Command struct { Text string `json:"text,omitempty"` Category string `json:"category,omitempty"` Level int `json:"level,omitempty"` + Help string `json:"help,omitempty"` } type CommandModel struct { @@ -20,7 +21,7 @@ type CommandModel struct { // Get tries to find a command in the database with the provided name. func (c CommandModel) Get(name string) (*Command, error) { query := ` - SELECT id, name, text, category, level + SELECT id, name, text, category, level, help FROM commands WHERE name = $1` @@ -32,6 +33,7 @@ func (c CommandModel) Get(name string) (*Command, error) { &command.Text, &command.Category, &command.Level, + &command.Help, ) if err != nil { @@ -49,15 +51,13 @@ func (c CommandModel) Get(name string) (*Command, error) { func (c CommandModel) Update(command *Command) error { query := ` UPDATE commands - SET text = $2, category = $3, level = $4 + SET text = $2 WHERE name = $1 RETURNING id` args := []interface{}{ command.Name, command.Text, - command.Category, - command.Level, } err := c.DB.QueryRow(query, args...).Scan(&command.ID) @@ -123,17 +123,42 @@ func (c CommandModel) SetLevel(name string, level int) error { return nil } +// SetLevel queries the database for an entry with the provided name, +// if there is one it updates the entrys level with the provided level. +func (c CommandModel) SetHelp(name string, helptext string) error { + query := ` + UPDATE commands + SET help = $2 + WHERE name = $1` + + result, err := c.DB.Exec(query, name, helptext) + if err != nil { + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + return ErrRecordNotFound + } + + return nil +} + // Insert adds a command into the database. func (c CommandModel) Insert(command *Command) error { query := ` - INSERT into commands(name, text, category, level) - VALUES ($1, $2, $3, $4) + INSERT into commands(name, text, category, level, help) + VALUES ($1, $2, $3, $4, $5) ON CONFLICT (name) DO NOTHING RETURNING id; ` - args := []interface{}{command.Name, command.Text, command.Category, command.Level} + args := []interface{}{command.Name, command.Text, command.Category, command.Level, command.Help} result, err := c.DB.Exec(query, args...) if err != nil {