-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmux.go
348 lines (306 loc) · 9.36 KB
/
mux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package handler
import (
"context"
"log/slog"
"strings"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
)
var defaultErrorHandler ErrorHandler = func(event *InteractionEvent, err error) {
event.Client().Logger().Error("error handling interaction", slog.Any("err", err))
}
// New returns a new Router.
func New() *Mux {
return &Mux{}
}
func newRouter(pattern string, middlewares []Middleware, routes []Route) *Mux {
return &Mux{
pattern: pattern,
middlewares: middlewares,
routes: routes,
}
}
// Mux is a basic Router implementation.
type Mux struct {
pattern string
middlewares []Middleware
routes []Route
notFoundHandler NotFoundHandler
errorHandler ErrorHandler
defaultContext func() context.Context
}
// OnEvent is called when a new event is received.
func (r *Mux) OnEvent(event bot.Event) {
e, ok := event.(*events.InteractionCreate)
if !ok {
return
}
var path string
switch i := e.Interaction.(type) {
case discord.ApplicationCommandInteraction:
if sci, ok := i.Data.(discord.SlashCommandInteractionData); ok {
path = sci.CommandPath()
} else {
path = "/" + i.Data.CommandName()
}
case discord.AutocompleteInteraction:
path = i.Data.CommandPath()
case discord.ComponentInteraction:
path = i.Data.CustomID()
case discord.ModalSubmitInteraction:
path = i.Data.CustomID
}
var ctx context.Context
if r.defaultContext != nil {
ctx = r.defaultContext()
} else {
ctx = context.Background()
}
ie := &InteractionEvent{
InteractionCreate: e,
Ctx: ctx,
Vars: make(map[string]string),
}
if err := r.Handle(path, ie); err != nil {
if r.errorHandler != nil {
r.errorHandler(ie, err)
return
}
defaultErrorHandler(ie, err)
}
}
// Match returns true if the given path matches the Route.
func (r *Mux) Match(path string, t discord.InteractionType, t2 int) bool {
if r.pattern != "" {
parts := splitPath(path)
patternParts := splitPath(r.pattern)
for i, part := range patternParts {
path = strings.TrimPrefix(path, "/"+parts[i])
if strings.HasPrefix(part, "{") && strings.HasSuffix(part, "}") {
continue
}
if len(parts) <= i || part != parts[i] {
return false
}
}
}
for _, matcher := range r.routes {
if matcher.Match(path, t, t2) {
return true
}
}
return false
}
// Handle handles the given interaction event.
func (r *Mux) Handle(path string, event *InteractionEvent) error {
path = parseVariables(path, r.pattern, event.Vars)
handlerChain := Handler(func(event *InteractionEvent) error {
t := event.Type()
var t2 int
switch i := event.Interaction.(type) {
case discord.ApplicationCommandInteraction:
t2 = int(i.Data.Type())
case discord.ComponentInteraction:
t2 = int(i.Data.Type())
}
for _, route := range r.routes {
if route.Match(path, t, t2) {
return route.Handle(path, event)
}
}
if r.notFoundHandler != nil {
return r.notFoundHandler(event)
}
return nil
})
for i := len(r.middlewares) - 1; i >= 0; i-- {
handlerChain = r.middlewares[i](handlerChain)
}
return handlerChain(event)
}
// Use adds the given middlewares to the current Router.
func (r *Mux) Use(middlewares ...Middleware) {
r.middlewares = append(r.middlewares, middlewares...)
}
// With returns a new Router with the given middlewares.
func (r *Mux) With(middlewares ...Middleware) Router {
return newRouter("", middlewares, nil)
}
// Group creates a new Router and adds it to the current Router.
func (r *Mux) Group(fn func(router Router)) {
router := New()
fn(router)
r.handle(router)
}
// Route creates a new sub-router with the given pattern and adds it to the current Router.
func (r *Mux) Route(pattern string, fn func(r Router)) Router {
checkPattern(pattern)
router := newRouter(pattern, nil, nil)
fn(router)
r.handle(router)
return router
}
// Mount mounts the given router with the given pattern to the current Router.
func (r *Mux) Mount(pattern string, router Router) {
if pattern == "" {
r.handle(router)
return
}
r.handle(newRouter(pattern, nil, []Route{router}))
}
func (r *Mux) handle(route Route) {
r.routes = append(r.routes, route)
}
// Interaction registers the given InteractionHandler to the current Router.
// This is a shortcut for Command, Autocomplete, Component and Modal.
func (r *Mux) Interaction(pattern string, h InteractionHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[InteractionHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionType(0),
})
}
// Command registers the given CommandHandler to the current Router.
func (r *Mux) Command(pattern string, h CommandHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[CommandHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeApplicationCommand,
})
}
// SlashCommand registers the given SlashCommandHandler to the current Router.
func (r *Mux) SlashCommand(pattern string, h SlashCommandHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[SlashCommandHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeApplicationCommand,
t2: []int{int(discord.ApplicationCommandTypeSlash)},
})
}
// UserCommand registers the given UserCommandHandler to the current Router.
func (r *Mux) UserCommand(pattern string, h UserCommandHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[UserCommandHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeApplicationCommand,
t2: []int{int(discord.ApplicationCommandTypeUser)},
})
}
// MessageCommand registers the given MessageCommandHandler to the current Router.
func (r *Mux) MessageCommand(pattern string, h MessageCommandHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[MessageCommandHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeApplicationCommand,
t2: []int{int(discord.ApplicationCommandTypeMessage)},
})
}
// EntryPointCommand registers the given EntryPointCommandHandler to the current Router.
func (r *Mux) EntryPointCommand(pattern string, h EntryPointCommandHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[EntryPointCommandHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeApplicationCommand,
t2: []int{int(discord.ApplicationCommandTypePrimaryEntryPoint)},
})
}
// Autocomplete registers the given AutocompleteHandler to the current Router.
func (r *Mux) Autocomplete(pattern string, h AutocompleteHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[AutocompleteHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeAutocomplete,
})
}
// Component registers the given ComponentHandler to the current Router.
func (r *Mux) Component(pattern string, h ComponentHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[ComponentHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeComponent,
})
}
// ButtonComponent registers the given ButtonComponentHandler to the current Router.
func (r *Mux) ButtonComponent(pattern string, h ButtonComponentHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[ButtonComponentHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeComponent,
t2: []int{int(discord.ComponentTypeButton)},
})
}
// SelectMenuComponent registers the given SelectMenuComponentHandler to the current Router.
func (r *Mux) SelectMenuComponent(pattern string, h SelectMenuComponentHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[SelectMenuComponentHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeComponent,
t2: []int{
int(discord.ComponentTypeStringSelectMenu),
int(discord.ComponentTypeUserSelectMenu),
int(discord.ComponentTypeRoleSelectMenu),
int(discord.ComponentTypeMentionableSelectMenu),
int(discord.ComponentTypeChannelSelectMenu),
},
})
}
// Modal registers the given ModalHandler to the current Router.
func (r *Mux) Modal(pattern string, h ModalHandler) {
checkPattern(pattern)
r.handle(&handlerHolder[ModalHandler]{
pattern: pattern,
handler: h,
t: discord.InteractionTypeModalSubmit,
})
}
// NotFound sets the NotFoundHandler for this router.
// This handler only works for the root router and will be ignored for sub routers.
func (r *Mux) NotFound(h NotFoundHandler) {
r.notFoundHandler = h
}
// Error sets the ErrorHandler for this router.
// This handler only works for the root router and will be ignored for sub routers.
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")
}
if pattern[0] != '/' {
panic("pattern must start with /")
}
}
func splitPath(path string) []string {
return strings.FieldsFunc(path, func(r rune) bool { return r == '/' })
}
func parseVariables(path string, pattern string, variables map[string]string) string {
if pattern == "" {
return path
}
parts := splitPath(path)
patternParts := splitPath(pattern)
for i := range patternParts {
path = strings.TrimPrefix(path, "/"+parts[i])
if strings.HasPrefix(patternParts[i], "{") && strings.HasSuffix(patternParts[i], "}") {
variables[patternParts[i][1:len(patternParts[i])-1]] = parts[i]
}
}
return path
}