forked from slack-io/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_group.go
50 lines (41 loc) · 1.51 KB
/
command_group.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
package slacker
import (
"fmt"
"strings"
)
// newGroup creates a new CommandGroup with a prefix
func newGroup(prefix string) *CommandGroup {
return &CommandGroup{prefix: prefix}
}
// CommandGroup groups commands with a common prefix and middlewares
type CommandGroup struct {
prefix string
middlewares []CommandMiddlewareHandler
commands []Command
}
// AddMiddleware define a new middleware and append it to the list of group middlewares
func (g *CommandGroup) AddMiddleware(middleware CommandMiddlewareHandler) {
g.middlewares = append(g.middlewares, middleware)
}
// AddCommand define a new command and append it to the list of group bot commands
func (g *CommandGroup) AddCommand(definition *CommandDefinition) {
definition.Command = strings.TrimSpace(fmt.Sprintf("%s %s", g.prefix, definition.Command))
g.commands = append(g.commands, newCommand(definition))
}
// PrependCommand define a new command and prepend it to the list of group bot commands
func (g *CommandGroup) PrependCommand(definition *CommandDefinition) {
definition.Command = strings.TrimSpace(fmt.Sprintf("%s %s", g.prefix, definition.Command))
g.commands = append([]Command{newCommand(definition)}, g.commands...)
}
// GetPrefix returns the group's prefix
func (g *CommandGroup) GetPrefix() string {
return g.prefix
}
// GetCommands returns Commands
func (g *CommandGroup) GetCommands() []Command {
return g.commands
}
// GetMiddlewares returns Middlewares
func (g *CommandGroup) GetMiddlewares() []CommandMiddlewareHandler {
return g.middlewares
}