This commit is contained in:
Chris Sexton 2018-06-22 14:25:43 -04:00
parent f116636aec
commit 5d6f41db3c
3 changed files with 54 additions and 14 deletions

View File

@ -12,8 +12,8 @@ import (
"github.com/velour/catbase/plugins/admin"
"github.com/velour/catbase/plugins/babbler"
"github.com/velour/catbase/plugins/beers"
"github.com/velour/catbase/plugins/couldashouldawoulda"
"github.com/velour/catbase/plugins/counter"
"github.com/velour/catbase/plugins/couldashouldwoulda"
"github.com/velour/catbase/plugins/db"
"github.com/velour/catbase/plugins/dice"
"github.com/velour/catbase/plugins/emojifyme"
@ -79,7 +79,7 @@ func main() {
b.AddHandler("rpgORdie", rpgORdie.New(b))
b.AddHandler("sisyphus", sisyphus.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
b.AddHandler("factoid", fact.New(b))
b.AddHandler("db", db.New(b))

View File

@ -80,6 +80,8 @@ func (p *CSWPlugin) BotMessage(message msg.Message) bool {
return false
}
func (p *CSWPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }
func (p *CSWPlugin) RegisterWeb() *string {
return nil
}

View File

@ -48,8 +48,7 @@ func (fe *FirstEntry) save(db *sqlx.DB) error {
// NewFirstPlugin creates a new FirstPlugin with the Plugin interface
func New(b bot.Bot) *FirstPlugin {
if b.DBVersion() == 1 {
_, err := b.DB().Exec(`create table if not exists first (
_, err := b.DB().Exec(`create table if not exists last (
id integer primary key,
day integer,
time integer,
@ -59,11 +58,20 @@ func New(b bot.Bot) *FirstPlugin {
if err != nil {
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()))
first, err := getLastFirst(b.DB())
first, _, err := getLastFirst(b.DB())
if err != nil {
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
var id sql.NullInt64
var day sql.NullInt64
@ -96,20 +104,50 @@ func getLastFirst(db *sqlx.DB) (*FirstEntry, error) {
switch {
case err == sql.ErrNoRows || !id.Valid:
log.Println("No previous first entries")
return nil, nil
return nil, nil, nil
case err != nil:
log.Println("Error on first query row: ", err)
return nil, err
return nil, nil, err
}
log.Println(id, day, timeEntered, body, nick)
return &FirstEntry{
first := &FirstEntry{
id: id.Int64,
day: time.Unix(day.Int64, 0),
time: time.Unix(timeEntered.Int64, 0),
body: body.String,
nick: nick.String,
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 {