From 5dd6402941c82c91d946e334ae00ad49cd1b301c Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 4 Mar 2023 01:29:29 +0000 Subject: [PATCH] add help column for commands and implement a command setting a new help text --- cmd/bot/command.go | 32 ++++++++++++++++++++ cmd/bot/commands.go | 3 ++ internal/data/commands.go | 61 +++++++++++++++++++-------------------- internal/data/models.go | 1 + 4 files changed, 66 insertions(+), 31 deletions(-) diff --git a/cmd/bot/command.go b/cmd/bot/command.go index fd7d0ad..bb57f2d 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -117,6 +117,38 @@ func (app *Application) EditCommandCategory(name, category string, message twitc } } +// SetCommandHelp updates the `help` column of a given commands name in the +// database to the provided new help text. +func (app *Application) EditCommandHelp(name string, message twitch.PrivateMessage) { + // snipLength is the length we need to "snip" off of the start of `message`. + // `()editcommand` = +13 + // trailing space = +1 + // zero-based = +1 + // `help` = +4 + // = 19 + snipLength := 19 + + // Split the twitch message at `snipLength` plus length of the name of the + // command that we want to set the help text for so that we get the + // actual help message left over and then assign this string to `text`. + // + // e.g. `()editcommand help FeelsDankMan Returns a FeelsDankMan ascii art. Requires user level 500.` + // | <---- snipLength + name ----> | <------ help text with however many characters. ----> | + // | <--------- 19 + 12 --------> | + text := message.Message[snipLength+len(name) : len(message.Message)] + err := app.Models.Commands.SetHelp(name, text) + + if err != nil { + common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), app.TwitchClient) + app.Logger.Error(err) + return + } else { + reply := fmt.Sprintf("Updated help text for command %s to: %v", name, text) + common.Send(message.Channel, reply, app.TwitchClient) + return + } +} + // DeleteCommand takes in a name value and deletes the command from the database if it exists. func (app *Application) DeleteCommand(name string, message twitch.PrivateMessage) { err := app.Models.Commands.Delete(name) diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 611a4f9..4d9446e 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -357,6 +357,9 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { } else if cmdParams[1] == "category" { app.EditCommandCategory(cmdParams[2], cmdParams[3], message) return + } else if cmdParams[1] == "help" { + app.EditCommandHelp(cmdParams[2], message) + return } else { return } diff --git a/internal/data/commands.go b/internal/data/commands.go index c06db42..11f8f07 100644 --- a/internal/data/commands.go +++ b/internal/data/commands.go @@ -48,6 +48,35 @@ func (c CommandModel) Get(name string) (*Command, error) { return &command, nil } +// Insert adds a command into the database. +func (c CommandModel) Insert(command *Command) error { + query := ` + 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, command.Help} + + result, err := c.DB.Exec(query, args...) + if err != nil { + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + return ErrCommandRecordAlreadyExists + } + + return nil +} + func (c CommandModel) Update(command *Command) error { query := ` UPDATE commands @@ -123,8 +152,7 @@ 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. +// SetHelp sets the help text for a given name of a command in the database. func (c CommandModel) SetHelp(name string, helptext string) error { query := ` UPDATE commands @@ -148,35 +176,6 @@ func (c CommandModel) SetHelp(name string, helptext string) error { return nil } -// Insert adds a command into the database. -func (c CommandModel) Insert(command *Command) error { - query := ` - 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, command.Help} - - result, err := c.DB.Exec(query, args...) - if err != nil { - return err - } - - rowsAffected, err := result.RowsAffected() - if err != nil { - return err - } - - if rowsAffected == 0 { - return ErrCommandRecordAlreadyExists - } - - return nil -} - // Delete takes in a command name and queries the database for an entry with // the same name and tries to delete that entry. func (c CommandModel) Delete(name string) error { diff --git a/internal/data/models.go b/internal/data/models.go index 6b0b206..eaf54df 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -35,6 +35,7 @@ type Models struct { Update(command *Command) error SetLevel(name string, level int) error SetCategory(name, category string) error + SetHelp(name, helptext string) error Delete(name string) error } Timers interface {