Update first plugin for SQL

This commit is contained in:
Chris Sexton 2016-01-15 11:54:09 -05:00
parent a40158c158
commit 92e1ea5d42
2 changed files with 86 additions and 28 deletions

View File

@ -57,7 +57,7 @@ func main() {
Bot = bot.NewBot(Config, Client) Bot = bot.NewBot(Config, Client)
// Bot.AddHandler(plugins.NewTestPlugin(Bot)) // Bot.AddHandler(plugins.NewTestPlugin(Bot))
Bot.AddHandler("admin", plugins.NewAdminPlugin(Bot)) Bot.AddHandler("admin", plugins.NewAdminPlugin(Bot))
// Bot.AddHandler("first", plugins.NewFirstPlugin(Bot)) Bot.AddHandler("first", plugins.NewFirstPlugin(Bot))
// Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot)) // Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot))
Bot.AddHandler("talker", plugins.NewTalkerPlugin(Bot)) Bot.AddHandler("talker", plugins.NewTalkerPlugin(Bot))
Bot.AddHandler("dice", plugins.NewDicePlugin(Bot)) Bot.AddHandler("dice", plugins.NewDicePlugin(Bot))

View File

@ -3,6 +3,7 @@
package plugins package plugins
import ( import (
"database/sql"
"fmt" "fmt"
"log" "log"
"regexp" "regexp"
@ -10,8 +11,6 @@ import (
"time" "time"
"github.com/chrissexton/alepale/bot" "github.com/chrissexton/alepale/bot"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
) )
// This is a first plugin to serve as an example and quick copy/paste for new plugins. // This is a first plugin to serve as an example and quick copy/paste for new plugins.
@ -19,41 +18,96 @@ import (
type FirstPlugin struct { type FirstPlugin struct {
First *FirstEntry First *FirstEntry
Bot *bot.Bot Bot *bot.Bot
Coll *mgo.Collection db *sql.DB
} }
type FirstEntry struct { type FirstEntry struct {
Day time.Time id int64
Time time.Time day time.Time
Body string time time.Time
Nick string body string
nick string
saved bool
}
// Insert or update the first entry
func (fe *FirstEntry) save(db *sql.DB) error {
if _, err := db.Exec(`insert into first (day, time, body, nick)
values (?, ?, ?, ?)`,
fe.day,
fe.time,
fe.body,
fe.nick,
); err != nil {
return err
}
return nil
} }
// NewFirstPlugin creates a new FirstPlugin with the Plugin interface // NewFirstPlugin creates a new FirstPlugin with the Plugin interface
func NewFirstPlugin(b *bot.Bot) *FirstPlugin { func NewFirstPlugin(b *bot.Bot) *FirstPlugin {
// Mongo is removed, this plugin will crash if started if b.DBVersion == 1 {
log.Fatal("The First plugin has not been upgraded to SQL yet.") _, err := b.DB.Exec(`create table if not exists first (
var coll *mgo.Collection id integer primary key,
// coll := b.Db.C("first") day datetime,
var firsts []FirstEntry time datetime,
query := bson.M{"day": midnight(time.Now())} body string,
log.Println("Day:", midnight(time.Now())) nick string
coll.Find(query).All(&firsts) );`)
if err != nil {
log.Fatal("Could not create first table: ", err)
}
}
log.Println("FIRSTS:", firsts) log.Println("First plugin initialized with day:", midnight(time.Now()))
var first *FirstEntry first, err := getLastFirst(b.DB)
if len(firsts) > 0 { if err != nil {
first = &firsts[0] log.Fatal("Could not initialize first plugin: ", err)
} }
return &FirstPlugin{ return &FirstPlugin{
Bot: b, Bot: b,
Coll: coll, db: b.DB,
First: first, First: first,
} }
} }
func getLastFirst(db *sql.DB) (*FirstEntry, error) {
// Get last first entry
var id sql.NullInt64
var day sql.NullInt64
var timeEntered sql.NullInt64
var body sql.NullString
var nick sql.NullString
err := db.QueryRow(`select
id, max(day), time, body, nick from first
limit 1;
`).Scan(
&id,
&day,
&timeEntered,
&body,
&nick,
)
switch {
case err == sql.ErrNoRows || !id.Valid:
log.Println("No previous first entries")
return nil, nil
case err != nil:
return nil, err
}
return &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
}
func midnight(t time.Time) time.Time { func midnight(t time.Time) time.Time {
y, m, d := t.Date() y, m, d := t.Date()
return time.Date(y, m, d, 0, 0, 0, 0, time.UTC) return time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
@ -74,7 +128,7 @@ func (p *FirstPlugin) Message(message bot.Message) bool {
p.recordFirst(message) p.recordFirst(message)
return false return false
} else if p.First != nil { } else if p.First != nil {
if isToday(p.First.Time) && p.allowed(message) { if isToday(p.First.time) && p.allowed(message) {
p.recordFirst(message) p.recordFirst(message)
return false return false
} }
@ -122,12 +176,16 @@ func (p *FirstPlugin) allowed(message bot.Message) bool {
func (p *FirstPlugin) recordFirst(message bot.Message) { func (p *FirstPlugin) recordFirst(message bot.Message) {
log.Println("Recording first: ", message.User.Name, ":", message.Body) log.Println("Recording first: ", message.User.Name, ":", message.Body)
p.First = &FirstEntry{ p.First = &FirstEntry{
Day: midnight(time.Now()), day: midnight(time.Now()),
Time: message.Time, time: message.Time,
Body: message.Body, body: message.Body,
Nick: message.User.Name, nick: message.User.Name,
}
err := p.First.save(p.db)
if err != nil {
log.Println("Error saving first entry: ", err)
return
} }
p.Coll.Insert(p.First)
p.announceFirst(message) p.announceFirst(message)
} }
@ -135,7 +193,7 @@ func (p *FirstPlugin) announceFirst(message bot.Message) {
c := message.Channel c := message.Channel
if p.First != nil { if p.First != nil {
p.Bot.SendMessage(c, fmt.Sprintf("%s had first at %s with the message: \"%s\"", p.Bot.SendMessage(c, fmt.Sprintf("%s had first at %s with the message: \"%s\"",
p.First.Nick, p.First.Time.Format(time.Kitchen), p.First.Body)) p.First.nick, p.First.time.Format(time.Kitchen), p.First.body))
} }
} }