mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add command to change the level of a command in the database
This commit is contained in:
parent
a970aa470d
commit
ceb9c4b98d
|
@ -107,7 +107,19 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
|
||||||
} else {
|
} else {
|
||||||
return
|
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.
|
if userLevel < 1000 { // Limit to myself for now.
|
||||||
return
|
return
|
||||||
} else if msgLen < 3 {
|
} else if msgLen < 3 {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gempir/go-twitch-irc/v3"
|
"github.com/gempir/go-twitch-irc/v3"
|
||||||
"github.com/lyx0/nourybot/pkg/common"
|
"github.com/lyx0/nourybot/pkg/common"
|
||||||
|
@ -43,18 +44,18 @@ func (app *Application) GetCommand(name, username string) (string, error) {
|
||||||
return "", err
|
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.
|
// Otherwise check if the level is high enough.
|
||||||
if command.Permission == 0 {
|
if command.Level == 0 {
|
||||||
return command.Text, nil
|
return command.Text, nil
|
||||||
} else {
|
} else {
|
||||||
// Get the user from the database to check if the userlevel is equal
|
// 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)
|
user, err := app.Models.Users.Get(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if user.Level >= command.Permission {
|
if user.Level >= command.Level {
|
||||||
// Userlevel is sufficient so return the command.Text
|
// Userlevel is sufficient so return the command.Text
|
||||||
return command.Text, nil
|
return command.Text, nil
|
||||||
}
|
}
|
||||||
|
@ -64,3 +65,25 @@ func (app *Application) GetCommand(name, username string) (string, error) {
|
||||||
return "", ErrUserInsufficientLevel
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -10,9 +10,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrUserLevelNotInteger = errors.New("user level must be a number")
|
ErrUserLevelNotInteger = errors.New("user level must be a number")
|
||||||
ErrRecordNotFound = errors.New("user not found in the database")
|
ErrCommandLevelNotInteger = errors.New("command level must be a number")
|
||||||
ErrUserInsufficientLevel = errors.New("user has insufficient level")
|
ErrRecordNotFound = errors.New("user not found in the database")
|
||||||
|
ErrUserInsufficientLevel = errors.New("user has insufficient level")
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
|
|
|
@ -6,10 +6,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
Permission int `json:"permission"`
|
Level int `json:"level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommandModel struct {
|
type CommandModel struct {
|
||||||
|
@ -18,7 +18,7 @@ type CommandModel struct {
|
||||||
|
|
||||||
func (c CommandModel) Get(name string) (*Command, error) {
|
func (c CommandModel) Get(name string) (*Command, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT id, name, text, permission
|
SELECT id, name, text, level
|
||||||
FROM commands
|
FROM commands
|
||||||
WHERE name = $1`
|
WHERE name = $1`
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ func (c CommandModel) Get(name string) (*Command, error) {
|
||||||
&command.ID,
|
&command.ID,
|
||||||
&command.Name,
|
&command.Name,
|
||||||
&command.Text,
|
&command.Text,
|
||||||
&command.Permission,
|
&command.Level,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -43,10 +43,33 @@ func (c CommandModel) Get(name string) (*Command, error) {
|
||||||
return &command, nil
|
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 {
|
func (c CommandModel) Insert(name, text string) error {
|
||||||
perms := 0
|
perms := 0
|
||||||
query := `
|
query := `
|
||||||
INSERT into commands(name, text, permission)
|
INSERT into commands(name, text, level)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3)
|
||||||
ON CONFLICT (name)
|
ON CONFLICT (name)
|
||||||
DO NOTHING
|
DO NOTHING
|
||||||
|
|
|
@ -31,6 +31,7 @@ type Models struct {
|
||||||
Commands interface {
|
Commands interface {
|
||||||
Get(name string) (*Command, error)
|
Get(name string) (*Command, error)
|
||||||
Insert(name, text string) error
|
Insert(name, text string) error
|
||||||
|
SetLevel(name string, level int) error
|
||||||
Delete(name string) error
|
Delete(name string) error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue