new gaypoints feature
This commit is contained in:
parent
69e93622b3
commit
6115e09602
10 changed files with 310 additions and 14 deletions
97
commands/gaypoints/sql.go
Normal file
97
commands/gaypoints/sql.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package gaypoints
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var gpSelectUser *sql.Stmt
|
||||
|
||||
var gpSelectChat *sql.Stmt
|
||||
|
||||
var gpSet *sql.Stmt
|
||||
|
||||
func sqlGetAllGP(chatid int64) (gaypoints map[int64]int64) {
|
||||
rows, err := gpSelectChat.Query(chatid)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to query gaypoints: %v\n", err)
|
||||
}
|
||||
|
||||
gaypoints = make(map[int64]int64)
|
||||
var c int64
|
||||
var b int64
|
||||
for rows.Next() {
|
||||
switch err := rows.Scan(&c, &b); err {
|
||||
case sql.ErrNoRows:
|
||||
gaypoints[c] = -1
|
||||
case nil:
|
||||
gaypoints[c] = b
|
||||
default:
|
||||
log.Panicf("Failed to query gaypoints: %v\n", err)
|
||||
|
||||
}
|
||||
log.Println("[gaypoints.sql] Got gaypoints for " + strconv.FormatInt(c, 10) + " in chat " + strconv.FormatInt(chatid, 10) + ". Value is " + strconv.FormatInt(b, 10))
|
||||
}
|
||||
|
||||
return gaypoints
|
||||
|
||||
}
|
||||
|
||||
func sqlGetGP(chatid int64, userid int64) (gaypoints int64) {
|
||||
row := gpSelectUser.QueryRow(chatid, userid)
|
||||
|
||||
switch err := row.Scan(&gaypoints); err {
|
||||
case sql.ErrNoRows:
|
||||
gaypoints = -1
|
||||
case nil:
|
||||
break
|
||||
default:
|
||||
log.Panicf("Failed to query gaypoints: %v\n", err)
|
||||
|
||||
}
|
||||
log.Println("[gaypoints.sql] Got gaypoints for " + strconv.FormatInt(userid, 10) + " in chat " + strconv.FormatInt(chatid, 10) + ". Value is " + strconv.FormatInt(gaypoints, 10))
|
||||
return gaypoints
|
||||
}
|
||||
|
||||
func sqlSetGP(chatid int64, userid int64, gaypoints int64) {
|
||||
_, err := gpSet.Exec(chatid, userid, gaypoints)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to set gaypoints: %v\n", err)
|
||||
}
|
||||
log.Println("[gaypoints.sql] Set gaypoints for " + strconv.FormatInt(userid, 10) + " in chat " + strconv.FormatInt(chatid, 10) + " to " + strconv.FormatInt(gaypoints, 10))
|
||||
}
|
||||
|
||||
func InitDB() {
|
||||
const dbPath string = "./bloater.db"
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to open sqlite database: %v\n", err)
|
||||
}
|
||||
|
||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS gaypoints (chatid bigint, userid bigint,gaypoints bigint,UNIQUE(chatid,userid))")
|
||||
if err != nil {
|
||||
log.Panicf("Failed to create table: %v\n", err)
|
||||
}
|
||||
log.Println("[gaypoints.sql] Created sqlite table in database at " + dbPath)
|
||||
|
||||
gpSelectUser, err = db.Prepare("SELECT gaypoints FROM gaypoints WHERE chatid = ? AND userid = ?")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
}
|
||||
|
||||
gpSelectChat, err = db.Prepare("SELECT userid,gaypoints FROM gaypoints WHERE chatid = ? ORDER BY gaypoints DESC")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
}
|
||||
|
||||
gpSet, err = db.Prepare("INSERT OR REPLACE INTO gaypoints (chatid, userid, gaypoints) values (?,?,?)")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql insert: %v\n", err)
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue