TerraOceanPlugin/GoBot/db/active.go

41 lines
932 B
Go

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 GetTotalActiveLevel() (int, error) {
rows, err := db.Query("SELECT sum(level) FROM activeLevel")
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() {
var totalActiveLevel int
err := rows.Scan(&totalActiveLevel)
if err != nil {
return 0, err
}
return totalActiveLevel, nil
}
return 0, ErrNotFound
}
func AddActiveLevel(discordID string, addedLevel int) error {
_, err := db.Exec("INSERT INTO activeLevel(id, level) VALUES(?, ?) ON CONFLICT(id) DO UPDATE SET level=level+?", discordID, addedLevel, addedLevel)
return err
}