You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
_ "github.com/mattn/go-sqlite3" // the _ in front allows us to use the standard sql package
|
|
"database/sql"
|
|
"log"
|
|
"strings"
|
|
"math/rand"
|
|
)
|
|
|
|
const (
|
|
DBFileName string = "dunks.db"
|
|
DBVersion int = 1
|
|
)
|
|
|
|
var (
|
|
DBConn *sql.DB
|
|
)
|
|
|
|
|
|
/*
|
|
in our init we check and see if the DB exists (or needs to be upgraded
|
|
|
|
-- DB Schema --
|
|
meta: GuildID Text Unique, Version int
|
|
guild tables: SubmittedBy Text, Dunk text unique
|
|
*/
|
|
func init() {
|
|
var err error
|
|
|
|
if DBConn, err = sql.Open("sqlite3", DBFileName); err != nil {
|
|
log.Fatal("couldn't open DB", err)
|
|
} else if err = DBConn.Ping(); err != nil {
|
|
log.Fatal("can't ping db", err)
|
|
}
|
|
|
|
if res, err := DBConn.Query("SELECT Version FROM dunks_meta LIMIT 1"); err != nil {
|
|
log.Println("running initial DB setup")
|
|
|
|
if _, err = DBConn.Exec("CREATE TABLE dunks_meta(GuildID text unique, Version int)"); err != nil {
|
|
DBConn.Close()
|
|
log.Fatal("couldn't create table", err)
|
|
}
|
|
} else {
|
|
defer res.Close()
|
|
res.Next()
|
|
log.Println("checking for and running migrations if needed")
|
|
|
|
var dbver int
|
|
if res.Scan(&dbver); dbver < DBVersion {
|
|
res.Close()
|
|
RunMigrations()
|
|
}
|
|
}
|
|
}
|
|
|
|
// MIGRATION CODE GOES HERE IF IT'S NEEDED
|
|
func RunMigrations() {}
|
|
|
|
// code that adds a new saying into the db for a guild
|
|
// returns an error if something went wrong adding it to the db
|
|
func AddDunk(guildID, authorID, text string) error {
|
|
var tableName string = "dunks_" + guildID
|
|
|
|
_, err := DBConn.Exec("INSERT INTO TABLE " + tableName + " VALUES($1, $2)",
|
|
authorID, text)
|
|
|
|
return err
|
|
}
|
|
|
|
// creates a new table of dunks for a guild
|
|
func CreateGuildTable(guildID string) error {
|
|
var err error
|
|
var tableName string = "dunks_" + guildID
|
|
_, err = DBConn.Exec("CREATE TABLE IF NOT EXISTS " + tableName + "(SubmittedBy Text, Dunk Text Unique)")
|
|
if err == nil {
|
|
for _, s := range StarterPosts {
|
|
if e := AddDunk(guildID, "system", s); e != nil {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// returns a dunk
|
|
// if withDad is false then only a dunk that doesn't have the word "Dad" in it is returned
|
|
func GetDunk(guildID string, withDad bool) string {
|
|
var tableName string = "dunks_" + guildID
|
|
dunks := make([]string, 0)
|
|
|
|
if query, err := DBConn.Query("SELECT Dunk from " + tableName + ""); err == nil {
|
|
defer query.Close()
|
|
for query.Next() {
|
|
var d string
|
|
|
|
query.Scan(&d)
|
|
|
|
if (!withDad && (strings.Contains(d, "Dad") || strings.Contains(d, "dad"))) || withDad {
|
|
dunks = append(dunks, d)
|
|
}
|
|
}
|
|
|
|
return dunks[rand.Intn(len(dunks))]
|
|
}
|
|
|
|
return ""
|
|
}
|