/
commands.go
66 lines (56 loc) · 1.17 KB
/
commands.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
package scmd
import (
"fmt"
"sort"
"strconv"
"strings"
)
func getNewHelp(name string) *Cmd {
cmd := &Cmd{
name: name,
label: "help",
explain: []string{"show usage."},
run: func(c *Context) {
c.SendMessage(c.GetHelpMes(name))
},
}
return cmd
}
func (c *Context) GetHelpMes(name string) string {
commands := c.bot.Cmds[name]
labels := make([]string, 0, len(commands))
for label := range commands {
labels = append(labels, label)
}
sort.Strings(labels)
maxLen := 0
for _, u := range commands {
if len(u.label) > maxLen {
maxLen = len(u.label)
}
}
mes := fmt.Sprintf("```\n%s", name)
cmdSpace := strings.Repeat(" ", len(name))
labelSpace := strings.Repeat(" ", maxLen)
flag := true
form1 := " %-" + strconv.Itoa(maxLen) + "s : %s"
form2 := "\n%s" + form1
form3 := "\n%s %s %s"
for _, label := range labels {
u := commands[label]
for i, exp := range u.explain {
if flag {
mes += fmt.Sprintf(form1, u.label, exp)
flag = false
continue
}
if i == 0 {
mes += fmt.Sprintf(form2, cmdSpace, u.label, exp)
} else {
mes += fmt.Sprintf(form3, cmdSpace, labelSpace, exp)
}
}
}
mes += "\n```"
return mes
}