add adding and deleting user to the database

This commit is contained in:
lyx0 2022-08-09 22:34:12 +02:00
parent fda0e7d8b8
commit 1728fb54f9
6 changed files with 95 additions and 8 deletions

View file

@ -64,7 +64,29 @@ func handleCommand(message twitch.PrivateMessage, app *Application) {
AddChannel(cmdParams[1], message, app)
return
}
case "adduser":
if message.User.ID != "31437432" { // Limit to myself for now.
return
} else if msgLen < 3 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
// ()adduser nourylul 1000
AddUser(cmdParams[1], cmdParams[2], message, app)
return
}
case "deletechannel":
if message.User.ID != "31437432" { // Limit to myself for now.
return
} else if msgLen < 3 {
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
return
} else {
// ()addchannel noemience
DeleteChannel(cmdParams[1], message, app)
return
}
case "deleteuser":
if message.User.ID != "31437432" { // Limit to myself for now.
return
} else if msgLen < 2 {
@ -72,7 +94,7 @@ func handleCommand(message twitch.PrivateMessage, app *Application) {
return
} else {
// ()addchannel noemience
DeleteChannel(cmdParams[1], message, app)
DeleteUser(cmdParams[1], message, app)
return
}
case "bttv":

7
cmd/bot/models.go Normal file
View file

@ -0,0 +1,7 @@
package main
import "errors"
var (
ErrUserLevelNotInteger = errors.New("user level must be a number")
)

56
cmd/bot/user.go Normal file
View file

@ -0,0 +1,56 @@
package main
import (
"fmt"
"strconv"
"github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/data"
"github.com/lyx0/nourybot/pkg/commands/decapi"
"github.com/lyx0/nourybot/pkg/common"
)
func AddUser(login, lvl string, message twitch.PrivateMessage, app *Application) {
userId, err := decapi.GetIdByLogin(login)
if err != nil {
app.Logger.Error(err)
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
return
}
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
}
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
}
}
func DeleteUser(login string, message twitch.PrivateMessage, app *Application) {
err := app.Models.Users.Delete(login)
if err != nil {
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
app.Logger.Error(err)
return
}
reply := fmt.Sprintf("Deleted user %s", login)
common.Send(message.Channel, reply, app.TwitchClient)
}

View file

@ -8,6 +8,7 @@ import (
var (
ErrRecordNotFound = errors.New("record not found")
ErrRecordAlreadyExists = errors.New("channel already in database")
ErrUserAlreadyExists = errors.New("user already in database")
)
// struct Models wraps the models, making them callable
@ -18,9 +19,9 @@ type Models struct {
Get(login string) (*Channel, error)
Delete(login string) error
}
User interface {
Insert(channel *Channel) error
Get(login string) (*Channel, error)
Users interface {
Insert(user *User) error
Get(login string) (*User, error)
Delete(login string) error
}
}
@ -28,5 +29,6 @@ type Models struct {
func NewModels(db *sql.DB) Models {
return Models{
Channels: ChannelModel{DB: db},
Users: UserModel{DB: db},
}
}

View file

@ -27,7 +27,7 @@ func (u UserModel) Insert(user *User) error {
RETURNING id, added_at;
`
args := []interface{}{user.Login, user.TwitchID, 100}
args := []interface{}{user.Login, user.TwitchID, user.Level}
// Execute the query returning the number of affected rows.
result, err := u.DB.Exec(query, args...)
@ -42,7 +42,7 @@ func (u UserModel) Insert(user *User) error {
}
if rowsAffected == 0 {
return ErrRecordAlreadyExists
return ErrUserAlreadyExists
}
return nil

View file

@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS users (
id bigserial PRIMARY KEY,
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
login text NOT NULL,
twitchid text UNIQUE NOT NULL,
login text UNIQUE NOT NULL,
twitchid text NOT NULL,
level integer NOT NULL
);