catbase/plugins/plugins.go

110 lines
2.5 KiB
Go
Raw Normal View History

2016-01-17 18:00:44 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package plugins
import "fmt"
2016-01-17 18:00:44 +00:00
import "github.com/velour/catbase/bot"
// Plugin interface defines the methods needed to accept a plugin
type Plugin interface {
Message(message bot.Message) bool
Event(kind string, message bot.Message) bool
BotMessage(message bot.Message) bool
LoadData()
Help()
2013-06-01 17:10:15 +00:00
RegisterWeb()
}
// ---- Below are some example plugins
// Creates a new TestPlugin with the Plugin interface
func NewTestPlugin(bot *bot.Bot) *TestPlugin {
tp := TestPlugin{}
tp.LoadData()
tp.Bot = bot
return &tp
}
// TestPlugin type allows our plugin to store persistent state information
type TestPlugin struct {
Bot *bot.Bot
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 {
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)
p.Bot.SendMessage(channel, body)
return true
}
func (p *TestPlugin) Help(message bot.Message) {
for _, msg := range p.helpmsg {
p.Bot.SendMessage(message.Channel, msg)
}
}
// Empty event handler because this plugin does not do anything on event recv
2012-08-25 04:49:48 +00:00
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{}
}
// Loads plugin config (could be out of a DB or something)
func GetPluginConfig(name string) PluginConfig {
return PluginConfig{
Name: "TestPlugin",
Values: map[string]interface{}{
"Feces": "test",
"Responds": "fucker",
},
}
}
// FalsePlugin shows how plugin fallthrough works for handling messages
type FalsePlugin struct{}
func (fp FalsePlugin) Message(user, message string) bool {
fmt.Println("FalsePlugin returning false.")
return false
}
func (fp FalsePlugin) LoadData() {
}
// Empty event handler because this plugin does not do anything on event recv
2012-08-25 04:49:48 +00:00
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
}