Fixes #10: Bot can quote and count himself

This commit is contained in:
Chris Sexton 2013-05-07 20:08:18 -04:00
parent 46fa81cd68
commit 42c934f091
15 changed files with 146 additions and 30 deletions

View File

@ -17,6 +17,8 @@ type Bot struct {
// Users holds information about all of our friends
Users []User
// Represents the bot
Me User
// Conn allows us to send messages and modify our connection state
Conn *irc.Conn
@ -129,6 +131,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot {
Plugins: make(map[string]Handler),
PluginOrdering: make([]string, 0),
Users: users,
Me: users[0],
Conn: c,
DbSession: session,
Db: db,
@ -148,9 +151,37 @@ func (b *Bot) AddHandler(name string, h Handler) {
// Sends message to channel
func (b *Bot) SendMessage(channel, message string) {
b.Conn.Privmsg(channel, message)
// Notify plugins that we've said something
b.selfSaid(channel, message)
}
// Sends action to channel
func (b *Bot) SendAction(channel, message string) {
b.Conn.Action(channel, message)
// Notify plugins that we've said something
b.selfSaid(channel, message)
}
// Handles incomming PRIVMSG requests
func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
msg := b.buildMessage(conn, line)
if strings.HasPrefix(msg.Body, "help") && msg.Command {
parts := strings.Fields(strings.ToLower(msg.Body))
b.checkHelp(msg.Channel, parts)
goto RET
}
for _, name := range b.PluginOrdering {
p := b.Plugins[name]
if p.Message(msg) {
break
}
}
RET:
b.logIn <- msg
return
}

View File

@ -16,6 +16,7 @@ import irc "github.com/fluffle/goirc/client"
type Handler interface {
Message(message Message) bool
Event(kind string, message Message) bool
BotMessage(message Message) bool
Help(channel string, parts []string)
}
@ -152,28 +153,6 @@ func (b *Bot) LastMessage() (Message, error) {
return log[len(log)-1], nil
}
// Handles incomming PRIVMSG requests
func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
msg := b.buildMessage(conn, line)
if strings.HasPrefix(msg.Body, "help") && msg.Command {
parts := strings.Fields(strings.ToLower(msg.Body))
b.checkHelp(msg.Channel, parts)
goto RET
}
for _, name := range b.PluginOrdering {
p := b.Plugins[name]
if p.Message(msg) {
break
}
}
RET:
b.logIn <- msg
return
}
// Take an input string and mutate it based on $vars in the string
func (b *Bot) Filter(message Message, input string) string {
rand.Seed(time.Now().Unix())
@ -257,3 +236,23 @@ func (b *Bot) ActionRecieved(conn *irc.Conn, line *irc.Line) {
}
}
}
// Send our own musings to the plugins
func (b *Bot) selfSaid(channel, message string) {
msg := Message{
User: &b.Me, // hack
Channel: channel,
Body: message,
Raw: message, // hack
Command: false,
Time: time.Now(),
Host: "0.0.0.0", // hack
}
for _, name := range b.PluginOrdering {
p := b.Plugins[name]
if p.BotMessage(msg) {
break
}
}
}

View File

