/
simpleplugin.go
60 lines (52 loc) · 1.56 KB
/
simpleplugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package bruxism
// SimplePlugin is a simple wrapper around a Plugin that can implement handlers by function reference.
type SimplePlugin struct {
name string
LoadFunc LoadFunc `json:"-"`
SaveFunc SaveFunc `json:"-"`
MessageFunc MessageFunc `json:"-"`
HelpFunc HelpFunc `json:"-"`
StatsFunc StatsFunc `json:"-"`
}
// Name returns the name of the plugin.
func (p *SimplePlugin) Name() string {
return p.name
}
// Load will load plugin state from a byte array.
func (p *SimplePlugin) Load(bot *Bot, service Service, data []byte) error {
if p.LoadFunc != nil {
return p.LoadFunc(bot, service, data)
}
return nil
}
// Save will save plugin state to a byte array.
func (p *SimplePlugin) Save() ([]byte, error) {
if p.SaveFunc != nil {
return p.SaveFunc()
}
return nil, nil
}
// Help returns a list of help strings that are printed when the user requests them.
func (p *SimplePlugin) Help(bot *Bot, service Service, message Message, detailed bool) []string {
if p.HelpFunc != nil {
return p.HelpFunc(bot, service, message, detailed)
}
return nil
}
// Message handler.
func (p *SimplePlugin) Message(bot *Bot, service Service, message Message) {
defer MessageRecover()
if p.MessageFunc != nil {
p.MessageFunc(bot, service, message)
}
}
func (p *SimplePlugin) Stats(bot *Bot, service Service, message Message) []string {
if p.StatsFunc != nil {
return p.StatsFunc(bot, service, message)
}
return nil
}
// NewSimplePlugin creates a new simple plugin.
func NewSimplePlugin(name string) *SimplePlugin {
return &SimplePlugin{name: name}
}