mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
implement permissions for commands that checks a users level to be high enough
This commit is contained in:
parent
88b114a826
commit
a970aa470d
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue