From a0e724d009b495c6a08821e882cc669172cc2ec1 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Wed, 10 Aug 2022 22:06:36 +0200 Subject: [PATCH] get user level from database for command permissions --- cmd/bot/channel.go | 5 ++++- cmd/bot/command.go | 23 ++++++++++++++++------- cmd/bot/main.go | 19 ------------------- cmd/bot/message.go | 1 - cmd/bot/user.go | 10 ++++++++++ 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/cmd/bot/channel.go b/cmd/bot/channel.go index 406a4dd..965f2bb 100644 --- a/cmd/bot/channel.go +++ b/cmd/bot/channel.go @@ -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) } diff --git a/cmd/bot/command.go b/cmd/bot/command.go index dc0e36b..a8cc6e5 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -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) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 047babf..54b6711 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -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 diff --git a/cmd/bot/message.go b/cmd/bot/message.go index 48c3b59..3b63392 100644 --- a/cmd/bot/message.go +++ b/cmd/bot/message.go @@ -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, diff --git a/cmd/bot/user.go b/cmd/bot/user.go index 65b4742..d759ce8 100644 --- a/cmd/bot/user.go +++ b/cmd/bot/user.go @@ -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 + } +}