implement setting a location for your user that is checked when no argument is supplied to the weather command

This commit is contained in:
lyx0 2023-03-05 17:56:02 +00:00
parent d5710d727f
commit daa0469a84
4 changed files with 62 additions and 1 deletions

View file

@ -248,7 +248,12 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
// ()weather <location>
case "weather":
if msgLen < 2 {
if msgLen == 1 {
// Default to first argument supplied being the name
// of the user to look up recently played.
app.CheckWeather(message)
return
} else if msgLen < 2 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {

View file

@ -5,9 +5,11 @@ import (
"strconv"
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/commands"
"github.com/lyx0/nourybot/internal/commands/decapi"
"github.com/lyx0/nourybot/internal/common"
"github.com/lyx0/nourybot/internal/data"
"go.uber.org/zap"
)
// AddUser calls GetIdByLogin to get the twitch id of the login name and then adds
@ -156,3 +158,30 @@ func (app *Application) GetUserLevel(login string) int {
return user.Level
}
}
func (app *Application) CheckWeather(message twitch.PrivateMessage) {
sugar := zap.NewExample().Sugar()
defer sugar.Sync()
twitchLogin := message.User.Name
sugar.Infow("Twitchlogin: ",
"twitchLogin:", twitchLogin,
)
location, err := app.Models.Users.GetLocation(twitchLogin)
if err != nil {
sugar.Errorw("No LastFM account registered for: ",
"twitchLogin:", twitchLogin,
)
reply := "No location for your account set in my database. Use ()set location <location> to register. Otherwise use ()weather <location> without registering."
common.Send(message.Channel, reply, app.TwitchClient)
return
}
target := message.Channel
sugar.Infow("Twitchlogin: ",
"twitchLogin:", twitchLogin,
"location:", location,
)
commands.Weather(target, location, app.TwitchClient)
}

View file

@ -29,6 +29,7 @@ type Models struct {
Get(login string) (*User, error)
SetLevel(login string, level int) error
SetLocation(login, location string) error
GetLocation(login string) (string, error)
SetLastFM(login, lastfmUser string) error
GetLastFM(login string) (string, error)
Delete(login string) error

View file

@ -78,6 +78,32 @@ func (u UserModel) SetLocation(login, location string) 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) GetLocation(login string) (string, error) {
query := `
SELECT location
FROM users
WHERE login = $1`
var user User
err := u.DB.QueryRow(query, login).Scan(
&user.Location,
)
if err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
return "", ErrRecordNotFound
default:
return "", err
}
}
return user.Location, 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) SetLastFM(login, lastfmUser string) error {