github.com/starshine-sys/bcr@v0.21.0/group.go (about) 1 package bcr 2 3 import ( 4 "strings" 5 6 "github.com/diamondburned/arikawa/v3/discord" 7 ) 8 9 // Group is used for creating slash subcommands. 10 // No, we can't use the normal system, 11 // because a command with subcommands can't *itself* be invoked as a command. 12 // Also, no subcommand groups because those make everything more complicated 13 // and shouldn't be needed at this scale. Might change in the future, who knows! 14 type Group struct { 15 Name string 16 Description string 17 Subcommands []*Command 18 } 19 20 // Add adds a subcommand to the group. 21 func (g *Group) Add(cmd *Command) *Group { 22 g.Subcommands = append(g.Subcommands, cmd) 23 return g 24 } 25 26 // Command returns the group as a discord.Command. 27 func (g Group) Command() discord.Command { 28 c := discord.Command{ 29 Name: strings.ToLower(g.Name), 30 Description: g.Description, 31 } 32 33 for _, cmd := range g.Subcommands { 34 if cmd.SlashCommand == nil { 35 continue 36 } 37 38 options := []discord.CommandOption(nil) 39 if cmd.Options != nil { 40 options = *cmd.Options 41 } 42 43 c.Options = append(c.Options, discord.CommandOption{ 44 Type: discord.SubcommandOption, 45 Name: strings.ToLower(cmd.Name), 46 Description: cmd.Summary, 47 Options: options, 48 }) 49 } 50 51 return c 52 } 53 54 // AddGroup adds a slash command group. Will panic if the group's name already exists as a slash command! 55 func (r *Router) AddGroup(g *Group) { 56 r.cmdMu.RLock() 57 defer r.cmdMu.RUnlock() 58 for _, cmd := range r.cmds { 59 if strings.EqualFold(cmd.Name, g.Name) && cmd.Options != nil && cmd.SlashCommand != nil { 60 panic("slash command with name " + g.Name + "already exists!") 61 } 62 } 63 64 r.slashGroups = append(r.slashGroups, g) 65 }