From 6b255a2bb59073ae04de2ec8dba5b5c8622fc52e Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sun, 5 Mar 2023 20:07:41 +0000 Subject: [PATCH] get a users level from their twitch id instead of twitch username --- cmd/bot/commands.go | 2 +- cmd/bot/user.go | 8 ++++---- internal/data/models.go | 3 ++- internal/data/users.go | 35 ++++++++++++++++++++++++++++++----- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 4cd276e..9b4a52b 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -44,7 +44,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { // 250 = vip // 100 = normal // If the level returned is 0 then the user was not found in the database. - userLevel := app.GetUserLevel(message.User.Name) + userLevel := app.GetUserLevel(message.User.ID) app.Logger.Infow("Command received", // "message", message, // Pretty taxing diff --git a/cmd/bot/user.go b/cmd/bot/user.go index 9c9b879..fe61b4a 100644 --- a/cmd/bot/user.go +++ b/cmd/bot/user.go @@ -16,7 +16,7 @@ func (app *Application) InitUser(login, twitchId string, message twitch.PrivateM sugar := zap.NewExample().Sugar() defer sugar.Sync() - _, err := app.Models.Users.Check(login) + _, err := app.Models.Users.Check(twitchId) app.Logger.Error(err) if err != nil { app.Logger.Infow("InitUser: Adding new user:", @@ -144,12 +144,12 @@ func (app *Application) SetUserLastFM(lastfmUser string, message twitch.PrivateM // GetUserLevel takes in a login name and queries the database for an entry // with such a name value. If there is one it returns the level value as an integer. // Returns 0 on an error which is the level for unregistered users. -func (app *Application) GetUserLevel(login string) int { - user, err := app.Models.Users.Get(login) +func (app *Application) GetUserLevel(twitchId string) int { + userLevel, err := app.Models.Users.GetLevel(twitchId) if err != nil { return 0 } else { - return user.Level + return userLevel } } diff --git a/internal/data/models.go b/internal/data/models.go index 7fb878d..57acfc9 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -27,8 +27,9 @@ type Models struct { Users interface { Insert(login, twitchId string) error Get(login string) (*User, error) - Check(login string) (*User, error) + Check(twitchId string) (*User, error) SetLevel(login string, level int) error + GetLevel(twitchId string) (int, error) SetLocation(login, location string) error GetLocation(login string) (string, error) SetLastFM(login, lastfmUser string) error diff --git a/internal/data/users.go b/internal/data/users.go index eefb6ff..df9950f 100644 --- a/internal/data/users.go +++ b/internal/data/users.go @@ -157,6 +157,32 @@ func (u UserModel) GetLastFM(login string) (string, error) { return user.LastFMUsername, nil } +// SetLocation searches the database for a record with the provided login value +// and if that exists sets the location to the supplied +func (u UserModel) GetLevel(twitchId string) (int, error) { + query := ` + SELECT level + FROM users + WHERE twitchid = $1` + + var user User + + err := u.DB.QueryRow(query, twitchId).Scan( + &user.Level, + ) + + if err != nil { + switch { + case errors.Is(err, sql.ErrNoRows): + return -1, ErrRecordNotFound + default: + return -1, err + } + } + + return user.Level, nil +} + // Setlevel searches the database for a record with the provided login value // and if that exists sets the level to the supplied level value. func (u UserModel) SetLevel(login string, level int) error { @@ -216,18 +242,17 @@ func (u UserModel) Get(login string) (*User, error) { } // Check checks the database for a record with the given login name. -func (u UserModel) Check(login string) (*User, error) { +func (u UserModel) Check(twitchId string) (*User, error) { query := ` - SELECT id, added_at, login + SELECT id, login FROM users - WHERE login = $1` + WHERE twitchid = $1` var user User - err := u.DB.QueryRow(query, login).Scan( + err := u.DB.QueryRow(query, twitchId).Scan( &user.ID, &user.Login, - &user.TwitchID, ) if err != nil {