-
Notifications
You must be signed in to change notification settings - Fork 7
/
components.go
209 lines (177 loc) · 5.01 KB
/
components.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
package ken
import (
"sync"
"github.com/bwmarrin/discordgo"
)
// ComponentHandleFunc is the handler function for
// message component interactions. It is getting
// passed a ComponentContext which contians the
// interaction event data and can be used to respond
// to the interaction.
//
// A boolean is returned to indicate the success of
// the execution of the handler.
type ComponentHandlerFunc func(ctx ComponentContext) bool
type ModalHandlerFunc func(ctx ModalContext) bool
// ComponentHandler keeps a registry of component handler
// callbacks to be executed when a given component has
// been interacted with.
type ComponentHandler struct {
ken *Ken
unregisterFunc func()
mtx sync.RWMutex
handlers map[string]ComponentHandlerFunc
modalHandlers map[string]ModalHandlerFunc
ctxPool sync.Pool
modalCtxPool sync.Pool
}
// NewComponentHandler returns a new instance of
// ComponentHandler using the given instance of
// Ken.
func NewComponentHandler(ken *Ken) *ComponentHandler {
var t ComponentHandler
t.ken = ken
t.handlers = make(map[string]ComponentHandlerFunc)
t.modalHandlers = make(map[string]ModalHandlerFunc)
t.unregisterFunc = t.ken.s.AddHandler(t.handle)
t.ctxPool = sync.Pool{
New: func() interface{} {
return &componentCtx{}
},
}
t.modalCtxPool = sync.Pool{
New: func() interface{} {
return &modalCtx{}
},
}
return &t
}
// Add returns a new ComponentBuilder which attaches the
// added components to the given message by messageId and
// channelId on build.
func (t *ComponentHandler) Add(messageId, channelId string) *ComponentBuilder {
return newBuilderAttach(t, messageId, channelId)
}
// Register registers a raw ComponentHandlerFunc which is
// fired when the component with the specified customId has
// been interacted with.
//
// Te returned function unregisters the specified handler
// from the registry but does not remove the added message
// components from the message.
//
// Registering a handler twice on the smae customId
// overwrites the previously registered handler function.
func (t *ComponentHandler) Register(customId string, handler ComponentHandlerFunc) func() {
t.mtx.Lock()
defer t.mtx.Unlock()
t.handlers[customId] = handler
return func() {
t.Unregister(customId)
}
}
// Unregister removes one or more handlers from the
// registry set to the given customId(s) of the
// message component(s).
func (t *ComponentHandler) Unregister(customId ...string) {
if len(customId) == 0 {
return
}
t.mtx.Lock()
defer t.mtx.Unlock()
for _, id := range customId {
delete(t.handlers, id)
}
}
// AppendToMessage edits the given message by messageId and
// channelId which adds the passed message components to
// the message.
func (t *ComponentHandler) AppendToMessage(
messageId string,
channelId string,
components []discordgo.MessageComponent,
) error {
_, err := t.ken.s.ChannelMessageEditComplex(&discordgo.MessageEdit{
ID: messageId,
Channel: channelId,
Components: &components,
})
return err
}
// UnregisterDiscordHandler removes the Discord event handler
// function from the internal DiscordGo Session.
func (t *ComponentHandler) UnregisterDiscordHandler() {
t.unregisterFunc()
}
func (t *ComponentHandler) registerModalHandler(customId string, handler ModalHandlerFunc) func() {
t.mtx.Lock()
defer t.mtx.Unlock()
t.modalHandlers[customId] = func(ctx ModalContext) bool {
ok := handler(ctx)
if ok {
t.unregisterModalhandler(customId)
}
return ok
}
return func() {
t.unregisterModalhandler(customId)
}
}
func (t *ComponentHandler) unregisterModalhandler(customId ...string) {
if len(customId) == 0 {
return
}
t.mtx.Lock()
defer t.mtx.Unlock()
for _, id := range customId {
delete(t.modalHandlers, id)
}
}
func (t *ComponentHandler) handle(_ *discordgo.Session, e *discordgo.InteractionCreate) {
switch e.Type {
case discordgo.InteractionMessageComponent:
t.handleMessageComponent(e)
case discordgo.InteractionModalSubmit:
t.handleModalSubmit(e)
}
}
func (t *ComponentHandler) handleMessageComponent(e *discordgo.InteractionCreate) {
data := e.MessageComponentData()
t.mtx.RLock()
handler, ok := t.handlers[data.CustomID]
t.mtx.RUnlock()
if !ok {
return
}
ctx := t.ctxPool.Get().(*componentCtx)
ctx.Data = data
ctx.ephemeral = false
ctx.event = e
ctx.session = t.ken.s
ctx.ken = t.ken
ctx.responded = false
defer func() {
t.ctxPool.Put(ctx)
}()
handler(ctx)
}
func (t *ComponentHandler) handleModalSubmit(e *discordgo.InteractionCreate) {
data := e.ModalSubmitData()
t.mtx.RLock()
handler, ok := t.modalHandlers[data.CustomID]
t.mtx.RUnlock()
if !ok {
return
}
ctx := t.modalCtxPool.Get().(*modalCtx)
ctx.Data = data
ctx.ephemeral = false
ctx.event = e
ctx.session = t.ken.s
ctx.ken = t.ken
ctx.responded = false
defer func() {
t.modalCtxPool.Put(ctx)
}()
handler(ctx)
}