From 1963c773d6824eeef0c31c8bc3473edd89b45d96 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Thu, 17 Oct 2019 17:02:35 +0800 Subject: [PATCH] go: db: Implement active level --- GoBot/db/active.go | 23 +++++++++++++++++++++++ GoBot/db/db.go | 4 ++++ 2 files changed, 27 insertions(+) create mode 100644 GoBot/db/active.go 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) + } }