mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add help column for commands and implement a command setting a new help text
This commit is contained in:
parent
4be80755fc
commit
5dd6402941
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue