-
Notifications
You must be signed in to change notification settings - Fork 119
/
command.go
78 lines (65 loc) · 1.92 KB
/
command.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
package slacker
import (
"github.com/shomali11/commander"
"github.com/shomali11/proper"
)
// CommandDefinition structure contains definition of the bot command
type CommandDefinition struct {
Command string
Aliases []string
Description string
Examples []string
Middlewares []CommandMiddlewareHandler
Handler CommandHandler
// HideHelp will hide this command definition from appearing in the `help` results.
HideHelp bool
}
// newCommand creates a new bot command object
func newCommand(definition *CommandDefinition) Command {
cmdAliases := make([]*commander.Command, 0)
for _, alias := range definition.Aliases {
cmdAliases = append(cmdAliases, commander.NewCommand(alias))
}
return &command{
definition: definition,
cmd: commander.NewCommand(definition.Command),
cmdAliases: cmdAliases,
}
}
// Command interface
type Command interface {
Definition() *CommandDefinition
Match(string) (*proper.Properties, bool)
Tokenize() []*commander.Token
}
// command structure contains the bot's command, description and handler
type command struct {
definition *CommandDefinition
cmd *commander.Command
cmdAliases []*commander.Command
}
// Definition returns the command definition
func (c *command) Definition() *CommandDefinition {
return c.definition
}
// Match determines whether the bot should respond based on the text received
func (c *command) Match(text string) (*proper.Properties, bool) {
properties, isMatch := c.cmd.Match(text)
if isMatch {
return properties, isMatch
}
allCommands := make([]*commander.Command, 0)
allCommands = append(allCommands, c.cmd)
allCommands = append(allCommands, c.cmdAliases...)
for _, cmd := range allCommands {
properties, isMatch := cmd.Match(text)
if isMatch {
return properties, isMatch
}
}
return nil, false
}
// Tokenize returns the command format's tokens
func (c *command) Tokenize() []*commander.Token {
return c.cmd.Tokenize()
}