Skip to content

Commit

Permalink
fix nil Mux defaultContext field & added setter
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Mar 13, 2024
1 parent ecbc6f4 commit 0361f5d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion handler/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ func (r *Mux) OnEvent(event bot.Event) {
path = i.Data.CustomID
}

var ctx context.Context
if r.defaultContext != nil {
ctx = r.defaultContext()
} else {
ctx = context.Background()
}

ie := &InteractionEvent{
InteractionCreate: e,
Ctx: r.defaultContext(),
Ctx: ctx,
Vars: make(map[string]string),
}
if err := r.Handle(path, ie); err != nil {
Expand Down Expand Up @@ -224,6 +231,12 @@ func (r *Mux) Error(h ErrorHandler) {
r.errorHandler = h
}

// DefaultContext sets the default context for this router.
// This context will be used for all interaction events.
func (r *Mux) DefaultContext(ctx func() context.Context) {
r.defaultContext = ctx
}

func checkPattern(pattern string) {
if len(pattern) == 0 {
panic("pattern must not be empty")
Expand Down

0 comments on commit 0361f5d

Please sign in to comment.