mirror of https://github.com/velour/catbase.git
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:
parent
ce798b120a
commit
ad2e0f13e2
17
bot/bot.go
17
bot/bot.go
|
@ -3,12 +3,13 @@ package bot
|
|||
import irc "github.com/fluffle/goirc/client"
|
||||
import "labix.org/v2/mgo"
|
||||
import "bitbucket.org/phlyingpenguin/godeepintir/config"
|
||||
import "strings"
|
||||
|
||||
// Bot type provides storage for bot-wide information, configs, and database connections
|
||||
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 map[string]Handler
|
||||
|
||||
// Users holds information about all of our friends
|
||||
Users []User
|
||||
|
@ -21,6 +22,8 @@ type Bot struct {
|
|||
// Mongo connection and db allow botwide access to the database
|
||||
DbSession *mgo.Session
|
||||
Db *mgo.Database
|
||||
|
||||
Version string
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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)
|
||||
func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot {
|
||||
// NewBot creates a Bot for a given connection and set of handlers.
|
||||
func NewBot(config *config.Config, c *irc.Conn) *Bot {
|
||||
session, err := mgo.Dial(config.DbServer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -55,15 +57,16 @@ func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot {
|
|||
|
||||
return &Bot{
|
||||
Config: config,
|
||||
Plugins: p,
|
||||
Plugins: make(map[string]Handler),
|
||||
Users: make([]User, 10),
|
||||
Conn: c,
|
||||
DbSession: session,
|
||||
Db: db,
|
||||
Version: config.Version,
|
||||
}
|
||||
}
|
||||
|
||||
// Adds a constructed handler to the bots handlers list
|
||||
func (b *Bot) AddHandler(h Handler) {
|
||||
b.Plugins = append(b.Plugins, h)
|
||||
func (b *Bot) AddHandler(name string, h Handler) {
|
||||
b.Plugins[strings.ToLower(name)] = h
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import irc "github.com/fluffle/goirc/client"
|
|||
// Interface used for compatibility with the Plugin interface
|
||||
type Handler interface {
|
||||
Message(message Message) bool
|
||||
Help(channel string, parts []string)
|
||||
}
|
||||
|
||||
// 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]
|
||||
message := line.Args[1]
|
||||
parts := strings.Fields(strings.ToLower(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)
|
||||
for _, p := range b.Plugins {
|
||||
msg := Message{
|
||||
|
@ -72,3 +99,10 @@ func (b *Bot) Filter(message Message, input string) string {
|
|||
func (b *Bot) SendMessage(channel, message string) {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -13,10 +13,11 @@ type Config struct {
|
|||
MainChannel string
|
||||
Plugins []string
|
||||
Nick, Server, Pass string
|
||||
Version string
|
||||
}
|
||||
|
||||
// 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)
|
||||
file, e := ioutil.ReadFile(cfile)
|
||||
if e != nil {
|
||||
|
@ -28,5 +29,6 @@ func Readconfig(cfile string) *Config {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Version = version
|
||||
return &c
|
||||
}
|
||||
|
|
8
main.go
8
main.go
|
@ -1,11 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"bitbucket.org/phlyingpenguin/godeepintir/bot"
|
||||
"bitbucket.org/phlyingpenguin/godeepintir/config"
|
||||
"bitbucket.org/phlyingpenguin/godeepintir/plugins"
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
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)")
|
||||
flag.Parse() // parses the logging flags.
|
||||
|
||||
config := config.Readconfig(*cfile)
|
||||
config := config.Readconfig(Version, *cfile)
|
||||
fmt.Println(config)
|
||||
|
||||
c := irc.SimpleClient(config.Nick)
|
||||
|
@ -43,7 +43,7 @@ func main() {
|
|||
|
||||
b := bot.NewBot(config, c)
|
||||
// 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) {
|
||||
b.Msg_recieved(conn, line)
|
||||
|
|
|
@ -7,6 +7,7 @@ import "bitbucket.org/phlyingpenguin/godeepintir/bot"
|
|||
type Plugin interface {
|
||||
Message(message bot.Message) bool
|
||||
LoadData()
|
||||
Help()
|
||||
}
|
||||
|
||||
// ---- Below are some example plugins
|
||||
|
@ -25,12 +26,16 @@ type TestPlugin struct {
|
|||
Responds []string
|
||||
Name string
|
||||
Feces string
|
||||
helpmsg []string
|
||||
}
|
||||
|
||||
func (p *TestPlugin) LoadData() {
|
||||
config := GetPluginConfig("TestPlugin")
|
||||
p.Name = config.Name
|
||||
p.Feces = config.Values["Feces"].(string)
|
||||
p.helpmsg = []string{
|
||||
"TestPlugin just shows off how shit works.",
|
||||
}
|
||||
}
|
||||
|
||||
func (p *TestPlugin) Message(message bot.Message) bool {
|
||||
|
@ -44,6 +49,12 @@ func (p *TestPlugin) Message(message bot.Message) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (p *TestPlugin) Help(message bot.Message) {
|
||||
for _, msg := range p.helpmsg {
|
||||
p.Bot.SendMessage(message.Channel, msg)
|
||||
}
|
||||
}
|
||||
|
||||
type PluginConfig struct {
|
||||
Name string
|
||||
Values map[string]interface{}
|
||||
|
|
|
@ -37,3 +37,7 @@ func (p *TalkerPlugin) Message(message bot.Message) bool {
|
|||
func (p *TalkerPlugin) LoadData() {
|
||||
// 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!")
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package main
|
||||
|
||||
const Version = "0.1"
|
Loading…
Reference in New Issue