diff --git a/GoBot/db/active.go b/GoBot/db/active.go new file mode 100644 index 0000000..465eee8 --- /dev/null +++ b/GoBot/db/active.go @@ -0,0 +1,23 @@ +package db + +func GetActiveLevel(discordID string) (int, error) { + rows, err := db.Query("SELECT level FROM activeLevel WHERE id=?", discordID) + if err != nil { + return 0, err + } + defer rows.Close() + if rows.Next() { + var activeLevel int + err := rows.Scan(&activeLevel) + if err != nil { + return 0, err + } + return activeLevel, nil + } + return 0, ErrNotFound +} + +func AddActiveLevel(discordID string, addedLevel int) error { + _, err := db.Exec("INSERT INTO activeLevel(id, level) VALUES(?, ?) ON DUPLICATE KEY UPDATE level=level+?", discordID, addedLevel, addedLevel) + return err +} diff --git a/GoBot/db/db.go b/GoBot/db/db.go index ea904f5..b2beebc 100644 --- a/GoBot/db/db.go +++ b/GoBot/db/db.go @@ -39,4 +39,8 @@ func init() { if err != nil { panic(err) } + _, err = db.Exec("CREATE TABLE IF NOT EXISTS activeLevel (id TEXT, level INTEGER, UNIQUE(id))") + if err != nil { + panic(err) + } }