Skip to content

Commit

Permalink
fix: autocomplete routing on focused field (#27)
Browse files Browse the repository at this point in the history
* fix: autocomplete routing on focused field

* fix: when mounting focused, we were removing the default named options

* update interactions to fix possible conflicts
  • Loading branch information
PL Pery authored Jan 27, 2022
1 parent c23c585 commit cc9f3e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion 0_example/todo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
m.Command("list", t.listHandler)
m.Route("rm", func(m *corde.Mux) {
m.Command("", t.removeHandler)
m.Autocomplete("", t.autoCompleteNames)
m.Autocomplete("name", t.autoCompleteNames)
})
})

Expand Down
24 changes: 18 additions & 6 deletions interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const (
RouteInteractionSubcommandGroup = "$group"
// RouteInteractionSubcommand reprensents the map key for a subcommand route
RouteInteractionSubcommand = "$command"

// RouteInteractionFocused represents the map key for a focused route.
// This is useful for autocomplete interactions so we can route on focused keys
// Such, we route on `$group/$command/$focused`,
RouteInteractionFocused = "$focused"
)

// InteractionType is the type of interaction
Expand Down Expand Up @@ -291,6 +296,7 @@ func (o *OptionsInteractions) UnmarshalJSON(b []byte) error {
Value JsonRaw `json:"value"`
Type OptionType `json:"type"`
Options []opt `json:"options"`
Focused bool `json:"focused"`
}

var opts []opt
Expand All @@ -301,26 +307,32 @@ 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:
switch {
case OPTION_SUB_COMMAND_GROUP == opt.Type:
opt.Value = []byte(opt.Name)
opt.Name = RouteInteractionSubcommandGroup
case OPTION_SUB_COMMAND:
case OPTION_SUB_COMMAND == opt.Type:
opt.Value = []byte(opt.Name)
opt.Name = RouteInteractionSubcommand
case opt.Focused:
m[RouteInteractionFocused] = []byte(opt.Name)
}

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

m[opt2.Name] = opt2.Value
for _, opt3 := range opt2.Options {
if opt3.Focused {
m[RouteInteractionFocused] = []byte(opt3.Name)
}
m[opt3.Name] = opt3.Value
}
}
Expand Down
9 changes: 4 additions & 5 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (m *Mux) Route(pattern string, fn func(m *Mux)) {
// When you mount a command on the mux, it's prefix based routed,
// which means you can route to a button like `/list/next/456132153` having mounted `/list/next`
func NewMux(publicKey string, appID Snowflake, botToken string) *Mux {

m := &Mux{
rMu: &sync.RWMutex{},
routes: routes{
Expand Down Expand Up @@ -164,12 +163,11 @@ func (m *Mux) ListenAndServe(addr string) error {
return http.ListenAndServe(addr, r)
}

// ServeHTTP will serve HTTP requests with discord public key validation
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request){
// ServeHTTP will serve HTTP requests with discord public key validation
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.handler.ServeHTTP(w, r)
}


// route handles routing the requests
func (m *Mux) route(w http.ResponseWriter, r *http.Request) {
i := &InteractionRequest{
Expand Down Expand Up @@ -211,7 +209,8 @@ func (m *Mux) routeReq(r ResponseWriter, i *InteractionRequest) {
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())
focused := i.Data.Options[RouteInteractionFocused]
i.Data.Name = path.Join(i.Data.Name, group.String(), cmd.String(), focused.String())
if _, h, ok := m.routes.autocomplete.LongestPrefix(i.Data.Name); ok {
(*h)(r, i)
return
Expand Down

0 comments on commit cc9f3e1

Please sign in to comment.