Skip to content

Commit

Permalink
extend middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Feb 1, 2024
1 parent 34e3555 commit 51c65aa
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions handler/middleware/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,40 @@ package middleware
import (
"log/slog"

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

// Go is a middleware that runs the next handler in a goroutine
var Go handler.Middleware = func(next handler.Handler) handler.Handler {
return func(e *events.InteractionCreate) error {
go func() {
if err := next(e); err != nil {
e.Client().Logger().Error("failed to handle interaction", slog.String("err", err.Error()))
}
}()
return nil
// Go is a middleware that runs the next handler in a goroutine.
var Go = GoErr(func(e *events.InteractionCreate, err error) {
e.Client().Logger().Error("failed to handle interaction", slog.String("err", err.Error()))
})

// GoDefer combines Go and Defer
func GoDefer(interactionType discord.InteractionType, updateMessage bool, ephemeral bool) handler.Middleware {
return func(next handler.Handler) handler.Handler {
return Go(Defer(interactionType, updateMessage, ephemeral)(next))
}
}

// GoErr is a middleware that runs the next handler in a goroutine and lets you handle the error which may occur.
func GoErr(h handler.ErrorHandler) handler.Middleware {
return func(next handler.Handler) handler.Handler {
return func(e *events.InteractionCreate) error {
go func() {
if err := next(e); err != nil {
h(e, err)
}
}()
return nil
}
}
}

// GoErrDefer combines GoErr and Defer
func GoErrDefer(h handler.ErrorHandler, interactionType discord.InteractionType, updateMessage bool, ephemeral bool) handler.Middleware {
return func(next handler.Handler) handler.Handler {
return GoErr(h)(Defer(interactionType, updateMessage, ephemeral)(next))
}
}

0 comments on commit 51c65aa

Please sign in to comment.