From 3953f0a8318c93ecc64a5512c5237384d1a863e9 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Thu, 23 Aug 2012 16:28:45 -0400 Subject: [PATCH] Massive improvements to the remember plugin, fixed some help issues, added ACTION parsing. --- bot/bot.go | 2 ++ bot/handlers.go | 29 +++++++++++++++++--------- config.json | 1 + config/config.go | 1 + main.go | 4 ++++ plugins/remember.go | 51 +++++++++++++++++++++++++++++++-------------- 6 files changed, 62 insertions(+), 26 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 057ab37..d484eee 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -44,7 +44,9 @@ type User struct { type Message struct { User *User Channel, Body string + Raw string Command bool + Action bool } // NewBot creates a Bot for a given connection and set of handlers. diff --git a/bot/handlers.go b/bot/handlers.go index a877f0d..ad60d5d 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -88,16 +88,23 @@ func (b *Bot) isCmd(message string) (bool, string) { // Handles incomming PRIVMSG requests func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { // Check for the user + user := b.checkuser(line.Nick) channel := line.Args[0] if channel == conn.Me.Nick { channel = line.Nick } + + isaction := line.Cmd == "ACTION" + message := line.Args[1] iscmd := false - iscmd, message = b.isCmd(message) - parts := strings.Fields(strings.ToLower(message)) + filteredMessage := message + if !isaction { + iscmd, filteredMessage = b.isCmd(message) + } + parts := strings.Fields(strings.ToLower(filteredMessage)) if iscmd { fmt.Println("Hey, I got a command!") @@ -105,19 +112,21 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { user.MessageLog = append(user.MessageLog, message) - if strings.HasPrefix(message, "help") { + if strings.HasPrefix(filteredMessage, "help") && iscmd{ b.checkHelp(channel, parts) return } - fmt.Printf("In %s, %s said: '%s'\n", channel, line.Nick, message) + msg := Message{ + User: user, + Channel: channel, + Body: filteredMessage, + Raw: message, + Command: iscmd, + Action: isaction, + } + fmt.Printf("%#v\n", msg) for _, p := range b.Plugins { - msg := Message{ - User: user, - Channel: channel, - Body: message, - Command: iscmd, - } if p.Message(msg) { break } diff --git a/config.json b/config.json index 4c87bed..81cd8a2 100644 --- a/config.json +++ b/config.json @@ -9,6 +9,7 @@ "Pass": "AlePaleTest:test", "CommandChar": "!", "QuoteChance": 0.10, + "QuoteTime": 30, "LogLength": 50, "comment": "Follows is the old bot", diff --git a/config/config.go b/config/config.go index ff26c64..7b70454 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ type Config struct { Version string CommandChar string QuoteChance float64 + QuoteTime int LogLength int } diff --git a/main.go b/main.go index c773081..a0535a8 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,10 @@ func main() { b.AddHandler("beers", plugins.NewBeersPlugin(b)) b.AddHandler("remember", plugins.NewRememberPlugin(b)) + c.AddHandler("ACTION", func(conn *irc.Conn, line *irc.Line) { + b.MsgRecieved(conn, line) + }) + c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) { b.MsgRecieved(conn, line) }) diff --git a/plugins/remember.go b/plugins/remember.go index 9f8dd27..76aed55 100644 --- a/plugins/remember.go +++ b/plugins/remember.go @@ -46,37 +46,56 @@ func (p *RememberPlugin) Message(message bot.Message) bool { if message.Body == "quote" && message.Command { q := p.randQuote() p.Bot.SendMessage(message.Channel, q) + + // is it evil not to remember that the user said quote? return true } parts := strings.Fields(message.Body) - if len(parts) < 3 || parts[0] != "remember" { - p.Log[message.Channel] = append(p.Log[message.Channel], message) - return false - } else { + if message.Command && len(parts) >= 3 && parts[0] == "remember" { // we have a remember! // look through the logs and find parts[1] as a user, if not, fuck this hoser nick := parts[1] snip := strings.Join(parts[2:], " ") - for _, entry := range p.Log[message.Channel] { + + if nick == message.User.Name { + msg := fmt.Sprintf("Don't try to quote yourself, %s.", nick) + p.Bot.SendMessage(message.Channel, msg) + return true + } + + for i := len(p.Log[message.Channel])-1; i >= 0; i-- { + entry := p.Log[message.Channel][i] // find the entry we want + fmt.Printf("Comparing '%s' to '%s'\n", entry.Raw, snip) if entry.User.Name == nick && strings.Contains(entry.Body, snip) { // insert new remember entry + var msg string + + // check if it's an action + if entry.Action { + msg = fmt.Sprintf("*%s* %s", entry.User.Name, entry.Raw) + } else { + msg = fmt.Sprintf("<%s> %s", entry.User.Name, entry.Raw) + } u := userRemember{ Nick: entry.User.Name, - Message: fmt.Sprintf("<%s> %s", entry.User.Name, entry.Body), + Message: msg, Date: time.Now(), } p.Coll.Insert(u) - msg := fmt.Sprintf("Okay, %s, remembering '<%s> %s'.", - message.User.Name, entry.User.Name, entry.Body) + + // sorry, not creative with names so we're reusing msg + msg = fmt.Sprintf("Okay, %s, remembering '%s'.", + message.User.Name, msg) p.Bot.SendMessage(message.Channel, msg) + p.Log[message.Channel] = append(p.Log[message.Channel], message) return true } } p.Bot.SendMessage(message.Channel, "Sorry, I don't know that phrase.") - return true } + p.Log[message.Channel] = append(p.Log[message.Channel], message) return false } @@ -85,9 +104,6 @@ func (p *RememberPlugin) Message(message bot.Message) bool { // date. func (p *RememberPlugin) LoadData() { p.Coll = p.Bot.Db.C("remember") - if p.Coll == nil { - panic("FUCK ME") - } rand.Seed(time.Now().Unix()) } @@ -113,9 +129,6 @@ func (p *RememberPlugin) record(nick, msg string) { // to have this function execute a quote for a particular channel func (p *RememberPlugin) randQuote() string { var quotes []userRemember - if p.Coll == nil { - panic("FUCK ME HARD") - } iter := p.Coll.Find(bson.M{}).Iter() err := iter.All("es) if err != nil { @@ -124,16 +137,22 @@ func (p *RememberPlugin) randQuote() string { // rand quote idx nquotes := len(quotes) + if nquotes == 0 { + return "Sorry, I don't know any quotes." + } quote := quotes[rand.Intn(nquotes)] return quote.Message } func (p *RememberPlugin) quoteTimer(channel string) { for { - time.Sleep(30 * time.Minute) + // this pisses me off: You can't multiply int * time.Duration so it + // has to look ugly as shit. + time.Sleep(time.Duration(p.Bot.Config.QuoteTime) * time.Minute) chance := 1.0 / p.Bot.Config.QuoteChance if rand.Intn(int(chance)) == 0 { msg := p.randQuote() + fmt.Println("Delivering quote.") p.Bot.SendMessage(channel, msg) } }