package discord_router import "github.com/bwmarrin/discordgo" type HandlerFunc func(*discordgo.Session, *discordgo.InteractionCreate) type RouteEntry struct { CommandName string Handler HandlerFunc } func (re *RouteEntry) Match(i *discordgo.InteractionCreate) bool { switch i.Type { case discordgo.InteractionApplicationCommand: if i.ApplicationCommandData().Name != re.CommandName { return false } case discordgo.InteractionMessageComponent: if i.MessageComponentData().CustomID != re.CommandName { return false } } return true } type Router struct { session *discordgo.Session routes []RouteEntry } func NewApp(s *discordgo.Session) *Router { return &Router{ session: s, } } func (r *Router) Route(cmd string, handlerFunc HandlerFunc) *Router { r.routes = append(r.routes, RouteEntry{ CommandName: cmd, Handler: handlerFunc, }) return r } func (r *Router) Serve(s *discordgo.Session, i *discordgo.InteractionCreate) { for _, e := range r.routes { ok := e.Match(i) if ok { e.Handler(s, i) } } //[ ] Is there something like 404?! } type Middleware func(HandlerFunc) HandlerFunc func (r *Router) Wrapped(f HandlerFunc, m ...Middleware) HandlerFunc { if len(m) < 1 { return f } wrapped := f // loop in reverse to preserve middleware order for i := len(m) - 1; i >= 0; i-- { wrapped = m[i](wrapped) } return wrapped }