get user level from database for command permissions

This commit is contained in:
lyx0 2022-08-10 22:06:36 +02:00
parent 94f21a2a66
commit a0e724d009
5 changed files with 30 additions and 28 deletions

View file

@ -22,16 +22,17 @@ func (app *Application) AddChannel(login string, message twitch.PrivateMessage)
}
err = app.Models.Channels.Insert(channel)
if err != nil {
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
common.Send(message.Channel, reply, app.TwitchClient)
return
} else {
app.TwitchClient.Join(login)
reply := fmt.Sprintf("Added channel %s", login)
common.Send(message.Channel, reply, app.TwitchClient)
return
}
}
func (app *Application) GetAllChannels() {
@ -52,6 +53,8 @@ func (app *Application) DeleteChannel(login string, message twitch.PrivateMessag
return
}
app.TwitchClient.Depart(login)
reply := fmt.Sprintf("Deleted channel %s", login)
common.Send(message.Channel, reply, app.TwitchClient)
}

View file

@ -38,9 +38,18 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
// where we are responding.
target := message.Channel
// Userlevel is the level set for a user in the database.
// It is NOT same as twitch user/mod.
// 1000 = admin
// 500 = mod
// 100 = normal
// If the level returned is 0 then the user was not found in the database.
userLevel := app.GetUserLevel(message.User.Name)
sugar.Infow("Command received",
// "message", message,
"message.Channel", target,
"user level", userLevel,
"message.Message", message.Message,
"commandName", commandName,
"cmdParams", cmdParams,
@ -54,7 +63,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
return
}
case "addchannel":
if message.User.ID != "31437432" { // Limit to myself for now.
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
@ -65,7 +74,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
return
}
case "adduser":
if message.User.ID != "31437432" { // Limit to myself for now.
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 3 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
@ -76,7 +85,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
return
}
case "edituser": // ()edituser level nourylul 1000
if message.User.ID != "31437432" { // Limit to myself for now.
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 4 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
@ -88,7 +97,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
return
}
case "debug": // ()edituser level nourylul 1000
if message.User.ID != "31437432" { // Limit to myself for now.
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 3 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
@ -100,7 +109,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
return
}
case "deletechannel":
if message.User.ID != "31437432" { // Limit to myself for now.
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
@ -111,7 +120,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
return
}
case "deleteuser":
if message.User.ID != "31437432" { // Limit to myself for now.
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
@ -153,7 +162,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
commands.Currency(target, cmdParams[1], cmdParams[2], cmdParams[4], app.TwitchClient)
return
case "echo":
if message.User.ID != "31437432" { // Limit to myself for now.
if userLevel < 1000 { // Limit to myself for now.
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)

View file

@ -15,25 +15,6 @@ import (
"go.uber.org/zap"
)
type config struct {
twitchUsername string
twitchOauth string
environment string
db struct {
dsn string
maxOpenConns int
maxIdleConns int
maxIdleTime string
}
}
type Application struct {
TwitchClient *twitch.Client
Logger *zap.SugaredLogger
Db *sql.DB
Models data.Models
}
func main() {
var cfg config

View file

@ -29,7 +29,6 @@ func (app *Application) handlePrivateMessage(message twitch.PrivateMessage) {
}
}
// Message was no command so we just print it.
// app.Logger.Infow("Private Message received",
// // "message", message,
// "message.Channel", message.Channel,

View file

@ -92,3 +92,13 @@ func (app *Application) EditUserLevel(user, lvl string, message twitch.PrivateMe
}
}
func (app *Application) GetUserLevel(login string) int {
user, err := app.Models.Users.Get(login)
if err != nil {
return 0
} else {
return user.Level
}
}