From a71e743e79ad9b6449c47184c638943259b59848 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 24 Aug 2012 21:43:53 -0400 Subject: [PATCH] Fixed bugs found on initial unveiling in beers and the handler code. Beers now counts correctly and does not die when it can't find a user. The code for nonexistant users was bogus and the code for setting beers was not counting dates. Handlers were not breaking after finding users (linear scan, ack). This should probably be switched to a map[string]user type. --- bot/bot.go | 2 +- bot/handlers.go | 8 +++----- plugins/beers.go | 9 +++++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index d484eee..8651a3b 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -61,7 +61,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot { return &Bot{ Config: config, Plugins: make(map[string]Handler), - Users: make([]User, 10), + Users: make([]User, 1), Conn: c, DbSession: session, Db: db, diff --git a/bot/handlers.go b/bot/handlers.go index ad60d5d..fe81a48 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -18,7 +18,9 @@ func (b *Bot) checkuser(nick string) *User { var user *User = nil for _, usr := range b.Users { if usr.Name == nick { + fmt.Println("Matched ", nick) user = &usr + break } } if user == nil { @@ -106,10 +108,6 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { } parts := strings.Fields(strings.ToLower(filteredMessage)) - if iscmd { - fmt.Println("Hey, I got a command!") - } - user.MessageLog = append(user.MessageLog, message) if strings.HasPrefix(filteredMessage, "help") && iscmd{ @@ -125,7 +123,7 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { Command: iscmd, Action: isaction, } - fmt.Printf("%#v\n", msg) + fmt.Printf("%#v said %#v\n", user, msg) for _, p := range b.Plugins { if p.Message(msg) { break diff --git a/plugins/beers.go b/plugins/beers.go index bcd97c2..2da0255 100644 --- a/plugins/beers.go +++ b/plugins/beers.go @@ -43,12 +43,12 @@ func (u *userBeers) Save(coll *mgo.Collection) { func getUserBeers(coll *mgo.Collection, nick string) *userBeers { ub := userBeers{New: true} - err := coll.Find(bson.M{"nick": nick}).One(&ub) - if err != nil { - panic(err) - } + coll.Find(bson.M{"nick": nick}).One(&ub) if ub.New == true { ub.New = false + ub.Nick = nick + ub.Beercount = 0 + ub.Momentum = 0 ub.Save(coll) } return &ub @@ -146,6 +146,7 @@ func (p *BeersPlugin) Help(channel string, parts []string) { func (p *BeersPlugin) setBeers(user *bot.User, amount int) { ub := getUserBeers(p.Coll, user.Name) ub.Beercount = amount + ub.Lastdrunk = time.Now() ub.Save(p.Coll) }