This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
84 lines (68 loc) · 2.42 KB
/
plugin.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package drouter
// DefaultPrefix defines the default command prefix of plugins.
var DefaultPrefix = "/"
// Plugin defines the structure of a disgord plugin.
type Plugin struct {
// ImportName defines the import name of the plugin.
ImportName string
// Prefix defines the commands prefix.
Prefix string
// RootCommand defines the base plugin's root/ base command
RootCommand Command
// Listeners defines the different event handlers
// of the plugin (see https://godoc.org/github.com/andersfylling/disgord/event).
Listeners map[string][]interface{}
// Wrappers Contains the registered sub-commands of the plugin.
Commands []*Command
// IsReady is true is the module was loaded and installed into the client.
IsReady bool
}
// Use appends given callbacks to a plugin to call
// whenever a command is being invoked.
func (plugin *Plugin) Use(callbackFuncs ...callbackFunc) *Plugin {
// FIXME: we should put them as global wrappers in Plugin
// instead of the root command.
plugin.RootCommand.Use(callbackFuncs...)
return plugin
}
// SetPrefix sets the plugin commands prefix (can be empty for no prefix).
func (plugin *Plugin) SetPrefix(prefix string) *Plugin {
plugin.Prefix = prefix
return plugin
}
// Handler defines the function to invoke whenever the plugin command
// is being invoked.
func (plugin *Plugin) Handler(callbackFunc callbackFunc) *Plugin {
plugin.RootCommand.Handler(callbackFunc)
return plugin
}
// On registers given handlers to be invoked whenever the event is fired.
func (plugin *Plugin) On(eventName string, inputs ...interface{}) *Plugin {
existingEvents := plugin.Listeners[eventName]
if existingEvents != nil {
plugin.Listeners[eventName] = append(existingEvents, inputs...)
} else {
plugin.Listeners[eventName] = inputs
}
return plugin
}
// Command creates a new sub-command for the plugin.
func (plugin *Plugin) Command(names ...string) *Command {
newCommand := &Command{
Names: NewStringSet(names...),
}
plugin.Commands = append(plugin.Commands, newCommand)
return newCommand
}
// Help sets the help text of a command. The first line is
// the short and straightforward documentation. The whole text
// is the long and descriptive documentation.
func (plugin *Plugin) Help(helpText string) *Plugin {
plugin.RootCommand.Help(helpText)
return plugin
}
// Activate marks a plugin as ready.
func (plugin *Plugin) Activate() {
// TODO: we should dispatch setUp(...)
plugin.IsReady = true
}