mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
check if a user is registered in the database everytime a command is used and if not register said user
This commit is contained in:
parent
d6712d314a
commit
a23e7aafe9
|
@ -200,7 +200,7 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
|
||||||
//app.SetLastFMUser(cmdParams[2], message)
|
//app.SetLastFMUser(cmdParams[2], message)
|
||||||
return
|
return
|
||||||
} else if cmdParams[1] == "location" {
|
} else if cmdParams[1] == "location" {
|
||||||
app.SetUserLocation(cmdParams[2], message)
|
app.SetUserLocation(message)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
|
@ -349,17 +349,6 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
|
||||||
app.AddTimer(cmdParams[1], message)
|
app.AddTimer(cmdParams[1], message)
|
||||||
return
|
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
|
// Edit
|
||||||
|
|
|
@ -128,11 +128,11 @@ func main() {
|
||||||
// Received a PrivateMessage (normal chat message).
|
// Received a PrivateMessage (normal chat message).
|
||||||
app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
|
app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
|
||||||
|
|
||||||
app.Logger.Infow("Message received",
|
// app.Logger.Infow("Message received",
|
||||||
"message", message,
|
// "message", message,
|
||||||
"message.User.DisplayName", message.User.DisplayName,
|
// "message.User.DisplayName", message.User.DisplayName,
|
||||||
"message.Message", message.Message,
|
// "message.Message", message.Message,
|
||||||
)
|
// )
|
||||||
|
|
||||||
// roomId is the Twitch UserID of the channel the message originated from.
|
// roomId is the Twitch UserID of the channel the message originated from.
|
||||||
// If there is no roomId something went really wrong.
|
// 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.
|
// Check if the first 2 characters of the mesage were our prefix.
|
||||||
// if they were forward the message to the command handler.
|
// if they were forward the message to the command handler.
|
||||||
if message.Message[:2] == cfg.commandPrefix {
|
if message.Message[:2] == cfg.commandPrefix {
|
||||||
|
app.InitUser(message.User.Name, message.User.ID, message)
|
||||||
app.handleCommand(message)
|
app.handleCommand(message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,48 +6,32 @@ import (
|
||||||
|
|
||||||
"github.com/gempir/go-twitch-irc/v3"
|
"github.com/gempir/go-twitch-irc/v3"
|
||||||
"github.com/lyx0/nourybot/internal/commands"
|
"github.com/lyx0/nourybot/internal/commands"
|
||||||
"github.com/lyx0/nourybot/internal/commands/decapi"
|
|
||||||
"github.com/lyx0/nourybot/internal/common"
|
"github.com/lyx0/nourybot/internal/common"
|
||||||
"github.com/lyx0/nourybot/internal/data"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddUser calls GetIdByLogin to get the twitch id of the login name and then adds
|
// 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.
|
// the login name, twitch id and supplied level to the database.
|
||||||
func (app *Application) AddUser(login, lvl string, message twitch.PrivateMessage) {
|
func (app *Application) InitUser(login, twitchId string, message twitch.PrivateMessage) {
|
||||||
userId, err := decapi.GetIdByLogin(login)
|
sugar := zap.NewExample().Sugar()
|
||||||
|
defer sugar.Sync()
|
||||||
|
|
||||||
|
_, err := app.Models.Users.Check(login)
|
||||||
|
app.Logger.Error(err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error(err)
|
app.Logger.Infow("InitUser: Adding new user:",
|
||||||
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
|
"login: ", login,
|
||||||
|
"twitchId: ", twitchId,
|
||||||
|
)
|
||||||
|
app.Models.Users.Insert(login, twitchId)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the level string to an integer. This is an easy check to see if
|
sugar.Infow("User Insert: User already registered: xd",
|
||||||
// the level supplied was a number only.
|
"login: ", login,
|
||||||
level, err := strconv.Atoi(lvl)
|
"twitchId: ", twitchId,
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DebugUser queries the database for a login name, if that name exists it returns the fields
|
// 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
|
// SetUserLocation sets new location for the user
|
||||||
// login name with the new level.
|
func (app *Application) SetUserLocation(message twitch.PrivateMessage) {
|
||||||
func (app *Application) SetUserLocation(location string, 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
|
login := message.User.Name
|
||||||
|
|
||||||
app.Logger.Infow("SetUserLocation",
|
app.Logger.Infow("SetUserLocation",
|
||||||
|
|
|
@ -21,16 +21,16 @@ type UserModel struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert inserts a user model into the database.
|
// Insert inserts a user model into the database.
|
||||||
func (u UserModel) Insert(user *User) error {
|
func (u UserModel) Insert(login, twitchId string) error {
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO users(login, twitchid, level)
|
INSERT INTO users(login, twitchid)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2)
|
||||||
ON CONFLICT (login)
|
ON CONFLICT (login)
|
||||||
DO NOTHING
|
DO NOTHING
|
||||||
RETURNING id, added_at;
|
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.
|
// Execute the query returning the number of affected rows.
|
||||||
result, err := u.DB.Exec(query, args...)
|
result, err := u.DB.Exec(query, args...)
|
||||||
|
@ -215,6 +215,33 @@ func (u UserModel) Get(login string) (*User, error) {
|
||||||
return &user, nil
|
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
|
// 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.
|
// one exists deletes the record, returning any errors that might occur.
|
||||||
func (u UserModel) Delete(login string) error {
|
func (u UserModel) Delete(login string) error {
|
||||||
|
|
Loading…
Reference in a new issue