mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
get a users level from their twitch id instead of twitch username
This commit is contained in:
parent
a23e7aafe9
commit
6b255a2bb5
4 changed files with 37 additions and 11 deletions
|
@ -44,7 +44,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
|
||||||
// 250 = vip
|
// 250 = vip
|
||||||
// 100 = normal
|
// 100 = normal
|
||||||
// If the level returned is 0 then the user was not found in the database.
|
// 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",
|
app.Logger.Infow("Command received",
|
||||||
// "message", message, // Pretty taxing
|
// "message", message, // Pretty taxing
|
||||||
|
|
|
@ -16,7 +16,7 @@ func (app *Application) InitUser(login, twitchId string, message twitch.PrivateM
|
||||||
sugar := zap.NewExample().Sugar()
|
sugar := zap.NewExample().Sugar()
|
||||||
defer sugar.Sync()
|
defer sugar.Sync()
|
||||||
|
|
||||||
_, err := app.Models.Users.Check(login)
|
_, err := app.Models.Users.Check(twitchId)
|
||||||
app.Logger.Error(err)
|
app.Logger.Error(err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Infow("InitUser: Adding new user:",
|
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
|
// 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.
|
// 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.
|
// Returns 0 on an error which is the level for unregistered users.
|
||||||
func (app *Application) GetUserLevel(login string) int {
|
func (app *Application) GetUserLevel(twitchId string) int {
|
||||||
user, err := app.Models.Users.Get(login)
|
userLevel, err := app.Models.Users.GetLevel(twitchId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
} else {
|
} else {
|
||||||
return user.Level
|
return userLevel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,8 +27,9 @@ type Models struct {
|
||||||
Users interface {
|
Users interface {
|
||||||
Insert(login, twitchId string) error
|
Insert(login, twitchId string) error
|
||||||
Get(login string) (*User, error)
|
Get(login string) (*User, error)
|
||||||
Check(login string) (*User, error)
|
Check(twitchId string) (*User, error)
|
||||||
SetLevel(login string, level int) error
|
SetLevel(login string, level int) error
|
||||||
|
GetLevel(twitchId string) (int, error)
|
||||||
SetLocation(login, location string) error
|
SetLocation(login, location string) error
|
||||||
GetLocation(login string) (string, error)
|
GetLocation(login string) (string, error)
|
||||||
SetLastFM(login, lastfmUser string) error
|
SetLastFM(login, lastfmUser string) error
|
||||||
|
|
|
@ -157,6 +157,32 @@ func (u UserModel) GetLastFM(login string) (string, error) {
|
||||||
return user.LastFMUsername, nil
|
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
|
// Setlevel searches the database for a record with the provided login value
|
||||||
// and if that exists sets the level to the supplied level value.
|
// and if that exists sets the level to the supplied level value.
|
||||||
func (u UserModel) SetLevel(login string, level int) error {
|
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.
|
// 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 := `
|
query := `
|
||||||
SELECT id, added_at, login
|
SELECT id, login
|
||||||
FROM users
|
FROM users
|
||||||
WHERE login = $1`
|
WHERE twitchid = $1`
|
||||||
|
|
||||||
var user User
|
var user User
|
||||||
|
|
||||||
err := u.DB.QueryRow(query, login).Scan(
|
err := u.DB.QueryRow(query, twitchId).Scan(
|
||||||
&user.ID,
|
&user.ID,
|
||||||
&user.Login,
|
&user.Login,
|
||||||
&user.TwitchID,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue