mirror of https://github.com/velour/catbase.git
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
|
package plugins
|
||
|
|
||
|
import "fmt"
|
||
|
import "godeepintir/bot"
|
||
|
|
||
|
// Plugin interface defines the methods needed to accept a plugin
|
||
|
type Plugin interface {
|
||
|
Message(user *bot.User, channel, message string) bool
|
||
|
LoadData()
|
||
|
}
|
||
|
|
||
|
// ---- 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
|
||
|
}
|
||
|
|
||
|
func (p *TestPlugin) LoadData() {
|
||
|
config := GetPluginConfig("TestPlugin")
|
||
|
p.Name = config.Name
|
||
|
p.Feces = config.Values["Feces"].(string)
|
||
|
}
|
||
|
|
||
|
func (p *TestPlugin) Message(user *bot.User, channel, message string) bool {
|
||
|
fmt.Println(user, message)
|
||
|
fmt.Println("My plugin name is:", p.Name, " My feces are:", p.Feces)
|
||
|
p.Bot.SendMessage(channel, message)
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
type PluginConfig struct {
|
||
|
Name string
|
||
|
Values map[string]interface{}
|
||
|
}
|
||
|
|
||
|
// Loads plugin config out of the DB
|
||
|
// Stored in db.plugins.find("name": name)
|
||
|
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() {
|
||
|
|
||
|
}
|