diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 810ba19..09cd72e 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -248,7 +248,12 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { // ()weather 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 { diff --git a/cmd/bot/user.go b/cmd/bot/user.go index 432d032..6fc9941 100644 --- a/cmd/bot/user.go +++ b/cmd/bot/user.go @@ -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 to register. Otherwise use ()weather 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) +} diff --git a/internal/data/models.go b/internal/data/models.go index caa1581..db76d03 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -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 diff --git a/internal/data/users.go b/internal/data/users.go index 51c823b..e4bc3c3 100644 --- a/internal/data/users.go +++ b/internal/data/users.go @@ -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 {