It looks like the beers plugin is faithfully copied over to the new bot

framework. I'm not sure that commands vs non-commands are well represented in
this as compared to the old bot.
This commit is contained in:
Chris Sexton 2012-08-17 22:30:04 -04:00
parent 1dafd3af6e
commit 9990decfc7
2 changed files with 78 additions and 24 deletions

View File

@ -36,7 +36,7 @@ func (b *Bot) checkuser(nick string) *User {
// Checks to see if the user is asking for help, returns true if so and handles the situation. // Checks to see if the user is asking for help, returns true if so and handles the situation.
func (b *Bot) checkHelp(channel string, parts []string) { func (b *Bot) checkHelp(channel string, parts []string) {
if len(parts) > 1 { if len(parts) == 1 {
// just print out a list of help topics // just print out a list of help topics
topics := "Help topics: about" topics := "Help topics: about"
for name, _ := range b.Plugins { for name, _ := range b.Plugins {

View File

@ -3,6 +3,8 @@ package plugins
import ( import (
"bitbucket.org/phlyingpenguin/godeepintir/bot" "bitbucket.org/phlyingpenguin/godeepintir/bot"
"fmt" "fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -12,20 +14,44 @@ import (
type BeersPlugin struct { type BeersPlugin struct {
Bot *bot.Bot Bot *bot.Bot
Coll *mgo.Collection
} }
// NewBeersPlugin creates a new BeersPlugin with the Plugin interface // NewBeersPlugin creates a new BeersPlugin with the Plugin interface
func NewBeersPlugin(bot *bot.Bot) *BeersPlugin { func NewBeersPlugin(bot *bot.Bot) *BeersPlugin {
return &BeersPlugin{ p := BeersPlugin{
Bot: bot, Bot: bot,
} }
p.LoadData()
return &p
}
type userBeers struct {
Nick string
Beercount int
Lastdrunk time.Time
Momentum float64
New bool
}
func (u *userBeers) Save(coll *mgo.Collection) {
_, err := coll.Upsert(bson.M{"nick": u.Nick}, u)
if err != nil {
panic(err)
}
} }
type UserBeers struct { func getUserBeers(coll *mgo.Collection, nick string) *userBeers {
Nick string ub := userBeers{New: true}
berrcount int err := coll.Find(bson.M{"nick": nick}).One(&ub)
lastdrunk time.Time if err != nil {
momentum float64 panic(err)
}
if ub.New == true {
ub.New = false
ub.Save(coll)
}
return &ub
} }
// Message responds to the bot hook on recieving messages. // Message responds to the bot hook on recieving messages.
@ -51,10 +77,11 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
if err != nil { if err != nil {
// if it's not a number, maybe it's a nick! // if it's not a number, maybe it's a nick!
if p.doIKnow(parts[2]) { if p.doIKnow(parts[2]) {
p.reportCount(parts[2], false) p.reportCount(parts[2], channel, false)
} else { } else {
msg := fmt.Sprintf("Sorry, I don't know %s.", parts[2]) msg := fmt.Sprintf("Sorry, I don't know %s.", parts[2])
p.Bot.SendMessage(channel, msg) p.Bot.SendMessage(channel, msg)
return true
} }
} }
@ -64,26 +91,36 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
p.Bot.SendMessage(channel, msg) p.Bot.SendMessage(channel, msg)
} }
if parts[1] == "+=" { if parts[1] == "+=" {
p.setBeers(user, p.getBeers(user)+count) p.setBeers(user, p.getBeers(nick)+count)
p.reportCount(nick, channel, true)
} else if parts[1] == "=" { } else if parts[1] == "=" {
if count == 0 { if count == 0 {
p.puke(user) p.puke(user, channel)
} else { } else {
p.setBeers(user, count) p.setBeers(user, count)
p.reportCount(nick, true) p.reportCount(nick, channel, true)
} }
} else { } else {
p.Bot.SendMessage(channel, "I don't know your math.") p.Bot.SendMessage(channel, "I don't know your math.")
} }
} else if len(parts) == 1 {
p.reportCount(nick, channel, true)
} }
// no matter what, if we're in here, then we've responded // no matter what, if we're in here, then we've responded
return true return true
} else if parts[0] == "beers++" {
p.addBeers(user)
p.reportCount(nick, channel, true)
return true
} else if parts[0] == "puke" {
p.puke(user, channel)
return true
} }
if message.Command && parts[0] == "imbibe" { if message.Command && parts[0] == "imbibe" {
p.setBeers(user, p.getBeers(user)+1) p.addBeers(user)
p.reportCount(nick, true) p.reportCount(nick, channel, true)
return true return true
} }
@ -94,7 +131,7 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
// than the fact that the Plugin interface demands it exist. This may be deprecated at a later // than the fact that the Plugin interface demands it exist. This may be deprecated at a later
// date. // date.
func (p *BeersPlugin) LoadData() { func (p *BeersPlugin) LoadData() {
// This bot has no data to load p.Coll = p.Bot.Db.C("beers")
} }
// Help responds to help requests. Every plugin must implement a help function. // Help responds to help requests. Every plugin must implement a help function.
@ -103,24 +140,41 @@ func (p *BeersPlugin) Help(channel string, parts []string) {
} }
func (p *BeersPlugin) setBeers(user *bot.User, amount int) { func (p *BeersPlugin) setBeers(user *bot.User, amount int) {
ub := getUserBeers(p.Coll, user.Name)
ub.Beercount = amount
ub.Save(p.Coll)
} }
func (p *BeersPlugin) addBeers(user *bot.User) { func (p *BeersPlugin) addBeers(user *bot.User) {
p.setBeers(user, p.getBeers(user.Name)+1)
} }
func (p *BeersPlugin) getBeers(user *bot.User) int { func (p *BeersPlugin) getBeers(nick string) int {
return 0 ub := getUserBeers(p.Coll, nick)
return ub.Beercount
} }
func (p *BeersPlugin) hasBeers(user *bot.User) { func (p *BeersPlugin) reportCount(nick, channel string, himself bool) {
beers := p.getBeers(nick)
msg := fmt.Sprintf("%s has had %d beers so far.", nick, beers)
if himself {
msg = fmt.Sprintf("You've had %d beers so far, %s.", beers, nick)
}
p.Bot.SendMessage(channel, msg)
fmt.Println("I should have reported a beer count!")
} }
func (p *BeersPlugin) reportCount(user string, himself bool) { func (p *BeersPlugin) puke(user *bot.User, channel string) {
p.setBeers(user, 0)
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user.Name)
p.Bot.SendMessage(channel, msg)
} }
func (p *BeersPlugin) puke(user *bot.User) { func (p *BeersPlugin) doIKnow(nick string) bool {
} count, err := p.Coll.Find(bson.M{"nick": nick}).Count()
if err != nil {
func (p *BeersPlugin) doIKnow(user string) bool { panic(err)
return false }
return count > 0
} }