mirror of https://github.com/velour/catbase.git
Migrate remember to SQL
This commit is contained in:
parent
5b7fef27bb
commit
1d62e6173f
2
main.go
2
main.go
|
@ -63,7 +63,7 @@ func main() {
|
||||||
Bot.AddHandler("dice", plugins.NewDicePlugin(Bot))
|
Bot.AddHandler("dice", plugins.NewDicePlugin(Bot))
|
||||||
Bot.AddHandler("beers", plugins.NewBeersPlugin(Bot))
|
Bot.AddHandler("beers", plugins.NewBeersPlugin(Bot))
|
||||||
Bot.AddHandler("counter", plugins.NewCounterPlugin(Bot))
|
Bot.AddHandler("counter", plugins.NewCounterPlugin(Bot))
|
||||||
// Bot.AddHandler("remember", plugins.NewRememberPlugin(Bot))
|
Bot.AddHandler("remember", plugins.NewRememberPlugin(Bot))
|
||||||
Bot.AddHandler("skeleton", plugins.NewSkeletonPlugin(Bot))
|
Bot.AddHandler("skeleton", plugins.NewSkeletonPlugin(Bot))
|
||||||
Bot.AddHandler("your", plugins.NewYourPlugin(Bot))
|
Bot.AddHandler("your", plugins.NewYourPlugin(Bot))
|
||||||
// catches anything left, will always return true
|
// catches anything left, will always return true
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
package plugins
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
@ -10,17 +11,15 @@ 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 skeleton plugin to serve as an example and quick copy/paste for new
|
// This is a skeleton plugin to serve as an example and quick copy/paste for new
|
||||||
// plugins.
|
// plugins.
|
||||||
|
|
||||||
type RememberPlugin struct {
|
type RememberPlugin struct {
|
||||||
Bot *bot.Bot
|
Bot *bot.Bot
|
||||||
Coll *mgo.Collection
|
Log map[string][]bot.Message
|
||||||
Log map[string][]bot.Message
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRememberPlugin creates a new RememberPlugin with the Plugin interface
|
// NewRememberPlugin creates a new RememberPlugin with the Plugin interface
|
||||||
|
@ -28,11 +27,8 @@ func NewRememberPlugin(b *bot.Bot) *RememberPlugin {
|
||||||
p := RememberPlugin{
|
p := RememberPlugin{
|
||||||
Bot: b,
|
Bot: b,
|
||||||
Log: make(map[string][]bot.Message),
|
Log: make(map[string][]bot.Message),
|
||||||
|
db: b.DB,
|
||||||
}
|
}
|
||||||
p.LoadData()
|
|
||||||
// for _, channel := range b.Config.Channels {
|
|
||||||
// go p.quoteTimer(channel)
|
|
||||||
// }
|
|
||||||
return &p
|
return &p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,15 +90,6 @@ func (p *RememberPlugin) Message(message bot.Message) bool {
|
||||||
|
|
||||||
if len(msgs) == len(snips) {
|
if len(msgs) == len(snips) {
|
||||||
msg := strings.Join(msgs, "$and")
|
msg := strings.Join(msgs, "$and")
|
||||||
// Needs to be upgraded to SQL
|
|
||||||
// err := p.Bot.Db.Run(
|
|
||||||
// bson.M{"eval": "return counter(\"factoid\");"},
|
|
||||||
// &funcres,
|
|
||||||
// )
|
|
||||||
|
|
||||||
// if err != nil {
|
|
||||||
// panic(err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
fact := factoid{
|
fact := factoid{
|
||||||
fact: strings.ToLower(trigger),
|
fact: strings.ToLower(trigger),
|
||||||
|
@ -113,8 +100,9 @@ func (p *RememberPlugin) Message(message bot.Message) bool {
|
||||||
accessed: time.Now(),
|
accessed: time.Now(),
|
||||||
count: 0,
|
count: 0,
|
||||||
}
|
}
|
||||||
if err := p.Coll.Insert(fact); err != nil {
|
if err := fact.save(p.db); err != nil {
|
||||||
log.Println("ERROR!!!!:", err)
|
log.Println("ERROR!!!!:", err)
|
||||||
|
p.Bot.SendMessage(message.Channel, "Tell somebody I'm broke.")
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Remembering factoid:", msg)
|
log.Println("Remembering factoid:", msg)
|
||||||
|
@ -135,16 +123,6 @@ func (p *RememberPlugin) Message(message bot.Message) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadData imports any configuration data into the plugin. This is not strictly
|
|
||||||
// necessary other than the fact that the Plugin interface demands it exist.
|
|
||||||
// This may be deprecated at a later date.
|
|
||||||
func (p *RememberPlugin) LoadData() {
|
|
||||||
// Mongo is removed, this plugin will crash if started
|
|
||||||
log.Fatal("The Remember plugin has not been upgraded to SQL yet.")
|
|
||||||
// p.Coll = p.Bot.Db.C("factoid")
|
|
||||||
rand.Seed(time.Now().Unix())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Help responds to help requests. Every plugin must implement a help function.
|
// Help responds to help requests. Every plugin must implement a help function.
|
||||||
func (p *RememberPlugin) Help(channel string, parts []string) {
|
func (p *RememberPlugin) Help(channel string, parts []string) {
|
||||||
|
|
||||||
|
@ -160,26 +138,29 @@ func (p *RememberPlugin) Help(channel string, parts []string) {
|
||||||
// Note: this is the same cache for all channels joined. This plugin needs to be
|
// Note: this is the same cache for all channels joined. This plugin needs to be
|
||||||
// expanded to have this function execute a quote for a particular channel
|
// expanded to have this function execute a quote for a particular channel
|
||||||
func (p *RememberPlugin) randQuote() string {
|
func (p *RememberPlugin) randQuote() string {
|
||||||
var quotes []factoid
|
|
||||||
// todo: find anything with the word "quotes" in the trigger
|
|
||||||
query := bson.M{
|
|
||||||
"trigger": bson.M{
|
|
||||||
"$regex": "quotes$",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
iter := p.Coll.Find(query).Iter()
|
|
||||||
err := iter.All("es)
|
|
||||||
if err != nil {
|
|
||||||
panic(iter.Err())
|
|
||||||
}
|
|
||||||
|
|
||||||
// rand quote idx
|
var f factoid
|
||||||
nquotes := len(quotes)
|
var tmpCreated int64
|
||||||
if nquotes == 0 {
|
var tmpAccessed int64
|
||||||
return "Sorry, I don't know any quotes."
|
err := p.db.QueryRow(`select * from factoid where fact like '%quotes'
|
||||||
|
order by random() limit 1;`).Scan(
|
||||||
|
&f.id,
|
||||||
|
&f.fact,
|
||||||
|
&f.tidbit,
|
||||||
|
&f.verb,
|
||||||
|
&f.owner,
|
||||||
|
&tmpCreated,
|
||||||
|
&tmpAccessed,
|
||||||
|
&f.count,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error getting quotes: ", err)
|
||||||
|
return "I had a problem getting your quote."
|
||||||
}
|
}
|
||||||
quote := quotes[rand.Intn(nquotes)]
|
f.created = time.Unix(tmpCreated, 0)
|
||||||
return quote.tidbit
|
f.accessed = time.Unix(tmpAccessed, 0)
|
||||||
|
|
||||||
|
return f.tidbit
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *RememberPlugin) quoteTimer(channel string) {
|
func (p *RememberPlugin) quoteTimer(channel string) {
|
||||||
|
|
Loading…
Reference in New Issue