diff --git a/cmd/bot/command.go b/cmd/bot/command.go index 996dea9..ef87a9e 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -107,7 +107,19 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { } else { return } - case "debug": // ()edituser level nourylul 1000 + case "editcommand": // ()editcommand level nourylul 1000 + if userLevel < 1000 { + return + } else if msgLen < 4 { + common.Send(target, "Not enough arguments provided.", app.TwitchClient) + return + } else if cmdParams[1] == "level" { + app.EditCommand(cmdParams[2], cmdParams[3], message) + return + } else { + return + } + case "debug": if userLevel < 1000 { // Limit to myself for now. return } else if msgLen < 3 { diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 83270a3..bcb2846 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strconv" "github.com/gempir/go-twitch-irc/v3" "github.com/lyx0/nourybot/pkg/common" @@ -43,18 +44,18 @@ func (app *Application) GetCommand(name, username string) (string, error) { return "", err } - // If the command has no permissions set just return the text. + // If the command has no level set just return the text. // Otherwise check if the level is high enough. - if command.Permission == 0 { + if command.Level == 0 { return command.Text, nil } else { // Get the user from the database to check if the userlevel is equal - // or higher than the command.Permission. + // or higher than the command.Level. user, err := app.Models.Users.Get(username) if err != nil { return "", err } - if user.Level >= command.Permission { + if user.Level >= command.Level { // Userlevel is sufficient so return the command.Text return command.Text, nil } @@ -64,3 +65,25 @@ func (app *Application) GetCommand(name, username string) (string, error) { return "", ErrUserInsufficientLevel } + +func (app *Application) EditCommand(name, lvl string, message twitch.PrivateMessage) { + + level, err := strconv.Atoi(lvl) + if err != nil { + app.Logger.Error(err) + common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrCommandLevelNotInteger), app.TwitchClient) + return + } + + err = app.Models.Commands.SetLevel(name, level) + + 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 command %s to level %v", name, level) + common.Send(message.Channel, reply, app.TwitchClient) + return + } +} diff --git a/cmd/bot/models.go b/cmd/bot/models.go index 41d1d18..3385841 100644 --- a/cmd/bot/models.go +++ b/cmd/bot/models.go @@ -10,9 +10,10 @@ import ( ) var ( - ErrUserLevelNotInteger = errors.New("user level must be a number") - ErrRecordNotFound = errors.New("user not found in the database") - ErrUserInsufficientLevel = errors.New("user has insufficient level") + ErrUserLevelNotInteger = errors.New("user level must be a number") + ErrCommandLevelNotInteger = errors.New("command level must be a number") + ErrRecordNotFound = errors.New("user not found in the database") + ErrUserInsufficientLevel = errors.New("user has insufficient level") ) type config struct { diff --git a/internal/data/commands.go b/internal/data/commands.go index 68c2442..474069b 100644 --- a/internal/data/commands.go +++ b/internal/data/commands.go @@ -6,10 +6,10 @@ import ( ) type Command struct { - ID int `json:"id"` - Name string `json:"name"` - Text string `json:"text"` - Permission int `json:"permission"` + ID int `json:"id"` + Name string `json:"name"` + Text string `json:"text"` + Level int `json:"level"` } type CommandModel struct { @@ -18,7 +18,7 @@ type CommandModel struct { func (c CommandModel) Get(name string) (*Command, error) { query := ` - SELECT id, name, text, permission + SELECT id, name, text, level FROM commands WHERE name = $1` @@ -28,7 +28,7 @@ func (c CommandModel) Get(name string) (*Command, error) { &command.ID, &command.Name, &command.Text, - &command.Permission, + &command.Level, ) if err != nil { @@ -43,10 +43,33 @@ func (c CommandModel) Get(name string) (*Command, error) { return &command, nil } +func (c CommandModel) SetLevel(name string, level int) error { + query := ` + UPDATE commands + SET level = $2 + WHERE name = $1` + + result, err := c.DB.Exec(query, name, level) + if err != nil { + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + return ErrRecordNotFound + } + + return nil +} + func (c CommandModel) Insert(name, text string) error { perms := 0 query := ` - INSERT into commands(name, text, permission) + INSERT into commands(name, text, level) VALUES ($1, $2, $3) ON CONFLICT (name) DO NOTHING diff --git a/internal/data/models.go b/internal/data/models.go index 3857587..9d870a5 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -31,6 +31,7 @@ type Models struct { Commands interface { Get(name string) (*Command, error) Insert(name, text string) error + SetLevel(name string, level int) error Delete(name string) error } }