-
Notifications
You must be signed in to change notification settings - Fork 14
/
command_group.go
55 lines (45 loc) · 1.71 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
51
52
53
54
55
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))
}
func (g *CommandGroup) AddCustomCommand(cmd Command) {
cmd.Definition().Command = strings.TrimSpace(fmt.Sprintf("%s %s", g.prefix, cmd.Definition().Command))
g.commands = append(g.commands, cmd)
}
// 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
}