/
help.go
100 lines (84 loc) · 2.25 KB
/
help.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
99
100
package core
import (
"sort"
"strings"
"github.com/TeamWhiskey/whiskey/arg"
"github.com/TeamWhiskey/whiskey/cmd"
"github.com/TeamWhiskey/whiskey/util"
)
func init() {
cmd := cmd.New()
cmd.Name("help")
cmd.Aliases("h")
cmd.Description("List of the bot's commands and how to use them")
cmd.Use(help)
cmd.Arg(&arg.Argument{
Name: "cmd",
})
Category.AddCommand(cmd.Command())
}
func help(ctx *cmd.Context, next cmd.NextFunc) {
var helpStrBldr strings.Builder
helpStrBldr.WriteString("**Whiskey (development) help**\n\n")
// TODO(@zorbyte): Dynamic prefix support.
prefix := util.GetConfig().Prefix
// w!help [cmd] <- RawArgs[0]
// get help info on a specific command
if len(ctx.RawArgs) > 0 {
callKeyArg := ctx.Args["cmd"]
if callKeyArg != nil {
callKey := callKeyArg.(string)
reg := cmd.GetRegistry()
cmnd, err := reg.LookupCommand(callKey)
if err != nil {
next(err)
return
}
if cmnd != nil {
buildLookupHelp(&helpStrBldr, prefix, cmnd)
} else {
buildRegularHelp(&helpStrBldr, prefix)
}
}
} else {
buildRegularHelp(&helpStrBldr, prefix)
}
ctx.Send(helpStrBldr.String())
next()
}
func buildLookupHelp(helpStrBldr *strings.Builder, prefix string, cmnd *cmd.Command) {
helpStrBldr.WriteString(cmnd.Category.DisplayName())
helpStrBldr.WriteString("\n\n")
finaliseCmdHelpStr(cmnd.Help(), prefix, helpStrBldr)
}
func buildRegularHelp(helpStrBldr *strings.Builder, prefix string) {
reg := cmd.GetRegistry()
var keys []string
for key := range reg.Categories {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
cat := reg.Categories[key]
helpStrBldr.WriteString(cat.DisplayName())
helpStrBldr.WriteString("\n\n")
var cmdKeys []string
cmdKeysToIdx := make(map[string]int)
for idx, cmnd := range cat.Commands {
cmdKeys = append(cmdKeys, cmnd.Name)
cmdKeysToIdx[cmnd.Name] = idx
}
sort.Strings(cmdKeys)
for _, cmdKey := range cmdKeys {
idx := cmdKeysToIdx[cmdKey]
cmnd := cat.Commands[idx]
finaliseCmdHelpStr(cmnd.Help(), prefix, helpStrBldr)
}
}
}
func finaliseCmdHelpStr(cmdHelpStr string, prefix string, strBldr *strings.Builder) {
strBldr.WriteString("> **`")
strBldr.WriteString(prefix)
strBldr.WriteString(cmdHelpStr)
strBldr.WriteString("\n\n")
}