This repository has been archived by the owner on Feb 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
help.go
executable file
·73 lines (56 loc) · 1.47 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
package command
import (
"fmt"
"github.com/bwmarrin/discordgo"
"sort"
"strconv"
)
// Help function provides a build in "help" command that will display a list
// of all registered routes (commands). To use this function it must first be
// registered with the Mux.Route function.
func (m *Mux) Help(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
// Set command prefix to display.
cp := ""
if ctx.IsPrivate {
cp = ""
} else if ctx.HasPrefix {
cp = m.Prefix
} else {
cp = fmt.Sprintf("@%s ", ds.State.User.Username)
}
// Sort commands
maxlen := 0
keys := make([]string, 0, len(m.Routes))
cmdmap := make(map[string]*Route)
for _, v := range m.Routes {
// Only display commands with a description
if v.Description == "" {
continue
}
// Calculate the max length of command+args string
l := len(v.JoinedPatterns) // TODO: Add the +args part :)
if l > maxlen {
maxlen = l
}
cmdmap[v.Pattern] = v
// help and about are added separately below.
if v.Pattern == "help" || v.Pattern == "about" {
continue
}
keys = append(keys, v.Pattern)
}
sort.Strings(keys)
resp := "```yml\n"
v, ok := cmdmap["help"]
if ok {
keys = append([]string{v.Pattern}, keys...)
}
// Add sorted result to help msg
for _, k := range keys {
v := cmdmap[k]
resp += fmt.Sprintf("%s%-"+strconv.Itoa(maxlen)+"s # %s\n", cp, v.JoinedPatterns+v.Help, v.Description)
}
resp += "```\n"
_, _ = ds.ChannelMessageSend(dm.ChannelID, resp)
return
}