Skip to content

Commit

Permalink
refactor handler middleware signature & add handler.InteractionEvent …
Browse files Browse the repository at this point in the history
…type
  • Loading branch information
topi314 committed Feb 18, 2024
1 parent 2f57a4e commit 830bfac
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 516 deletions.
5 changes: 2 additions & 3 deletions _examples/handler/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/disgo/handler/middleware"
"github.com/disgoorg/snowflake/v2"
Expand Down Expand Up @@ -138,6 +137,6 @@ func handleComponent(event *handler.ComponentEvent) error {
return event.CreateMessage(discord.MessageCreate{Content: "component: " + data})
}

func handleNotFound(event *events.InteractionCreate) error {
return event.Respond(discord.InteractionResponseTypeCreateMessage, discord.MessageCreate{Content: "not found"})
func handleNotFound(event *handler.InteractionEvent) error {
return event.CreateMessage(discord.MessageCreate{Content: "not found"})
}
42 changes: 0 additions & 42 deletions _examples/otel/go.mod

This file was deleted.

72 changes: 0 additions & 72 deletions _examples/otel/go.sum

This file was deleted.

99 changes: 0 additions & 99 deletions _examples/otel/main.go

This file was deleted.

45 changes: 0 additions & 45 deletions _examples/otel/otel.go

This file was deleted.

7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ require (
github.com/gorilla/websocket v1.5.1
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.23.1
go.opentelemetry.io/otel/metric v1.23.1
go.opentelemetry.io/otel/trace v1.23.1
golang.org/x/crypto v0.19.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 2 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=
github.com/disgoorg/json v1.1.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=
github.com/disgoorg/snowflake/v2 v2.0.1/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -19,16 +12,10 @@ github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad h1:qIQkSlF5vAUHxE
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/otel v1.23.1 h1:Za4UzOqJYS+MUczKI320AtqZHZb7EqxO00jAHE0jmQY=
go.opentelemetry.io/otel v1.23.1/go.mod h1:Td0134eafDLcTS4y+zQ26GE8u3dEuRBiBCTUIRHaikA=
go.opentelemetry.io/otel/metric v1.23.1 h1:PQJmqJ9u2QaJLBOELl1cxIdPcpbwzbkjfEyelTl2rlo=
go.opentelemetry.io/otel/metric v1.23.1/go.mod h1:mpG2QPlAfnK8yNhNJAxDZruU9Y1/HubbC+KyH8FaCWI=
go.opentelemetry.io/otel/trace v1.23.1 h1:4LrmmEd8AU2rFvU1zegmvqW7+kWarxtNOPyeL6HmYY8=
go.opentelemetry.io/otel/trace v1.23.1/go.mod h1:4IpnpJFwr1mo/6HL8XIPJaE9y0+u1KcVmuW7dwFSVrI=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
21 changes: 10 additions & 11 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package handler

import (
"context"
"errors"
"strings"

Expand Down Expand Up @@ -70,8 +69,8 @@ func (h *handlerHolder[T]) Match(path string, t discord.InteractionType) bool {
return true
}

func (h *handlerHolder[T]) Handle(ctx context.Context, path string, variables map[string]string, event *events.InteractionCreate) error {
parseVariables(path, h.pattern, variables)
func (h *handlerHolder[T]) Handle(path string, event *InteractionEvent) error {
parseVariables(path, h.pattern, event.Vars)

switch handler := any(h.handler).(type) {
case CommandHandler:
Expand All @@ -81,8 +80,8 @@ func (h *handlerHolder[T]) Handle(ctx context.Context, path string, variables ma
ApplicationCommandInteraction: event.Interaction.(discord.ApplicationCommandInteraction),
Respond: event.Respond,
},
Vars: variables,
Ctx: ctx,
Vars: event.Vars,
Ctx: event.Ctx,
})
case AutocompleteHandler:
return handler(&AutocompleteEvent{
Expand All @@ -91,8 +90,8 @@ func (h *handlerHolder[T]) Handle(ctx context.Context, path string, variables ma
AutocompleteInteraction: event.Interaction.(discord.AutocompleteInteraction),
Respond: event.Respond,
},
Vars: variables,
Ctx: ctx,
Vars: event.Vars,
Ctx: event.Ctx,
})
case ComponentHandler:
return handler(&ComponentEvent{
Expand All @@ -101,8 +100,8 @@ func (h *handlerHolder[T]) Handle(ctx context.Context, path string, variables ma
ComponentInteraction: event.Interaction.(discord.ComponentInteraction),
Respond: event.Respond,
},
Vars: variables,
Ctx: ctx,
Vars: event.Vars,
Ctx: event.Ctx,
})
case ModalHandler:
return handler(&ModalEvent{
Expand All @@ -111,8 +110,8 @@ func (h *handlerHolder[T]) Handle(ctx context.Context, path string, variables ma
ModalSubmitInteraction: event.Interaction.(discord.ModalSubmitInteraction),
Respond: event.Respond,
},
Vars: variables,
Ctx: ctx,
Vars: event.Vars,
Ctx: event.Ctx,
})
}
return errors.New("unknown handler type")
Expand Down
Loading

0 comments on commit 830bfac

Please sign in to comment.