add command to change the level of a command in the database

This commit is contained in:
lyx0 2022-08-11 20:35:22 +02:00
parent a970aa470d
commit ceb9c4b98d
5 changed files with 75 additions and 15 deletions

View file

@ -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 {

View file

@ -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
}
}

View file

@ -11,6 +11,7 @@ import (
var (
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")
)

View file

@ -9,7 +9,7 @@ type Command struct {
ID int `json:"id"`
Name string `json:"name"`
Text string `json:"text"`
Permission int `json:"permission"`
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

View file

@ -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
}
}