v4/interactions/router.go
2025-03-25 21:26:24 -07:00

38 lines
804 B
Go

package interactions
import (
"net/http"
"git.sapphic.engineer/roleypoly/v4/types"
"github.com/gofiber/fiber/v3"
)
type InteractionHandler func(interaction Interaction) (InteractionResponse, error)
type InteractionRouter struct {
Routes map[string]InteractionHandler
}
func NewInteractionRouter() *InteractionRouter {
return &InteractionRouter{
Routes: map[string]InteractionHandler{},
}
}
func (r *InteractionRouter) Add(name string, h InteractionHandler) {
r.Routes[name] = h
}
func (r *InteractionRouter) Handle(c fiber.Ctx, interaction Interaction) error {
route, ok := r.Routes[interaction.Data.Name]
if !ok {
return types.NewAPIError(http.StatusNotFound, "command not found").Send(c)
}
resp, err := route(interaction)
if err != nil {
return err
}
return c.JSON(resp)
}