From a970aa470d2320f176e7934674f770a2d7039026 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 11 Aug 2022 19:31:58 +0200 Subject: [PATCH] implement permissions for commands that checks a users level to be high enough --- cmd/bot/command.go | 8 ++++-- cmd/bot/commands.go | 25 ++++++++++++++++--- cmd/bot/models.go | 5 ++-- .../000003_create_commands_table.up.sql | 4 ++- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cmd/bot/command.go b/cmd/bot/command.go index 8ca32a8..996dea9 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -280,6 +280,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { return } else { commands.Tweet(target, cmdParams[1], app.TwitchClient) + return } case "rxkcd": commands.RandomXkcd(target, app.TwitchClient) @@ -290,8 +291,11 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { case "xkcd": commands.Xkcd(target, app.TwitchClient) 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 { return } diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index c86cedc..83270a3 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -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) - if err != nil { + // It probably did not exist 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 } diff --git a/cmd/bot/models.go b/cmd/bot/models.go index 3ea3f45..41d1d18 100644 --- a/cmd/bot/models.go +++ b/cmd/bot/models.go @@ -10,8 +10,9 @@ import ( ) var ( - ErrUserLevelNotInteger = errors.New("user level must be a number") - ErrRecordNotFound = errors.New("user not found in the database") + 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") ) type config struct { diff --git a/migrations/000003_create_commands_table.up.sql b/migrations/000003_create_commands_table.up.sql index f16254f..8cc168e 100644 --- a/migrations/000003_create_commands_table.up.sql +++ b/migrations/000003_create_commands_table.up.sql @@ -7,4 +7,6 @@ CREATE TABLE IF NOT EXISTS commands ( INSERT INTO commands (name,"text","permission") VALUES ('repeat','xset r rate 175 50',0), - ('eurkey','setxkbmap -layout eu',0); + ('eurkey','setxkbmap -layout eu',0), + ('dank','⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣾⣿⣿⣿⣿⣷⣄⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⠟⢁⣴⣿⣿⣿⣿⣿⣿⣿⣿⣦⡈⢻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡿⠁⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠙⢿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⡿⠃⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⡈⢻⣿⣿⣿⣿ ⣿⣿⣿⣿⡿⢁⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠹⣿⣿⣿ ⣿⣿⣿⣿⠁⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠿⠿⠿⠿⠆⠘⢿⣿ ⣿⣿⠟⠉⠄⠄⠄⠄⢤⣀⣦⣤⣤⣤⣤⣀⣀⡀⠄⠄⡀⠄⠄⠄⠄⠄⠄⠄⠙ ⣿⠃⠄⠄⠄⠄⠄⠄⠙⠿⣿⣿⠋⠩⠉⠉⢹⣿⣧⣤⣴⣶⣷⣿⠟⠛⠛⣿⣷ ⠇⠄⠄⠄⠄⠄⠄⠄⠄⠄⠁⠒⠄⠄⠄⠄⠈⠉⠛⢻⣿⣿⢿⠁⠄⠄⠁⠘⢁ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣂⣀⣐⣂⣐⣒⣃⠠⠥⠤⠴⠶⠖⠦⠤⠖⢂⣽ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠛⠂⠐⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣠⣴⣶⣿⣿ ⠃⣠⣄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣠⣤⣄⠚⢿⣿⣿⣿⣿ ⣾⣿⣿⣿⣶⣦⣤⣤⣄⣀⣀⣀⣀⣀⣀⣠⣤⣤⣶⣿⣿⣿⣿⣷⡄⢻⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠈⣿⣿⣿',1000); +