From f3c3625b26fe0f4ad188ec07d4903e2392786bc4 Mon Sep 17 00:00:00 2001 From: TopiSenpai Date: Tue, 13 Jun 2023 16:32:14 +0200 Subject: [PATCH] add context to handler events --- handler/autocomplete.go | 3 +++ handler/command.go | 3 +++ handler/component.go | 3 +++ handler/handler.go | 7 ++++++- handler/middleware.go | 4 +++- handler/modal.go | 3 +++ handler/mux.go | 6 ++++-- handler/router.go | 4 +++- 8 files changed, 28 insertions(+), 5 deletions(-) diff --git a/handler/autocomplete.go b/handler/autocomplete.go index fafb2e78a..f07f4c922 100644 --- a/handler/autocomplete.go +++ b/handler/autocomplete.go @@ -1,6 +1,8 @@ package handler import ( + "context" + "github.com/disgoorg/snowflake/v2" "github.com/disgoorg/disgo/discord" @@ -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) { diff --git a/handler/command.go b/handler/command.go index 7e6cfc0e4..ce03b22d6 100644 --- a/handler/command.go +++ b/handler/command.go @@ -1,6 +1,8 @@ package handler import ( + "context" + "github.com/disgoorg/snowflake/v2" "github.com/disgoorg/disgo/discord" @@ -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) { diff --git a/handler/component.go b/handler/component.go index 1d7f2d06b..9195bd509 100644 --- a/handler/component.go +++ b/handler/component.go @@ -1,6 +1,8 @@ package handler import ( + "context" + "github.com/disgoorg/snowflake/v2" "github.com/disgoorg/disgo/discord" @@ -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) { diff --git a/handler/handler.go b/handler/handler.go index 63d60b805..d83df9205 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -18,6 +18,7 @@ package handler import ( + "context" "errors" "strings" @@ -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) { @@ -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{ @@ -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{ @@ -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{ @@ -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") diff --git a/handler/middleware.go b/handler/middleware.go index d38732b95..6978c0184 100644 --- a/handler/middleware.go +++ b/handler/middleware.go @@ -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 diff --git a/handler/modal.go b/handler/modal.go index cdf0b6568..d14ea9499 100644 --- a/handler/modal.go +++ b/handler/modal.go @@ -1,6 +1,8 @@ package handler import ( + "context" + "github.com/disgoorg/snowflake/v2" "github.com/disgoorg/disgo/discord" @@ -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) { diff --git a/handler/mux.go b/handler/mux.go index 4dba1c25c..34d41e50e 100644 --- a/handler/mux.go +++ b/handler/mux.go @@ -2,6 +2,7 @@ package handler import ( "log/slog" + "context" "strings" "github.com/disgoorg/disgo/bot" @@ -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. @@ -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 @@ -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) diff --git a/handler/router.go b/handler/router.go index 33cc71319..ad0a87eaa 100644 --- a/handler/router.go +++ b/handler/router.go @@ -1,6 +1,8 @@ package handler import ( + "context" + "github.com/disgoorg/disgo/bot" "github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/events" @@ -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.