2012-08-17 23:22:37 +00:00
|
|
|
package plugins
|
|
|
|
|
2012-08-18 01:39:26 +00:00
|
|
|
import (
|
|
|
|
"bitbucket.org/phlyingpenguin/godeepintir/bot"
|
|
|
|
"fmt"
|
2012-08-18 02:30:04 +00:00
|
|
|
"labix.org/v2/mgo"
|
|
|
|
"labix.org/v2/mgo/bson"
|
2012-08-18 01:39:26 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2012-08-25 01:52:15 +00:00
|
|
|
"math/rand"
|
2012-08-18 01:39:26 +00:00
|
|
|
)
|
2012-08-17 23:22:37 +00:00
|
|
|
|
|
|
|
// This is a skeleton plugin to serve as an example and quick copy/paste for new plugins.
|
|
|
|
|
|
|
|
type BeersPlugin struct {
|
2012-08-18 02:30:04 +00:00
|
|
|
Bot *bot.Bot
|
|
|
|
Coll *mgo.Collection
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBeersPlugin creates a new BeersPlugin with the Plugin interface
|
|
|
|
func NewBeersPlugin(bot *bot.Bot) *BeersPlugin {
|
2012-08-18 02:30:04 +00:00
|
|
|
p := BeersPlugin{
|
2012-08-17 23:22:37 +00:00
|
|
|
Bot: bot,
|
|
|
|
}
|
2012-08-18 02:30:04 +00:00
|
|
|
p.LoadData()
|
|
|
|
return &p
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 02:30:04 +00:00
|
|
|
type userBeers struct {
|
2012-08-18 01:39:26 +00:00
|
|
|
Nick string
|
2012-08-18 02:30:04 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserBeers(coll *mgo.Collection, nick string) *userBeers {
|
|
|
|
ub := userBeers{New: true}
|
2012-08-25 01:43:53 +00:00
|
|
|
coll.Find(bson.M{"nick": nick}).One(&ub)
|
2012-08-18 02:30:04 +00:00
|
|
|
if ub.New == true {
|
|
|
|
ub.New = false
|
2012-08-25 01:43:53 +00:00
|
|
|
ub.Nick = nick
|
|
|
|
ub.Beercount = 0
|
|
|
|
ub.Momentum = 0
|
2012-08-18 02:30:04 +00:00
|
|
|
ub.Save(coll)
|
|
|
|
}
|
|
|
|
return &ub
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 23:22:37 +00:00
|
|
|
// Message responds to the bot hook on recieving messages.
|
|
|
|
// This function returns true if the plugin responds in a meaningful way to the users message.
|
|
|
|
// Otherwise, the function returns false and the bot continues execution of other plugins.
|
|
|
|
func (p *BeersPlugin) Message(message bot.Message) bool {
|
2012-08-18 01:39:26 +00:00
|
|
|
parts := strings.Fields(message.Body)
|
|
|
|
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
channel := message.Channel
|
|
|
|
user := message.User
|
|
|
|
nick := user.Name
|
|
|
|
|
|
|
|
// respond to the beers type of queries
|
|
|
|
if parts[0] == "beers" {
|
|
|
|
if len(parts) == 3 {
|
|
|
|
|
|
|
|
// try to get a count out of parts[2]
|
|
|
|
count, err := strconv.Atoi(parts[2])
|
|
|
|
if err != nil {
|
|
|
|
// if it's not a number, maybe it's a nick!
|
2012-08-18 02:53:39 +00:00
|
|
|
p.Bot.SendMessage(channel, "Sorry, that didn't make any sense.")
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if count < 0 {
|
|
|
|
// you can't be negative
|
|
|
|
msg := fmt.Sprintf("Sorry %s, you can't have negative beers!", nick)
|
|
|
|
p.Bot.SendMessage(channel, msg)
|
|
|
|
}
|
|
|
|
if parts[1] == "+=" {
|
2012-08-18 02:30:04 +00:00
|
|
|
p.setBeers(user, p.getBeers(nick)+count)
|
2012-08-25 01:52:15 +00:00
|
|
|
p.randomReply(channel)
|
2012-08-18 01:39:26 +00:00
|
|
|
} else if parts[1] == "=" {
|
|
|
|
if count == 0 {
|
2012-08-18 02:30:04 +00:00
|
|
|
p.puke(user, channel)
|
2012-08-18 01:39:26 +00:00
|
|
|
} else {
|
|
|
|
p.setBeers(user, count)
|
2012-08-25 01:52:15 +00:00
|
|
|
p.randomReply(channel)
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.Bot.SendMessage(channel, "I don't know your math.")
|
|
|
|
}
|
2012-08-18 02:53:39 +00:00
|
|
|
} else if len(parts) == 2 {
|
|
|
|
if p.doIKnow(parts[1]) {
|
|
|
|
p.reportCount(parts[1], channel, false)
|
|
|
|
} else {
|
|
|
|
msg := fmt.Sprintf("Sorry, I don't know %s.", parts[1])
|
|
|
|
p.Bot.SendMessage(channel, msg)
|
|
|
|
}
|
2012-08-18 02:30:04 +00:00
|
|
|
} else if len(parts) == 1 {
|
|
|
|
p.reportCount(nick, channel, true)
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// no matter what, if we're in here, then we've responded
|
|
|
|
return true
|
2012-08-18 02:30:04 +00:00
|
|
|
} else if parts[0] == "beers++" {
|
|
|
|
p.addBeers(user)
|
2012-08-25 01:52:15 +00:00
|
|
|
p.randomReply(channel)
|
2012-08-18 02:30:04 +00:00
|
|
|
return true
|
|
|
|
} else if parts[0] == "puke" {
|
|
|
|
p.puke(user, channel)
|
|
|
|
return true
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if message.Command && parts[0] == "imbibe" {
|
2012-08-18 02:30:04 +00:00
|
|
|
p.addBeers(user)
|
2012-08-25 01:52:15 +00:00
|
|
|
p.randomReply(channel)
|
2012-08-18 01:39:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2012-08-17 23:22:37 +00:00
|
|
|
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 *BeersPlugin) LoadData() {
|
2012-08-18 02:30:04 +00:00
|
|
|
p.Coll = p.Bot.Db.C("beers")
|
2012-08-25 01:52:15 +00:00
|
|
|
rand.Seed(time.Now().Unix())
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
|
|
|
func (p *BeersPlugin) Help(channel string, parts []string) {
|
2012-08-18 03:11:36 +00:00
|
|
|
msg := "Beers: imbibe by using either beers +=,=,++ or with the !imbibe/drink " +
|
|
|
|
"commands. I'll keep a count of how many beers you've had and then if you want " +
|
|
|
|
"to reset, just !puke it all up!"
|
|
|
|
p.Bot.SendMessage(channel, msg)
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
2012-08-18 01:39:26 +00:00
|
|
|
|
|
|
|
func (p *BeersPlugin) setBeers(user *bot.User, amount int) {
|
2012-08-18 02:30:04 +00:00
|
|
|
ub := getUserBeers(p.Coll, user.Name)
|
|
|
|
ub.Beercount = amount
|
2012-08-25 01:43:53 +00:00
|
|
|
ub.Lastdrunk = time.Now()
|
2012-08-18 02:30:04 +00:00
|
|
|
ub.Save(p.Coll)
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BeersPlugin) addBeers(user *bot.User) {
|
2012-08-18 02:30:04 +00:00
|
|
|
p.setBeers(user, p.getBeers(user.Name)+1)
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 02:30:04 +00:00
|
|
|
func (p *BeersPlugin) getBeers(nick string) int {
|
|
|
|
ub := getUserBeers(p.Coll, nick)
|
2012-08-18 01:39:26 +00:00
|
|
|
|
2012-08-18 02:30:04 +00:00
|
|
|
return ub.Beercount
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 02:30:04 +00:00
|
|
|
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 {
|
2012-08-18 02:53:39 +00:00
|
|
|
if beers == 0 {
|
|
|
|
msg = fmt.Sprintf("You really need to get drinkin, %s!", nick)
|
|
|
|
} else {
|
|
|
|
msg = fmt.Sprintf("You've had %d beers so far, %s.", beers, nick)
|
|
|
|
}
|
2012-08-18 02:30:04 +00:00
|
|
|
}
|
|
|
|
p.Bot.SendMessage(channel, msg)
|
|
|
|
fmt.Println("I should have reported a beer count!")
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 02:30:04 +00:00
|
|
|
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)
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 02:30:04 +00:00
|
|
|
func (p *BeersPlugin) doIKnow(nick string) bool {
|
|
|
|
count, err := p.Coll.Find(bson.M{"nick": nick}).Count()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return count > 0
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
2012-08-25 01:52:15 +00:00
|
|
|
|
|
|
|
// Sends random affirmation to the channel. This could be better (with a datastore for sayings)
|
|
|
|
func (p *BeersPlugin) randomReply(channel string) {
|
|
|
|
replies := []string{"ZIGGY! ZAGGY!", "HIC!", "Stay thirsty, my friend!"}
|
|
|
|
p.Bot.SendMessage(channel, replies[rand.Intn(len(replies))])
|
|
|
|
}
|