-
Notifications
You must be signed in to change notification settings - Fork 0
/
subrouter.go
71 lines (56 loc) · 1.77 KB
/
subrouter.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
package disroute
import "github.com/bwmarrin/discordgo"
type SubRouter struct {
root *Router
baseCmd *discordgo.ApplicationCommand
basePath string
lastOptions *[]*discordgo.ApplicationCommandOption
middlewares []MiddlewareFunc
}
func (r *SubRouter) Handle(cmd *discordgo.ApplicationCommandOption, h HandlerFunc) *AutocompletionBundle {
if cmd.Type != discordgo.ApplicationCommandOptionSubCommand {
return nil
}
path := r.basePath + ":" + cmd.Name
for i := len(r.middlewares) - 1; i >= 0; i-- {
h = r.middlewares[i](h)
}
r.root.handlers[path] = h
*r.lastOptions = append(*r.lastOptions, cmd)
return &AutocompletionBundle{
router: r.root,
path: path,
}
}
func (r *SubRouter) Group(cmd *discordgo.ApplicationCommandOption) *SubRouter {
if cmd.Type != discordgo.ApplicationCommandOptionSubCommandGroup {
return r
}
*r.lastOptions = append(*r.lastOptions, cmd)
newMiddlewares := make([]MiddlewareFunc, len(r.middlewares))
copy(newMiddlewares, r.middlewares)
return &SubRouter{
root: r.root,
baseCmd: r.baseCmd,
basePath: r.basePath + ":" + cmd.Name,
lastOptions: &cmd.Options,
middlewares: newMiddlewares,
}
}
func (r *SubRouter) Use(mw1 MiddlewareFunc, mw ...MiddlewareFunc) {
r.middlewares = append(r.middlewares, mw1)
r.middlewares = append(r.middlewares, mw...)
}
func (r *SubRouter) With(mw1 MiddlewareFunc, mw ...MiddlewareFunc) *SubRouter {
newMiddlewares := make([]MiddlewareFunc, len(r.middlewares), len(r.middlewares)+len(mw)+1)
copy(newMiddlewares, r.middlewares)
newMiddlewares = append(newMiddlewares, mw1)
newMiddlewares = append(newMiddlewares, mw...)
return &SubRouter{
root: r.root,
baseCmd: r.baseCmd,
basePath: r.basePath,
lastOptions: r.lastOptions,
middlewares: newMiddlewares,
}
}