diff --git a/interactions.go b/interactions.go index 8963f6e..464f163 100644 --- a/interactions.go +++ b/interactions.go @@ -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 @@ -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 diff --git a/router.go b/router.go index 33a0cde..6f726d9 100644 --- a/router.go +++ b/router.go @@ -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) }