@ -1,8 +1,8 @@
package plugins
import (
"github.com/chrissexton/alepale/bot"
"fmt"
"github.com/chrissexton/alepale/bot"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"math/rand"
@ -91,3 +91,8 @@ func (p *AdminPlugin) Help(channel string, parts []string) {
func (p *AdminPlugin) Event(kind string, message bot.Message) bool {
return false
}
// Handler for bot's own messages
func (p *AdminPlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -1,10 +1,10 @@
package plugins
import (
"github.com/chrissexton/alepale/bot"
"encoding/json"
"errors"
"fmt"
"github.com/chrissexton/alepale/bot"
"io/ioutil"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
@ -369,3 +369,8 @@ func (p *BeersPlugin) checkUntappd(channel string) {
}
}
}
// Handler for bot's own messages
func (p *BeersPlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -189,3 +189,8 @@ func (p *CounterPlugin) Help(channel string, parts []string) {
func (p *CounterPlugin) Event(kind string, message bot.Message) bool {
return false
}
// Handler for bot's own messages
func (p *CounterPlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -93,3 +93,8 @@ func (p *DicePlugin) Help(channel string, parts []string) {
func (p *DicePlugin) Event(kind string, message bot.Message) bool {
return false
}
// Handler for bot's own messages
func (p *DicePlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -148,3 +148,8 @@ func (p *DowntimePlugin) Event(kind string, message bot.Message) bool {
}
return false
}
// Handler for bot's own messages
func (p *DowntimePlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -1,8 +1,8 @@
package plugins
import (
"github.com/chrissexton/alepale/bot"
"fmt"
"github.com/chrissexton/alepale/bot"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"math/rand"
@ -437,3 +437,8 @@ func (p *FactoidPlugin) factTimer(channel string) {
}
}
}
// Handler for bot's own messages
func (p *FactoidPlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -1,8 +1,8 @@
package plugins
import (
"github.com/chrissexton/alepale/bot"
"fmt"
"github.com/chrissexton/alepale/bot"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
@ -141,3 +141,8 @@ func (p *FirstPlugin) Help(channel string, parts []string) {
func (p *FirstPlugin) Event(kind string, message bot.Message) bool {
return false
}
// Handler for bot's own messages
func (p *FirstPlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -7,6 +7,7 @@ import "github.com/chrissexton/alepale/bot"
type Plugin interface {
Message(message bot.Message) bool
Event(kind string, message bot.Message) bool
BotMessage(message bot.Message) bool
LoadData()
Help()
}
@ -61,6 +62,11 @@ func (p *TestPlugin) Event(kind string, message bot.Message) bool {
return false
}
// Handler for bot's own messages
func (p *TestPlugin) BotMessage(message bot.Message) bool {
return false
}
type PluginConfig struct {
Name string
Values map[string]interface{}
@ -94,3 +100,8 @@ func (fp FalsePlugin) LoadData() {
func (p *FalsePlugin) Event(kind string, message bot.Message) bool {
return false
}
// Handler for bot's own messages
func (p *FalsePlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -5,6 +5,7 @@ import (
"github.com/chrissexton/alepale/bot"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
"math/rand"
"strings"
"time"
@ -37,7 +38,6 @@ func NewRememberPlugin(b *bot.Bot) *RememberPlugin {
// users message. Otherwise, the function returns false and the bot continues
// execution of other plugins.
func (p *RememberPlugin) Message(message bot.Message) bool {
// This bot does not reply to anything
if message.Body == "quote" && message.Command {
q := p.randQuote()
@ -58,7 +58,11 @@ func (p *RememberPlugin) Message(message bot.Message) bool {
for i := len(p.Log[message.Channel]) - 1; i >= 0; i-- {
entry := p.Log[message.Channel][i]
if entry.User.Name == nick && strings.Contains(entry.Body, snip) {
if strings.ToLower(entry.User.Name) == strings.ToLower(nick) &&
strings.Contains(
strings.ToLower(entry.Body),
strings.ToLower(snip),
) {
// insert new remember entry
var msg string
@ -171,3 +175,19 @@ func (p *RememberPlugin) quoteTimer(channel string) {
func (p *RememberPlugin) Event(kind string, message bot.Message) bool {
return false
}
// Record what the bot says in the log
func (p *RememberPlugin) BotMessage(message bot.Message) bool {
log.Printf("Recieved: %+v\n", message)
p.Log[message.Channel] = append(p.Log[message.Channel], message)
for ch, _ := range p.Log {
log.Printf("Channel: %+v\n", ch)
for _, msg := range p.Log[ch] {
log.Println(msg)
}
}
log.Printf("Log:\n%+v\n", p.Log)
return false
}

View File

@ -39,3 +39,8 @@ func (p *SkeletonPlugin) Help(channel string, parts []string) {
func (p *SkeletonPlugin) Event(kind string, message bot.Message) bool {
return false
}
// Handler for bot's own messages
func (p *SkeletonPlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -1,8 +1,8 @@
package plugins
import (
"github.com/chrissexton/alepale/bot"
"fmt"
"github.com/chrissexton/alepale/bot"
"math/rand"
"strings"
"time"
@ -105,3 +105,8 @@ func (p *TalkerPlugin) Event(kind string, message bot.Message) bool {
}
return false
}
// Handler for bot's own messages
func (p *TalkerPlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -1,8 +1,8 @@
package plugins
import (
"github.com/chrissexton/alepale/bot"
"bitbucket.org/phlyingpenguin/twitter"
"github.com/chrissexton/alepale/bot"
"github.com/garyburd/go-oauth/oauth"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
@ -129,3 +129,8 @@ func (p *TwitterPlugin) checkMessages() {
}
}
}
// Handler for bot's own messages
func (p *TwitterPlugin) BotMessage(message bot.Message) bool {
return false
}

View File

@ -52,3 +52,8 @@ func (p *YourPlugin) Help(channel string, parts []string) {
func (p *YourPlugin) Event(kind string, message bot.Message) bool {
return false
}
// Handler for bot's own messages
func (p *YourPlugin) BotMessage(message bot.Message) bool {
return false
}