diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index f5b7e13..4cd276e 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -200,7 +200,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { //app.SetLastFMUser(cmdParams[2], message) return } else if cmdParams[1] == "location" { - app.SetUserLocation(cmdParams[2], message) + app.SetUserLocation(message) return } else { return @@ -349,17 +349,6 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { app.AddTimer(cmdParams[1], message) return } - case "adduser": - if userLevel < 1000 { - return - } else if msgLen < 3 { - common.Send(target, "Not enough arguments provided.", app.TwitchClient) - return - } else { - // ()adduser nourylul 1000 - app.AddUser(cmdParams[1], cmdParams[2], message) - return - } // ###### // Edit diff --git a/cmd/bot/main.go b/cmd/bot/main.go index ef0d1be..5109edb 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -128,11 +128,11 @@ func main() { // Received a PrivateMessage (normal chat message). app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { - app.Logger.Infow("Message received", - "message", message, - "message.User.DisplayName", message.User.DisplayName, - "message.Message", message.Message, - ) + // app.Logger.Infow("Message received", + // "message", message, + // "message.User.DisplayName", message.User.DisplayName, + // "message.Message", message.Message, + // ) // roomId is the Twitch UserID of the channel the message originated from. // If there is no roomId something went really wrong. @@ -150,6 +150,7 @@ func main() { // Check if the first 2 characters of the mesage were our prefix. // if they were forward the message to the command handler. if message.Message[:2] == cfg.commandPrefix { + app.InitUser(message.User.Name, message.User.ID, message) app.handleCommand(message) return } diff --git a/cmd/bot/user.go b/cmd/bot/user.go index 5477829..9c9b879 100644 --- a/cmd/bot/user.go +++ b/cmd/bot/user.go @@ -6,48 +6,32 @@ import ( "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 // the login name, twitch id and supplied level to the database. -func (app *Application) AddUser(login, lvl string, message twitch.PrivateMessage) { - userId, err := decapi.GetIdByLogin(login) +func (app *Application) InitUser(login, twitchId string, message twitch.PrivateMessage) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + _, err := app.Models.Users.Check(login) + app.Logger.Error(err) if err != nil { - app.Logger.Error(err) - common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient) + app.Logger.Infow("InitUser: Adding new user:", + "login: ", login, + "twitchId: ", twitchId, + ) + app.Models.Users.Insert(login, twitchId) + return } - // Convert the level string to an integer. This is an easy check to see if - // the level supplied was a number only. - level, err := strconv.Atoi(lvl) - if err != nil { - app.Logger.Error(err) - common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrUserLevelNotInteger), app.TwitchClient) - return - } - - // Create a user to hold our values to be inserted to the database. - user := &data.User{ - Login: login, - TwitchID: userId, - Level: level, - } - - err = app.Models.Users.Insert(user) - if err != nil { - reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) - common.Send(message.Channel, reply, app.TwitchClient) - return - } else { - reply := fmt.Sprintf("Added user %s with level %v", login, level) - common.Send(message.Channel, reply, app.TwitchClient) - return - } + sugar.Infow("User Insert: User already registered: xd", + "login: ", login, + "twitchId: ", twitchId, + ) } // DebugUser queries the database for a login name, if that name exists it returns the fields @@ -105,9 +89,19 @@ 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) { +// SetUserLocation sets new location for the user +func (app *Application) SetUserLocation(message twitch.PrivateMessage) { + // snipLength is the length we need to "snip" off of the start of `message`. + // `()set location` = +13 + // trailing space = +1 + // zero-based = +1 + // = 16 + snipLength := 15 + + // Split the twitch message at `snipLength` plus length of the name of the + // The part of the message we are left over with is then passed on to the database + // handlers as the `location` part of the command. + location := message.Message[snipLength:len(message.Message)] login := message.User.Name app.Logger.Infow("SetUserLocation", diff --git a/internal/data/users.go b/internal/data/users.go index e4bc3c3..eefb6ff 100644 --- a/internal/data/users.go +++ b/internal/data/users.go @@ -21,16 +21,16 @@ type UserModel struct { } // Insert inserts a user model into the database. -func (u UserModel) Insert(user *User) error { +func (u UserModel) Insert(login, twitchId string) error { query := ` - INSERT INTO users(login, twitchid, level) - VALUES ($1, $2, $3) + INSERT INTO users(login, twitchid) + VALUES ($1, $2) ON CONFLICT (login) DO NOTHING RETURNING id, added_at; ` - args := []interface{}{user.Login, user.TwitchID, user.Level} + args := []interface{}{login, twitchId} // Execute the query returning the number of affected rows. result, err := u.DB.Exec(query, args...) @@ -215,6 +215,33 @@ func (u UserModel) Get(login string) (*User, error) { return &user, nil } +// Check checks the database for a record with the given login name. +func (u UserModel) Check(login string) (*User, error) { + query := ` + SELECT id, added_at, login + FROM users + WHERE login = $1` + + var user User + + err := u.DB.QueryRow(query, login).Scan( + &user.ID, + &user.Login, + &user.TwitchID, + ) + + if err != nil { + switch { + case errors.Is(err, sql.ErrNoRows): + return nil, ErrRecordNotFound + default: + return nil, err + } + } + + return &user, nil +} + // Delete searches the database for a value with the supplied login name and if // one exists deletes the record, returning any errors that might occur. func (u UserModel) Delete(login string) error {