github.com/starshine-sys/bcr@v0.21.0/bot/commands.go (about) 1 package bot 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/diamondburned/arikawa/v3/discord" 8 "github.com/starshine-sys/bcr" 9 ) 10 11 // CommandList is a command that shows a list of all commands in the bot instance 12 func (bot *Bot) CommandList(ctx *bcr.Context) (err error) { 13 // help for commands 14 if len(ctx.Args) > 0 { 15 return ctx.Help(ctx.Args) 16 } 17 18 embeds := make([]discord.Embed, 0) 19 20 // get an accurate page count, modules with 0 non-hidden commands don't show up at all 21 var modCount int 22 for _, m := range bot.Modules { 23 modCount += commandCount(m) 24 } 25 26 // create a list of commands per module 27 for i, mod := range bot.Modules { 28 cmds := make([]string, 0) 29 for _, cmd := range mod.Commands() { 30 if cmd.Hidden { 31 // skip hidden commands 32 continue 33 } 34 cmds = append(cmds, fmt.Sprintf("`%v%v`: %v", 35 ifThing( 36 cmd.CustomPermissions == nil && cmd.Permissions == 0 && !cmd.OwnerOnly, 37 "", "[!] ", 38 ), cmd.Name, cmd.Summary, 39 )) 40 } 41 42 // if the module has no commands, skip this embed 43 if len(cmds) == 0 { 44 continue 45 } 46 47 embeds = append(embeds, discord.Embed{ 48 Title: fmt.Sprintf("%v (%v)", mod.String(), len(mod.Commands())), 49 Description: strings.Join(cmds, "\n"), 50 Color: ctx.Router.EmbedColor, 51 52 Footer: &discord.EmbedFooter{ 53 Text: fmt.Sprintf("Commands marked with [!] need extra permissions. Page %v/%v", i+1, modCount), 54 }, 55 }) 56 } 57 58 _, err = ctx.PagedEmbed(embeds, false) 59 return err 60 } 61 62 func commandCount(m Module) int { 63 var c int 64 for _, i := range m.Commands() { 65 if !i.Hidden { 66 c++ 67 } 68 } 69 70 if c == 0 { 71 return 0 72 } 73 return 1 74 } 75 76 func ifThing(b bool, t, f string) string { 77 if b { 78 return t 79 } 80 return f 81 }