Added the felpsbot talker which exhibits the formatter a little bit. This is a

function that will be used to transform strings that include $variables into
strings that include the original meaning. This eventually needs to be some kind
of configurable thing so that new $vars can be added dynamically, however some
of the $vars are static and need not change.
This commit is contained in:
Chris Sexton 2012-08-17 18:09:29 -04:00
parent e1c841ecae
commit f31362fca4
7 changed files with 85 additions and 11 deletions

View File

@ -6,10 +6,19 @@ import "godeepintir/config"
// Bot type provides storage for bot-wide information, configs, and database connections // Bot type provides storage for bot-wide information, configs, and database connections
type Bot struct { type Bot struct {
// Each plugin must be registered in our plugins handler. To come: a map so that this
// will allow plugins to respond to specific kinds of events
Plugins []Handler Plugins []Handler
// Users holds information about all of our friends
Users []User Users []User
// Conn allows us to send messages and modify our connection state
Conn *irc.Conn Conn *irc.Conn
// mongodb connection will go here
Config *config.Config
// Mongo connection and db allow botwide access to the database
DbSession *mgo.Session DbSession *mgo.Session
Db *mgo.Database Db *mgo.Database
} }
@ -29,6 +38,11 @@ type User struct {
MessageLog []string MessageLog []string
} }
type Message struct {
User *User
Channel, Body string
}
// NewBot creates a Bot for a given connection and set of handlers. The handlers must not // NewBot creates a Bot for a given connection and set of handlers. The handlers must not
// require the bot as input for their creation (so use AddHandler instead to add handlers) // require the bot as input for their creation (so use AddHandler instead to add handlers)
func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot { func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot {
@ -40,6 +54,7 @@ func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot {
db := session.DB(config.DbName) db := session.DB(config.DbName)
return &Bot{ return &Bot{
Config: config,
Plugins: p, Plugins: p,
Users: make([]User, 10), Users: make([]User, 10),
Conn: c, Conn: c,

View File

@ -2,12 +2,13 @@ package bot
import ( import (
"fmt" "fmt"
"strings"
) )
import irc "github.com/fluffle/goirc/client" import irc "github.com/fluffle/goirc/client"
// Interface used for compatibility with the Plugin interface // Interface used for compatibility with the Plugin interface
type Handler interface { type Handler interface {
Message(user *User, channel, message string) bool Message(message Message) bool
} }
// Checks to see if our user exists and if any changes have occured to it // Checks to see if our user exists and if any changes have occured to it
@ -44,14 +45,26 @@ func (b *Bot) Msg_recieved(conn *irc.Conn, line *irc.Line) {
fmt.Printf("In %s, %s said: '%s'\n", channel, line.Nick, message) fmt.Printf("In %s, %s said: '%s'\n", channel, line.Nick, message)
for _, p := range b.Plugins { for _, p := range b.Plugins {
if p.Message(user, channel, message) { msg := Message{
User: user,
Channel: channel,
Body: message,
}
if p.Message(msg) {
break break
} }
} }
} }
// Take an input string and mutate it based on $vars in the string // Take an input string and mutate it based on $vars in the string
func (b *Bot) filter(input string) string { func (b *Bot) Filter(message Message, input string) string {
if strings.Contains(input, "$NICK") {
nick := strings.ToUpper(message.User.Name)
input = strings.Replace(input, "$NICK", nick, -1)
} else if strings.Contains(input, "$nick") {
nick := message.User.Name
input = strings.Replace(input, "$nick", nick, -1)
}
return input return input
} }

View File

@ -2,6 +2,7 @@
"Dbname": "deepintir", "Dbname": "deepintir",
"Dbserver": "127.0.0.1", "Dbserver": "127.0.0.1",
"Channels": ["#AlePaleTest"], "Channels": ["#AlePaleTest"],
"MainChannel": "#AlePaleTest",
"Plugins": [], "Plugins": [],
"Server": "127.0.0.1:6666", "Server": "127.0.0.1:6666",
"Nick": "AlePaleTest", "Nick": "AlePaleTest",

View File

@ -10,6 +10,7 @@ type Config struct {
DbName string DbName string
DbServer string DbServer string
Channels []string Channels []string
MainChannel string
Plugins []string Plugins []string
Nick, Server, Pass string Nick, Server, Pass string
} }

View File

@ -42,7 +42,8 @@ func main() {
func(conn *irc.Conn, line *irc.Line) { quit <- true }) func(conn *irc.Conn, line *irc.Line) { quit <- true })
b := bot.NewBot(config, c) b := bot.NewBot(config, c)
b.AddHandler(plugins.NewTestPlugin(b)) // b.AddHandler(plugins.NewTestPlugin(b))
b.AddHandler(plugins.NewTalkerPlugin(b))
c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) { c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) {
b.Msg_recieved(conn, line) b.Msg_recieved(conn, line)

View File

@ -5,7 +5,7 @@ import "godeepintir/bot"
// Plugin interface defines the methods needed to accept a plugin // Plugin interface defines the methods needed to accept a plugin
type Plugin interface { type Plugin interface {
Message(user *bot.User, channel, message string) bool Message(message bot.Message) bool
LoadData() LoadData()
} }
@ -33,10 +33,14 @@ func (p *TestPlugin) LoadData() {
p.Feces = config.Values["Feces"].(string) p.Feces = config.Values["Feces"].(string)
} }
func (p *TestPlugin) Message(user *bot.User, channel, message string) bool { func (p *TestPlugin) Message(message bot.Message) bool {
fmt.Println(user, message) user := message.User
channel := message.Channel
body := message.Body
fmt.Println(user, body)
fmt.Println("My plugin name is:", p.Name, " My feces are:", p.Feces) fmt.Println("My plugin name is:", p.Name, " My feces are:", p.Feces)
p.Bot.SendMessage(channel, message) p.Bot.SendMessage(channel, body)
return true return true
} }

39
plugins/talker.go Normal file
View File

@ -0,0 +1,39 @@
package plugins
import (
"godeepintir/bot"
"strings"
)
type TalkerPlugin struct {
Bot *bot.Bot
}
func NewTalkerPlugin(bot *bot.Bot) *TalkerPlugin {
return &TalkerPlugin{
Bot: bot,
}
}
func (p *TalkerPlugin) Message(message bot.Message) bool {
channel := message.Channel
body := message.Body
if channel != p.Bot.Config.MainChannel {
return false
}
lowermessage := strings.ToLower(body)
if strings.Contains(lowermessage, "felps") || strings.Contains(lowermessage, "fredfelps") {
outmsg := p.Bot.Filter(message, "GOD HATES $NICK")
p.Bot.SendMessage(channel, outmsg)
return true
}
return false
}
func (p *TalkerPlugin) LoadData() {
// no data to load yet?
}