-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_loader.go
178 lines (146 loc) · 5.34 KB
/
router_loader.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
package router
import (
"fmt"
"os"
"github.com/Postcord/interactions"
"github.com/Postcord/objects"
"github.com/Postcord/rest"
)
// ErrorHandler defines the error handler function used within Postcord.
type ErrorHandler = func(error) *objects.InteractionResponse
// Defines the builder.
type loaderBuilder struct {
globalAllowedMentions *objects.AllowedMentions
components *ComponentRouter
commands *CommandRouter
modals *ModalRouter
errHandler ErrorHandler
app HandlerAccepter
}
func (l *loaderBuilder) ComponentRouter(router *ComponentRouter) LoaderBuilder {
l.components = router
return l
}
func (l *loaderBuilder) ErrorHandler(cb ErrorHandler) LoaderBuilder {
l.errHandler = cb
return l
}
func (l *loaderBuilder) CommandRouter(router *CommandRouter) LoaderBuilder {
l.commands = router
return l
}
func (l *loaderBuilder) ModalRouter(router *ModalRouter) LoaderBuilder {
l.modals = router
return l
}
// CombinedRouter is an extension of both CommandRouter and ComponentRouter to combine the two.
// I'm personally not a huge fan of using this, but it might be appealing to some people who wish to treat it as one router.
type CombinedRouter struct {
CommandRouter
ComponentRouter
ModalRouter
}
func (l *loaderBuilder) CombinedRouter(router *CombinedRouter) LoaderBuilder {
if router == nil {
l.components = nil
l.commands = nil
l.modals = nil
} else {
l.components = &router.ComponentRouter
l.commands = &router.CommandRouter
l.modals = &router.ModalRouter
}
return l
}
func genericErrorHandler(err error) *objects.InteractionResponse {
// Log the message.
fmt.Println("error on route:", err)
// Pass off to Postcord/interaction's generic handler by setting to nil.
return nil
}
func (l *loaderBuilder) AllowedMentions(config *objects.AllowedMentions) LoaderBuilder {
l.globalAllowedMentions = config
return l
}
// HandlerAccepter is an interface for an object which accepts Postcord handler functions.
// In most cases, you probably want to pass through *interactions.App here.
type HandlerAccepter interface {
ComponentHandler(handler interactions.HandlerFunc)
CommandHandler(handler interactions.HandlerFunc)
AutocompleteHandler(handler interactions.HandlerFunc)
ModalHandler(handler interactions.HandlerFunc)
Rest() *rest.Client
}
// Defines the various bits passed through from the loader.
type loaderPassthrough struct {
rest rest.RESTClient
errHandler ErrorHandler
modalRouter *ModalRouter
globalAllowedMentions *objects.AllowedMentions
generateFrames bool
}
func (l *loaderBuilder) Build(app HandlerAccepter) LoaderBuilder {
l.app = app
cb := l.errHandler
if cb == nil {
// Defines a generic error handler if the user hasn't made their own.
cb = genericErrorHandler
}
generateFrames := os.Getenv("POSTCORD_GENERATE_FRAMES") == "1"
// Create the passthrough.
passthrough := loaderPassthrough{
rest: app.Rest(),
errHandler: cb,
modalRouter: l.modals,
globalAllowedMentions: l.globalAllowedMentions,
generateFrames: generateFrames,
}
if l.modals != nil {
// Build and load the modals handler.
modals := l.modals.build(passthrough)
app.ModalHandler(modals)
}
if l.components != nil {
// Build and load the components handler.
handler := l.components.build(l.modals, passthrough)
app.ComponentHandler(handler)
}
if l.commands != nil {
// Build and load the commands/autocomplete handler.
commandHandler, autocompleteHandler := l.commands.build(passthrough)
app.CommandHandler(commandHandler)
app.AutocompleteHandler(autocompleteHandler)
}
return l
}
func (l *loaderBuilder) CurrentChain() (*ComponentRouter, *CommandRouter, *ModalRouter, ErrorHandler, rest.RESTClient, *objects.AllowedMentions) {
var restClient rest.RESTClient
if l.app != nil {
restClient = l.app.Rest()
}
return l.components, l.commands, l.modals, l.errHandler, restClient, l.globalAllowedMentions
}
// LoaderBuilder is the interface for a router loader builder.
type LoaderBuilder interface {
// ComponentRouter is used to add a component router to the load process.
ComponentRouter(*ComponentRouter) LoaderBuilder
// CommandRouter is used to add a command router to the load process.
CommandRouter(*CommandRouter) LoaderBuilder
// ModalRouter is used to add a modal router to the load process.
ModalRouter(*ModalRouter) LoaderBuilder
// CombinedRouter is used to add a combined router to the load process.
CombinedRouter(*CombinedRouter) LoaderBuilder
// ErrorHandler is used to add an error handler to the load process.
ErrorHandler(ErrorHandler) LoaderBuilder
// AllowedMentions allows you to set a global allowed mentions configuration.
AllowedMentions(*objects.AllowedMentions) LoaderBuilder
// Build is used to execute the build.
Build(app HandlerAccepter) LoaderBuilder
// CurrentChain is used to get the current chain of items. Note that for obvious reasons, this is not chainable.
// Used internally by Postcord for our testing mechanism.
CurrentChain() (componentRouter *ComponentRouter, commandRouter *CommandRouter, modalRouter *ModalRouter, errHandler ErrorHandler, restClient rest.RESTClient, allowedMentions *objects.AllowedMentions)
}
// RouterLoader is used to create a new router loader builder.
func RouterLoader() LoaderBuilder {
return &loaderBuilder{}
}