initialize user to database when he uses a command

This commit is contained in:
lyx0 2023-10-10 12:49:06 +02:00
parent 4d8aab7fd4
commit abc3b45656
4 changed files with 8 additions and 11 deletions

View file

@ -62,11 +62,6 @@ func (app *application) GetCommand(target, commandName string, userLevel int) (s
return "", err
}
app.Log.Infow("levels",
"commandName", commandName,
"userLevel", userLevel,
"command.Level", command.Level,
)
if command.Level == 0 {
return command.Text, nil
} else if userLevel >= command.Level {

View file

@ -14,7 +14,9 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
var reply string
// Increments the counter how many commands have been used, called in the ping command.
common.CommandUsed()
go common.CommandUsed()
go app.InitUser(message.User.Name, message.User.ID)
// commandName is the actual name of the command without the prefix.
// e.g. `()ping` would be `ping`.

View file

@ -10,9 +10,9 @@ import (
// 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) InitUser(login, twitchId string, message twitch.PrivateMessage) {
func (app *application) InitUser(login, twitchId string) {
_, err := app.Models.Users.Check(twitchId)
app.Log.Error(err)
//app.Log.Error(err)
if err != nil {
app.Models.Users.Insert(login, twitchId)

View file

@ -23,14 +23,14 @@ type UserModel struct {
// Insert inserts a user model into the database.
func (u UserModel) Insert(login, twitchId string) error {
query := `
INSERT INTO users(login, twitchid)
VALUES ($1, $2)
INSERT INTO users(login, twitchid, level)
VALUES ($1, $2, $3)
ON CONFLICT (login)
DO NOTHING
RETURNING id, added_at;
`
args := []interface{}{login, twitchId}
args := []interface{}{login, twitchId, "0"}
// Execute the query returning the number of affected rows.
result, err := u.DB.Exec(query, args...)