Added help command as a general builtin and created some record of the version

we're running. Help text refers to this repository.
This commit is contained in:
Chris Sexton 2012-08-17 18:56:44 -04:00
parent ce798b120a
commit ad2e0f13e2
7 changed files with 69 additions and 12 deletions

View File

@ -3,12 +3,13 @@ package bot
import irc "github.com/fluffle/goirc/client" import irc "github.com/fluffle/goirc/client"
import "labix.org/v2/mgo" import "labix.org/v2/mgo"
import "bitbucket.org/phlyingpenguin/godeepintir/config" import "bitbucket.org/phlyingpenguin/godeepintir/config"
import "strings"
// 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 // 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 // will allow plugins to respond to specific kinds of events
Plugins []Handler Plugins map[string]Handler
// Users holds information about all of our friends // Users holds information about all of our friends
Users []User Users []User
@ -21,6 +22,8 @@ type Bot struct {
// Mongo connection and db allow botwide access to the database // Mongo connection and db allow botwide access to the database
DbSession *mgo.Session DbSession *mgo.Session
Db *mgo.Database Db *mgo.Database
Version string
} }
// User type stores user history. This is a vehicle that will follow the user for the active // User type stores user history. This is a vehicle that will follow the user for the active
@ -43,9 +46,8 @@ type Message struct {
Channel, Body string 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.
// require the bot as input for their creation (so use AddHandler instead to add handlers) func NewBot(config *config.Config, c *irc.Conn) *Bot {
func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot {
session, err := mgo.Dial(config.DbServer) session, err := mgo.Dial(config.DbServer)
if err != nil { if err != nil {
panic(err) panic(err)
@ -55,15 +57,16 @@ func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot {
return &Bot{ return &Bot{
Config: config, Config: config,
Plugins: p, Plugins: make(map[string]Handler),
Users: make([]User, 10), Users: make([]User, 10),
Conn: c, Conn: c,
DbSession: session, DbSession: session,
Db: db, Db: db,
Version: config.Version,
} }
} }
// Adds a constructed handler to the bots handlers list // Adds a constructed handler to the bots handlers list
func (b *Bot) AddHandler(h Handler) { func (b *Bot) AddHandler(name string, h Handler) {
b.Plugins = append(b.Plugins, h) b.Plugins[strings.ToLower(name)] = h
} }

View File

@ -9,6 +9,7 @@ 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(message Message) bool Message(message Message) bool
Help(channel string, parts []string)
} }
// 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
@ -40,9 +41,35 @@ func (b *Bot) Msg_recieved(conn *irc.Conn, line *irc.Line) {
channel := line.Args[0] channel := line.Args[0]
message := line.Args[1] message := line.Args[1]
parts := strings.Fields(strings.ToLower(message))
user.MessageLog = append(user.MessageLog, message) user.MessageLog = append(user.MessageLog, message)
if len(parts) > 0 && parts[0] == "help" {
if len(parts) == 1 {
// just print out a list of help topics
topics := "Help topics: About"
for name, _ := range b.Plugins {
topics = fmt.Sprintf("%s, %s ", topics, name)
}
b.SendMessage(channel, topics)
} else {
// trigger the proper plugin's help response
if parts[1] == "about" {
b.Help(channel, parts)
return
}
plugin := b.Plugins[parts[1]]
if plugin != nil {
plugin.Help(channel, parts)
} else {
msg := fmt.Sprintf("I'm sorry, I don't know what %s is!", parts[1])
b.SendMessage(channel, msg)
}
}
return
}
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 {
msg := Message{ msg := Message{
@ -72,3 +99,10 @@ func (b *Bot) Filter(message Message, input string) string {
func (b *Bot) SendMessage(channel, message string) { func (b *Bot) SendMessage(channel, message string) {
b.Conn.Privmsg(channel, message) b.Conn.Privmsg(channel, message)
} }
func (b *Bot) Help(channel string, parts []string) {
msg := fmt.Sprintf("Hi, I'm based on godeepintir version %s. I'm written in Go, and you "+
"can find my source code on the internet here: "+
"http://bitbucket.org/phlyingpenguin/godeepintir", b.Version)
b.SendMessage(channel, msg)
}

View File

@ -13,10 +13,11 @@ type Config struct {
MainChannel string MainChannel string
Plugins []string Plugins []string
Nick, Server, Pass string Nick, Server, Pass string
Version string
} }
// Readconfig loads the config data out of a JSON file located in cfile // Readconfig loads the config data out of a JSON file located in cfile
func Readconfig(cfile string) *Config { func Readconfig(version, cfile string) *Config {
fmt.Printf("Using %s as config file.\n", cfile) fmt.Printf("Using %s as config file.\n", cfile)
file, e := ioutil.ReadFile(cfile) file, e := ioutil.ReadFile(cfile)
if e != nil { if e != nil {
@ -28,5 +29,6 @@ func Readconfig(cfile string) *Config {
if err != nil { if err != nil {
panic(err) panic(err)
} }
c.Version = version
return &c return &c
} }

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"flag"
"fmt"
"bitbucket.org/phlyingpenguin/godeepintir/bot" "bitbucket.org/phlyingpenguin/godeepintir/bot"
"bitbucket.org/phlyingpenguin/godeepintir/config" "bitbucket.org/phlyingpenguin/godeepintir/config"
"bitbucket.org/phlyingpenguin/godeepintir/plugins" "bitbucket.org/phlyingpenguin/godeepintir/plugins"
"flag"
"fmt"
) )
import irc "github.com/fluffle/goirc/client" import irc "github.com/fluffle/goirc/client"
@ -20,7 +20,7 @@ func main() {
var cfile = flag.String("config", "config.json", "Config file to load. (Defaults to config.json)") var cfile = flag.String("config", "config.json", "Config file to load. (Defaults to config.json)")
flag.Parse() // parses the logging flags. flag.Parse() // parses the logging flags.
config := config.Readconfig(*cfile) config := config.Readconfig(Version, *cfile)
fmt.Println(config) fmt.Println(config)
c := irc.SimpleClient(config.Nick) c := irc.SimpleClient(config.Nick)
@ -43,7 +43,7 @@ func main() {
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)) b.AddHandler("Talker", 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

@ -7,6 +7,7 @@ import "bitbucket.org/phlyingpenguin/godeepintir/bot"
type Plugin interface { type Plugin interface {
Message(message bot.Message) bool Message(message bot.Message) bool
LoadData() LoadData()
Help()
} }
// ---- Below are some example plugins // ---- Below are some example plugins
@ -25,12 +26,16 @@ type TestPlugin struct {
Responds []string Responds []string
Name string Name string
Feces string Feces string
helpmsg []string
} }
func (p *TestPlugin) LoadData() { func (p *TestPlugin) LoadData() {
config := GetPluginConfig("TestPlugin") config := GetPluginConfig("TestPlugin")
p.Name = config.Name p.Name = config.Name
p.Feces = config.Values["Feces"].(string) p.Feces = config.Values["Feces"].(string)
p.helpmsg = []string{
"TestPlugin just shows off how shit works.",
}
} }
func (p *TestPlugin) Message(message bot.Message) bool { func (p *TestPlugin) Message(message bot.Message) bool {
@ -44,6 +49,12 @@ func (p *TestPlugin) Message(message bot.Message) bool {
return true return true
} }
func (p *TestPlugin) Help(message bot.Message) {
for _, msg := range p.helpmsg {
p.Bot.SendMessage(message.Channel, msg)
}
}
type PluginConfig struct { type PluginConfig struct {
Name string Name string
Values map[string]interface{} Values map[string]interface{}

View File

@ -37,3 +37,7 @@ func (p *TalkerPlugin) Message(message bot.Message) bool {
func (p *TalkerPlugin) LoadData() { func (p *TalkerPlugin) LoadData() {
// no data to load yet? // no data to load yet?
} }
func (p *TalkerPlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Hi, this is talker. I like to talk about FredFelps!")
}

3
version.go Normal file
View File

@ -0,0 +1,3 @@
package main
const Version = "0.1"