Skip to content

Commit

Permalink
add context to handler events
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Feb 7, 2024
1 parent d3e0672 commit f3c3625
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 5 deletions.
3 changes: 3 additions & 0 deletions handler/autocomplete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handler

import (
"context"

"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/discord"
Expand All @@ -11,6 +13,7 @@ import (
type AutocompleteEvent struct {
*events.AutocompleteInteractionCreate
Variables map[string]string
Ctx context.Context
}

func (e *AutocompleteEvent) GetFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error) {
Expand Down
3 changes: 3 additions & 0 deletions handler/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handler

import (
"context"

"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/discord"
Expand All @@ -11,6 +13,7 @@ import (
type CommandEvent struct {
*events.ApplicationCommandInteractionCreate
Variables map[string]string
Ctx context.Context
}

func (e *CommandEvent) GetInteractionResponse(opts ...rest.RequestOpt) (*discord.Message, error) {
Expand Down
3 changes: 3 additions & 0 deletions handler/component.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handler

import (
"context"

"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/discord"
Expand All @@ -11,6 +13,7 @@ import (
type ComponentEvent struct {
*events.ComponentInteractionCreate
Variables map[string]string
Ctx context.Context
}

func (e *ComponentEvent) GetInteractionResponse(opts ...rest.RequestOpt) (*discord.Message, error) {
Expand Down
7 changes: 6 additions & 1 deletion handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package handler

import (
"context"
"errors"
"strings"

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

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

switch handler := any(h.handler).(type) {
Expand All @@ -81,6 +82,7 @@ func (h *handlerHolder[T]) Handle(path string, variables map[string]string, even
Respond: event.Respond,
},
Variables: variables,
Ctx: ctx,
})
case AutocompleteHandler:
return handler(&AutocompleteEvent{
Expand All @@ -90,6 +92,7 @@ func (h *handlerHolder[T]) Handle(path string, variables map[string]string, even
Respond: event.Respond,
},
Variables: variables,
Ctx: ctx,
})
case ComponentHandler:
return handler(&ComponentEvent{
Expand All @@ -99,6 +102,7 @@ func (h *handlerHolder[T]) Handle(path string, variables map[string]string, even
Respond: event.Respond,
},
Variables: variables,
Ctx: ctx,
})
case ModalHandler:
return handler(&ModalEvent{
Expand All @@ -108,6 +112,7 @@ func (h *handlerHolder[T]) Handle(path string, variables map[string]string, even
Respond: event.Respond,
},
Variables: variables,
Ctx: ctx,
})
}
return errors.New("unknown handler type")
Expand Down
4 changes: 3 additions & 1 deletion handler/middleware.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package handler

import (
"context"

"github.com/disgoorg/disgo/events"
)

type (
Handler func(e *events.InteractionCreate) error
Handler func(ctx context.Context, e *events.InteractionCreate) error

Middleware func(next Handler) Handler

Expand Down
3 changes: 3 additions & 0 deletions handler/modal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handler

import (
"context"

"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/discord"
Expand All @@ -11,6 +13,7 @@ import (
type ModalEvent struct {
*events.ModalSubmitInteractionCreate
Variables map[string]string
Ctx context.Context
}

func (e *ModalEvent) GetInteractionResponse(opts ...rest.RequestOpt) (*discord.Message, error) {
Expand Down
6 changes: 4 additions & 2 deletions handler/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"log/slog"
"context"
"strings"

"github.com/disgoorg/disgo/bot"
Expand Down Expand Up @@ -33,6 +34,7 @@ type Mux struct {
routes []Route
notFoundHandler NotFoundHandler
errorHandler ErrorHandler
defaultContext func() context.Context
}

// OnEvent is called when a new event is received.
Expand All @@ -58,7 +60,7 @@ func (r *Mux) OnEvent(event bot.Event) {
path = i.Data.CustomID
}

if err := r.Handle(path, make(map[string]string), e); err != nil {
if err := r.Handle(r.defaultContext(), path, make(map[string]string), e); err != nil {
if r.errorHandler != nil {
r.errorHandler(e, err)
return
Expand Down Expand Up @@ -109,7 +111,7 @@ func (r *Mux) Handle(path string, variables map[string]string, e *events.Interac
}

for i := len(r.middlewares) - 1; i >= 0; i-- {
handlerChain = r.middlewares[i](handlerChain)
handlerChain = r.middlewares[i](ctx, handlerChain)
}

return handlerChain(e)
Expand Down
4 changes: 3 additions & 1 deletion handler/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handler

import (
"context"

"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
Expand Down Expand Up @@ -29,7 +31,7 @@ type Route interface {
Match(path string, t discord.InteractionType) bool

// Handle handles the given interaction event.
Handle(path string, variables map[string]string, e *events.InteractionCreate) error
Handle(ctx context.Context, path string, variables map[string]string, e *events.InteractionCreate) error
}

// Router provides with the core routing functionality.
Expand Down

0 comments on commit f3c3625

Please sign in to comment.