-
Notifications
You must be signed in to change notification settings - Fork 0
/
disroute.go
143 lines (111 loc) · 3.3 KB
/
disroute.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
package disroute
import (
"github.com/bwmarrin/discordgo"
)
type Router struct {
session *discordgo.Session
cmds *[]*discordgo.ApplicationCommand
middlewares []MiddlewareFunc
handlers map[string]HandlerFunc
autocomp map[string]AutocompleteFunc
components map[string]HandlerFunc
responseHandler func(*Ctx, *Response)
autocompleteHandler func(*Ctx, []*discordgo.ApplicationCommandOptionChoice)
componentKeyFunc func(*discordgo.Interaction) string
}
func New(token string) (*Router, error) {
s, err := discordgo.New("Bot " + token)
if err != nil {
return nil, err
}
cmds := make([]*discordgo.ApplicationCommand, 0, 100)
return &Router{
session: s,
cmds: &cmds,
handlers: make(map[string]HandlerFunc),
autocomp: make(map[string]AutocompleteFunc),
components: make(map[string]HandlerFunc),
responseHandler: defaultResponseHandler,
autocompleteHandler: defaultAutocompleteHandler,
componentKeyFunc: defaultComponentKeyFunc,
}, nil
}
func (r *Router) Open() error {
err := r.session.Open()
if err != nil {
return err
}
_, err = r.session.ApplicationCommandBulkOverwrite(r.session.State.User.ID, "", *r.cmds)
if err != nil {
return err
}
return nil
}
func (r *Router) SetResponseHandler(h func(*Ctx, *Response)) {
r.responseHandler = h
}
func (r *Router) SetAutocompleteHandler(h func(*Ctx, []*discordgo.ApplicationCommandOptionChoice)) {
r.autocompleteHandler = h
}
func (r *Router) SetComponentKeyFunc(f func(*discordgo.Interaction) (key string)) {
r.componentKeyFunc = f
}
func (r *Router) Session() *discordgo.Session {
return r.session
}
func (r *Router) Handle(cmd *discordgo.ApplicationCommand, h HandlerFunc) *AutocompletionBundle {
if _, ok := r.handlers[cmd.Name]; ok {
return nil
}
for i := len(r.middlewares) - 1; i >= 0; i-- {
h = r.middlewares[i](h)
}
r.handlers[cmd.Name] = h
*r.cmds = append(*r.cmds, cmd)
return &AutocompletionBundle{
router: r,
path: cmd.Name,
}
}
func (r *Router) HandleComponent(key string, h HandlerFunc) {
if _, ok := r.components[key]; ok {
return
}
for i := len(r.middlewares) - 1; i >= 0; i-- {
h = r.middlewares[i](h)
}
r.components[key] = h
}
func (r *Router) Mount(cmd *discordgo.ApplicationCommand) *SubRouter {
newMiddlewares := make([]MiddlewareFunc, len(r.middlewares))
copy(newMiddlewares, r.middlewares)
*r.cmds = append(*r.cmds, cmd)
return &SubRouter{
root: r,
baseCmd: cmd,
basePath: cmd.Name,
lastOptions: &cmd.Options,
middlewares: newMiddlewares,
}
}
func (r *Router) Use(mw1 MiddlewareFunc, mw ...MiddlewareFunc) {
r.middlewares = append(r.middlewares, mw1)
r.middlewares = append(r.middlewares, mw...)
}
func (r *Router) With(mw1 MiddlewareFunc, mw ...MiddlewareFunc) *Router {
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 &Router{
session: r.session,
middlewares: newMiddlewares,
cmds: r.cmds,
handlers: r.handlers,
autocomp: r.autocomp,
components: r.components,
responseHandler: r.responseHandler,
autocompleteHandler: r.autocompleteHandler,
componentKeyFunc: r.componentKeyFunc,
}
}