/
cmdList.go
77 lines (65 loc) · 1.55 KB
/
cmdList.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
package static
import (
"fmt"
"strings"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/starshine-sys/bcr"
"github.com/starshine-sys/bcr/bot"
"github.com/termora/berry/db"
)
func (bot *Bot) commandList(ctx *bcr.Context) (err error) {
embeds := make([]discord.Embed, 0)
// get an accurate page count, modules with 0 non-hidden commands don't show up at all
var modCount int
for _, m := range bot.Modules {
modCount += commandCount(m)
}
// create a list of commands per module
for i, mod := range bot.Modules {
cmds := make([]string, 0)
for _, cmd := range mod.Commands() {
if cmd.Hidden {
// skip hidden commands
continue
}
cmds = append(cmds, fmt.Sprintf("`%v%v`: %v",
ifThing(
cmd.CustomPermissions == nil && cmd.Permissions == 0 && !cmd.OwnerOnly,
"", "[!] ",
), cmd.Name, cmd.Summary,
))
}
// if the module has no commands, skip this embed
if len(cmds) == 0 {
continue
}
embeds = append(embeds, discord.Embed{
Title: fmt.Sprintf("%v (%v)", mod.String(), len(mod.Commands())),
Description: strings.Join(cmds, "\n"),
Color: db.EmbedColour,
Footer: &discord.EmbedFooter{
Text: fmt.Sprintf("Commands marked with [!] need extra permissions. Page %v/%v", i+1, modCount),
},
})
}
_, err = ctx.PagedEmbed(embeds, false)
return err
}
func commandCount(m bot.Module) int {
var c int
for _, i := range m.Commands() {
if !i.Hidden {
c++
}
}
if c == 0 {
return 0
}
return 1
}
func ifThing(b bool, t, f string) string {
if b {
return t
}
return f
}