add help column for commands and implement a command setting a new help text

This commit is contained in:
lyx0 2023-03-04 01:29:29 +00:00
parent 4be80755fc
commit 5dd6402941
4 changed files with 66 additions and 31 deletions

View file

@ -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. // 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) { func (app *Application) DeleteCommand(name string, message twitch.PrivateMessage) {
err := app.Models.Commands.Delete(name) err := app.Models.Commands.Delete(name)

View file

@ -357,6 +357,9 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
} else if cmdParams[1] == "category" { } else if cmdParams[1] == "category" {
app.EditCommandCategory(cmdParams[2], cmdParams[3], message) app.EditCommandCategory(cmdParams[2], cmdParams[3], message)
return return
} else if cmdParams[1] == "help" {
app.EditCommandHelp(cmdParams[2], message)
return
} else { } else {
return return
} }

View file

@ -48,6 +48,35 @@ func (c CommandModel) Get(name string) (*Command, error) {
return &command, nil 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 { func (c CommandModel) Update(command *Command) error {
query := ` query := `
UPDATE commands UPDATE commands
@ -123,8 +152,7 @@ func (c CommandModel) SetLevel(name string, level int) error {
return nil return nil
} }
// SetLevel queries the database for an entry with the provided name, // SetHelp sets the help text for a given name of a command in the database.
// if there is one it updates the entrys level with the provided level.
func (c CommandModel) SetHelp(name string, helptext string) error { func (c CommandModel) SetHelp(name string, helptext string) error {
query := ` query := `
UPDATE commands UPDATE commands
@ -148,35 +176,6 @@ func (c CommandModel) SetHelp(name string, helptext string) error {
return nil 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 // Delete takes in a command name and queries the database for an entry with
// the same name and tries to delete that entry. // the same name and tries to delete that entry.
func (c CommandModel) Delete(name string) error { func (c CommandModel) Delete(name string) error {

View file

@ -35,6 +35,7 @@ type Models struct {
Update(command *Command) error Update(command *Command) error
SetLevel(name string, level int) error SetLevel(name string, level int) error
SetCategory(name, category string) error SetCategory(name, category string) error
SetHelp(name, helptext string) error
Delete(name string) error Delete(name string) error
} }
Timers interface { Timers interface {