mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
allow a user to set his own location
This commit is contained in:
parent
2e7b115795
commit
012dfc03a6
|
@ -198,6 +198,9 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
|
|||
} else if cmdParams[1] == "lastfm" {
|
||||
app.SetLastFMUser(cmdParams[2], message)
|
||||
return
|
||||
} else if cmdParams[1] == "location" {
|
||||
app.SetUserLocation(cmdParams[2], message)
|
||||
return
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -103,6 +103,27 @@ func (app *Application) EditUserLevel(login, lvl string, message twitch.PrivateM
|
|||
}
|
||||
}
|
||||
|
||||
// EditUserLevel tries to update the database record for the supplied
|
||||
// login name with the new level.
|
||||
func (app *Application) SetUserLocation(location string, message twitch.PrivateMessage) {
|
||||
login := message.User.Name
|
||||
|
||||
app.Logger.Infow("SetUserLocation",
|
||||
"location", location,
|
||||
"login", login,
|
||||
)
|
||||
err := app.Models.Users.SetLocation(login, location)
|
||||
if err != nil {
|
||||
common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), app.TwitchClient)
|
||||
app.Logger.Error(err)
|
||||
return
|
||||
} else {
|
||||
reply := fmt.Sprintf("Successfully set your location to %v", location)
|
||||
common.Send(message.Channel, reply, app.TwitchClient)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
|
|
@ -28,6 +28,7 @@ type Models struct {
|
|||
Insert(user *User) error
|
||||
Get(login string) (*User, error)
|
||||
SetLevel(login string, level int) error
|
||||
SetLocation(login, location string) error
|
||||
Delete(login string) error
|
||||
}
|
||||
Commands interface {
|
||||
|
|
|
@ -12,6 +12,7 @@ type User struct {
|
|||
Login string `json:"login"`
|
||||
TwitchID string `json:"twitchid"`
|
||||
Level int `json:"level"`
|
||||
Location string `json:"location,omitempty"`
|
||||
}
|
||||
|
||||
type UserModel struct {
|
||||
|
@ -49,6 +50,33 @@ func (u UserModel) Insert(user *User) error {
|
|||
return 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) SetLocation(login, location string) error {
|
||||
query := `
|
||||
UPDATE users
|
||||
SET location = $2
|
||||
WHERE login = $1`
|
||||
|
||||
result, err := u.DB.Exec(query, login, location)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check how many rows were affected.
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// We want atleast 1, if it is 0 the entry did not exist.
|
||||
if rowsAffected == 0 {
|
||||
return ErrRecordNotFound
|
||||
}
|
||||
|
||||
return 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 {
|
||||
|
@ -80,7 +108,7 @@ func (u UserModel) SetLevel(login string, level int) error {
|
|||
// Get searches the database for a login name and returns the user struct on success.
|
||||
func (u UserModel) Get(login string) (*User, error) {
|
||||
query := `
|
||||
SELECT id, added_at, login, twitchid, level
|
||||
SELECT id, added_at, login, twitchid, level, location
|
||||
FROM users
|
||||
WHERE login = $1`
|
||||
|
||||
|
@ -92,6 +120,7 @@ func (u UserModel) Get(login string) (*User, error) {
|
|||
&user.Login,
|
||||
&user.TwitchID,
|
||||
&user.Level,
|
||||
&user.Location,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
CREATE TABLE IF NOT EXISTS users (
|
||||
id bigserial PRIMARY KEY,
|
||||
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
|
||||
login text UNIQUE NOT NULL,
|
||||
twitchid text NOT NULL,
|
||||
level integer NOT NULL
|
||||
id bigserial PRIMARY KEY,
|
||||
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
|
||||
login text UNIQUE NOT NULL,
|
||||
twitchid text NOT NULL,
|
||||
level integer NOT NULL,
|
||||
location text
|
||||
);
|
||||
|
||||
INSERT INTO users (added_at,login,twitchid,"level") VALUES
|
||||
|
|
Loading…
Reference in a new issue