-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds.go
98 lines (93 loc) · 2.5 KB
/
cmds.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cmds
import (
"errors"
"regexp"
)
var (
ErrCmdEmptyMsg = errors.New("cmd: nil message")
ErrCmdNotFound = errors.New("cmd: no commands to handle message")
ErrCmdNoResp = errors.New("cmd: no resp")
)
//Commands holds the complete command list
var Commands []*Cmd = make([]*Cmd, 0)
//GetCmd returns a cmd that matches the given alias
func GetCmd(match string) *Cmd {
for _, cmd := range Commands {
if match == cmd.Name {
return cmd
}
for _, alias := range cmd.Aliases {
if match == alias {
return cmd
}
}
}
return nil
}
//GetSubCmd returns a subcmd that matches the given alias
func (cmd *Cmd) GetSubCmd(match string) *Cmd {
for _, subCmd := range cmd.Subcommands {
if match == subCmd.Name {
return subCmd
}
for _, alias := range subCmd.Aliases {
if match == alias {
return subCmd
}
}
}
return nil
}
type Cmd struct {
Exec func(*CmdCtx) *CmdResp //Go function to handle command
Name string //Display name for command
Description string //Description for command lists and command usage
Aliases []string //Aliases to refer to command
Regexes []regexp.Regexp //Regular expressions to match command call with natural language ($1 is Args[0], $2 is Args[1], etc)
Args []*CmdArg //Arguments for command
Subcommands []*Cmd //Subcommands for command (nestable, i.e. "/minecraft server mc.hypixel.net" where server is subcommand to minecraft command)
}
func NewCmd(name, desc string, handler func(*CmdCtx) *CmdResp) *Cmd {
return &Cmd{
Name: name,
Description: desc,
Aliases: []string{name},
Exec: handler,
}
}
func (cmd *Cmd) SetHandler(handler func(*CmdCtx) *CmdResp) *Cmd {
cmd.Exec = handler
return cmd
}
func (cmd *Cmd) SetName(name string) *Cmd {
cmd.Name = name
return cmd
}
func (cmd *Cmd) SetDescription(desc string) *Cmd {
cmd.Description = desc
return cmd
}
func (cmd *Cmd) AddAliases(alias ...string) *Cmd {
cmd.Aliases = append(cmd.Aliases, alias...)
return cmd
}
func (cmd *Cmd) AddRegexes(regex ...regexp.Regexp) *Cmd {
cmd.Regexes = append(cmd.Regexes, regex...)
return cmd
}
func (cmd *Cmd) AddArgs(arg ...*CmdArg) *Cmd {
cmd.Args = append(cmd.Args, arg...)
return cmd
}
func (cmd *Cmd) AddSubCmds(subCmd ...*Cmd) *Cmd {
cmd.Subcommands = append(cmd.Subcommands, subCmd...)
return cmd
}
func (cmd *Cmd) IsAlias(alias string) bool {
for i := 0; i < len(cmd.Aliases); i++ {
if cmd.Aliases[i] == alias {
return true
}
}
return false
}