implement permissions for commands that checks a users level to be high enough

This commit is contained in:
lyx0 2022-08-11 19:31:58 +02:00
parent 88b114a826
commit a970aa470d
4 changed files with 34 additions and 8 deletions

View file

@ -280,6 +280,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
return return
} else { } else {
commands.Tweet(target, cmdParams[1], app.TwitchClient) commands.Tweet(target, cmdParams[1], app.TwitchClient)
return
} }
case "rxkcd": case "rxkcd":
commands.RandomXkcd(target, app.TwitchClient) commands.RandomXkcd(target, app.TwitchClient)
@ -290,8 +291,11 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
case "xkcd": case "xkcd":
commands.Xkcd(target, app.TwitchClient) commands.Xkcd(target, app.TwitchClient)
return return
default: // Check if the command exists in the database, if it doesnt ignore it
reply, err := app.GetCommand(commandName) // Check if the commandName exists as the "name" of a command in the database.
// if it doesnt then ignore it.
default:
reply, err := app.GetCommand(commandName, message.User.Name)
if err != nil { if err != nil {
return return
} }

View file

@ -35,13 +35,32 @@ func (app *Application) AddCommand(name string, message twitch.PrivateMessage) {
} }
} }
func (app *Application) GetCommand(name string) (string, error) { func (app *Application) GetCommand(name, username string) (string, error) {
// Fetch the command from the database if it exists.
command, err := app.Models.Commands.Get(name) command, err := app.Models.Commands.Get(name)
if err != nil { if err != nil {
// It probably did not exist
return "", err return "", err
} }
return command.Text, nil // If the command has no permissions set just return the text.
// Otherwise check if the level is high enough.
if command.Permission == 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.
user, err := app.Models.Users.Get(username)
if err != nil {
return "", err
}
if user.Level >= command.Permission {
// Userlevel is sufficient so return the command.Text
return command.Text, nil
}
}
// Userlevel was not enough so return an empty string and error.
return "", ErrUserInsufficientLevel
} }

View file

@ -10,8 +10,9 @@ 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") ErrRecordNotFound = errors.New("user not found in the database")
ErrUserInsufficientLevel = errors.New("user has insufficient level")
) )
type config struct { type config struct {

View file

@ -7,4 +7,6 @@ CREATE TABLE IF NOT EXISTS commands (
INSERT INTO commands (name,"text","permission") VALUES INSERT INTO commands (name,"text","permission") VALUES
('repeat','xset r rate 175 50',0), ('repeat','xset r rate 175 50',0),
('eurkey','setxkbmap -layout eu',0); ('eurkey','setxkbmap -layout eu',0),
('dank','⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣾⣿⣿⣿⣿⣷⣄⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⠟⢁⣴⣿⣿⣿⣿⣿⣿⣿⣿⣦⡈⢻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡿⠁⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠙⢿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⡿⠃⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⡈⢻⣿⣿⣿⣿ ⣿⣿⣿⣿⡿⢁⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠹⣿⣿⣿ ⣿⣿⣿⣿⠁⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠿⠿⠿⠿⠆⠘⢿⣿ ⣿⣿⠟⠉⠄⠄⠄⠄⢤⣀⣦⣤⣤⣤⣤⣀⣀⡀⠄⠄⡀⠄⠄⠄⠄⠄⠄⠄⠙ ⣿⠃⠄⠄⠄⠄⠄⠄⠙⠿⣿⣿⠋⠩⠉⠉⢹⣿⣧⣤⣴⣶⣷⣿⠟⠛⠛⣿⣷ ⠇⠄⠄⠄⠄⠄⠄⠄⠄⠄⠁⠒⠄⠄⠄⠄⠈⠉⠛⢻⣿⣿⢿⠁⠄⠄⠁⠘⢁ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣂⣀⣐⣂⣐⣒⣃⠠⠥⠤⠴⠶⠖⠦⠤⠖⢂⣽ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠛⠂⠐⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣠⣴⣶⣿⣿ ⠃⣠⣄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣠⣤⣄⠚⢿⣿⣿⣿⣿ ⣾⣿⣿⣿⣶⣦⣤⣤⣄⣀⣀⣀⣀⣀⣀⣠⣤⣤⣶⣿⣿⣿⣿⣷⡄⢻⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠈⣿⣿⣿',1000);