Skip to content

Commit

Permalink
subcommand group routing fix (#25)
Browse files Browse the repository at this point in the history
Fix routing of sub command groups, and as a side effect, fix overlaying between map keys on args & commands
  • Loading branch information
PL Pery authored Jan 19, 2022
1 parent 0370526 commit f0b80f4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
23 changes: 23 additions & 0 deletions interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"strings"
)

const (
// RouteInteractionSubcommandGroup represents the map key for a subcommand group route
RouteInteractionSubcommandGroup = "$group"
// RouteInteractionSubcommand reprensents the map key for a subcommand route
RouteInteractionSubcommand = "$command"
)

// InteractionType is the type of interaction
type InteractionType int

Expand Down Expand Up @@ -294,8 +301,24 @@ func (o *OptionsInteractions) UnmarshalJSON(b []byte) error {
// max is 3 deep, as per discord's docs
m := make(map[string]JsonRaw)
for _, opt := range opts {
// enables us to route easily
switch opt.Type {
case OPTION_SUB_COMMAND_GROUP:
opt.Value = []byte(opt.Name)
opt.Name = RouteInteractionSubcommandGroup
case OPTION_SUB_COMMAND:
opt.Value = []byte(opt.Name)
opt.Name = RouteInteractionSubcommand
}

m[opt.Name] = opt.Value
for _, opt2 := range opt.Options {
// enables us to route easily
if opt2.Type == OPTION_SUB_COMMAND {
opt2.Value = []byte(opt2.Name)
opt2.Name = RouteInteractionSubcommand
}

m[opt2.Name] = opt2.Value
for _, opt3 := range opt2.Options {
m[opt3.Name] = opt3.Value
Expand Down
38 changes: 9 additions & 29 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,43 +193,23 @@ func (m *Mux) routeReq(r ResponseWriter, i *InteractionRequest) {
}
case INTERACTION_TYPE_APPLICATION_COMMAND:
// for menu & app commands, which can have spaces
path := strings.ReplaceAll(i.Data.Name, " ", "/")
if _, h, ok := m.routes.command.LongestPrefix(path); ok {
i.Data.Name = path.Join(strings.Fields(i.Data.Name)...)

group := i.Data.Options[RouteInteractionSubcommandGroup]
cmd := i.Data.Options[RouteInteractionSubcommand]
i.Data.Name = path.Join(i.Data.Name, group.String(), cmd.String())
if _, h, ok := m.routes.command.LongestPrefix(i.Data.Name); ok {
(*h)(r, i)
return
}
for optName, optV := range i.Data.Options {
// Subcommands cannot have values :)
if optV != nil {
continue
}

nr := i.Data.Name + "/" + optName
if _, h, ok := m.routes.command.LongestPrefix(nr); ok {
i.Data.Name = nr
(*h)(r, i)
return
}
}
case INTERACTION_TYPE_APPLICATION_COMMAND_AUTOCOMPLETE:
group := i.Data.Options[RouteInteractionSubcommandGroup]
cmd := i.Data.Options[RouteInteractionSubcommand]
i.Data.Name = path.Join(i.Data.Name, group.String(), cmd.String())
if _, h, ok := m.routes.autocomplete.LongestPrefix(i.Data.Name); ok {
(*h)(r, i)
return
}

for optName, optV := range i.Data.Options {
// Subcommands cannot have values :)
if optV != nil {
continue
}

nr := i.Data.Name + "/" + optName
if _, h, ok := m.routes.autocomplete.LongestPrefix(nr); ok {
i.Data.Name = nr
(*h)(r, i)
return
}
}
}
m.OnNotFound(r, i)
}
Expand Down

0 comments on commit f0b80f4

Please sign in to comment.