mirror of https://github.com/velour/catbase.git
Fix CSW
This commit is contained in:
parent
f116636aec
commit
5d6f41db3c
4
main.go
4
main.go
|
@ -12,8 +12,8 @@ import (
|
||||||
"github.com/velour/catbase/plugins/admin"
|
"github.com/velour/catbase/plugins/admin"
|
||||||
"github.com/velour/catbase/plugins/babbler"
|
"github.com/velour/catbase/plugins/babbler"
|
||||||
"github.com/velour/catbase/plugins/beers"
|
"github.com/velour/catbase/plugins/beers"
|
||||||
|
"github.com/velour/catbase/plugins/couldashouldawoulda"
|
||||||
"github.com/velour/catbase/plugins/counter"
|
"github.com/velour/catbase/plugins/counter"
|
||||||
"github.com/velour/catbase/plugins/couldashouldwoulda"
|
|
||||||
"github.com/velour/catbase/plugins/db"
|
"github.com/velour/catbase/plugins/db"
|
||||||
"github.com/velour/catbase/plugins/dice"
|
"github.com/velour/catbase/plugins/dice"
|
||||||
"github.com/velour/catbase/plugins/emojifyme"
|
"github.com/velour/catbase/plugins/emojifyme"
|
||||||
|
@ -79,7 +79,7 @@ func main() {
|
||||||
b.AddHandler("rpgORdie", rpgORdie.New(b))
|
b.AddHandler("rpgORdie", rpgORdie.New(b))
|
||||||
b.AddHandler("sisyphus", sisyphus.New(b))
|
b.AddHandler("sisyphus", sisyphus.New(b))
|
||||||
b.AddHandler("tell", tell.New(b))
|
b.AddHandler("tell", tell.New(b))
|
||||||
b.AddHandler("couldashouldwoulda", couldashouldwoulda.New(b))
|
b.AddHandler("couldashouldawoulda", couldashouldawoulda.New(b))
|
||||||
// catches anything left, will always return true
|
// catches anything left, will always return true
|
||||||
b.AddHandler("factoid", fact.New(b))
|
b.AddHandler("factoid", fact.New(b))
|
||||||
b.AddHandler("db", db.New(b))
|
b.AddHandler("db", db.New(b))
|
||||||
|
|
|
@ -80,6 +80,8 @@ func (p *CSWPlugin) BotMessage(message msg.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *CSWPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }
|
||||||
|
|
||||||
func (p *CSWPlugin) RegisterWeb() *string {
|
func (p *CSWPlugin) RegisterWeb() *string {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,8 +48,7 @@ func (fe *FirstEntry) save(db *sqlx.DB) error {
|
||||||
|
|
||||||
// NewFirstPlugin creates a new FirstPlugin with the Plugin interface
|
// NewFirstPlugin creates a new FirstPlugin with the Plugin interface
|
||||||
func New(b bot.Bot) *FirstPlugin {
|
func New(b bot.Bot) *FirstPlugin {
|
||||||
if b.DBVersion() == 1 {
|
_, err := b.DB().Exec(`create table if not exists last (
|
||||||
_, err := b.DB().Exec(`create table if not exists first (
|
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
day integer,
|
day integer,
|
||||||
time integer,
|
time integer,
|
||||||
|
@ -59,11 +58,20 @@ func New(b bot.Bot) *FirstPlugin {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Could not create first table: ", err)
|
log.Fatal("Could not create first table: ", err)
|
||||||
}
|
}
|
||||||
|
_, err = b.DB().Exec(`create table if not exists first (
|
||||||
|
id integer primary key,
|
||||||
|
day integer,
|
||||||
|
time integer,
|
||||||
|
body string,
|
||||||
|
nick string
|
||||||
|
);`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Could not create first table: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("First plugin initialized with day:", midnight(time.Now()))
|
log.Println("First plugin initialized with day:", midnight(time.Now()))
|
||||||
|
|
||||||
first, err := getLastFirst(b.DB())
|
first, _, err := getLastFirst(b.DB())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Could not initialize first plugin: ", err)
|
log.Fatal("Could not initialize first plugin: ", err)
|
||||||
}
|
}
|
||||||
|
@ -75,7 +83,7 @@ func New(b bot.Bot) *FirstPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLastFirst(db *sqlx.DB) (*FirstEntry, error) {
|
func getLastFirst(db *sqlx.DB) (*FirstEntry, *FirstEntry, error) {
|
||||||
// Get last first entry
|
// Get last first entry
|
||||||
var id sql.NullInt64
|
var id sql.NullInt64
|
||||||
var day sql.NullInt64
|
var day sql.NullInt64
|
||||||
|
@ -96,20 +104,50 @@ func getLastFirst(db *sqlx.DB) (*FirstEntry, error) {
|
||||||
switch {
|
switch {
|
||||||
case err == sql.ErrNoRows || !id.Valid:
|
case err == sql.ErrNoRows || !id.Valid:
|
||||||
log.Println("No previous first entries")
|
log.Println("No previous first entries")
|
||||||
return nil, nil
|
return nil, nil, nil
|
||||||
case err != nil:
|
case err != nil:
|
||||||
log.Println("Error on first query row: ", err)
|
log.Println("Error on first query row: ", err)
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
log.Println(id, day, timeEntered, body, nick)
|
log.Println(id, day, timeEntered, body, nick)
|
||||||
return &FirstEntry{
|
first := &FirstEntry{
|
||||||
id: id.Int64,
|
id: id.Int64,
|
||||||
day: time.Unix(day.Int64, 0),
|
day: time.Unix(day.Int64, 0),
|
||||||
time: time.Unix(timeEntered.Int64, 0),
|
time: time.Unix(timeEntered.Int64, 0),
|
||||||
body: body.String,
|
body: body.String,
|
||||||
nick: nick.String,
|
nick: nick.String,
|
||||||
saved: true,
|
saved: true,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
err = db.QueryRow(`select
|
||||||
|
id, max(day), time, body, nick from last
|
||||||
|
limit 1;
|
||||||
|
`).Scan(
|
||||||
|
&id,
|
||||||
|
&day,
|
||||||
|
&timeEntered,
|
||||||
|
&body,
|
||||||
|
&nick,
|
||||||
|
)
|
||||||
|
switch {
|
||||||
|
case err == sql.ErrNoRows || !id.Valid:
|
||||||
|
log.Println("No previous last entries")
|
||||||
|
return nil, nil, nil
|
||||||
|
case err != nil:
|
||||||
|
log.Println("Error on last query row: ", err)
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
log.Println(id, day, timeEntered, body, nick)
|
||||||
|
last := &FirstEntry{
|
||||||
|
id: id.Int64,
|
||||||
|
day: time.Unix(day.Int64, 0),
|
||||||
|
time: time.Unix(timeEntered.Int64, 0),
|
||||||
|
body: body.String,
|
||||||
|
nick: nick.String,
|
||||||
|
saved: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return first, last, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func midnight(t time.Time) time.Time {
|
func midnight(t time.Time) time.Time {
|
||||||
|
|
Loading…
Reference in New Issue