-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathembed_menus.go
347 lines (304 loc) · 9.77 KB
/
embed_menus.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
package gommand
import (
"fmt"
"strings"
"sync"
"time"
"github.com/andersfylling/disgord"
)
// This is used to represent all of the current menus.
var menuCache = map[disgord.Snowflake]*EmbedMenu{}
// This is the thread lock for the menu cache.
var menuCacheLock = sync.RWMutex{}
// This is used to hold the lifetime information (if applicable) for each active menu.
var menuLifetimeCache = map[disgord.Snowflake]*EmbedLifetimeOptions{}
// This is the thread lock for the lifetimeCache.
var menuLifetimeCacheLock = sync.RWMutex{}
// MenuInfo contains the information about the menu.
type MenuInfo struct {
Author string
Info []string
}
// MenuButton is the datatype containing information about the button.
type MenuButton struct {
Emoji string
Name string
Description string
}
// MenuReaction represents the button and the function which it triggers.
type MenuReaction struct {
Button *MenuButton
Function func(ChannelID, MessageID disgord.Snowflake, _ *EmbedMenu, client disgord.Session)
}
// MenuReactions are all of the reactions which the menu has.
type MenuReactions struct {
ReactionSlice []MenuReaction
}
// EmbedMenu is the base menu.
type EmbedMenu struct {
Reactions *MenuReactions
parent *EmbedMenu
Embed *disgord.Embed
MenuInfo *MenuInfo
myID disgord.Snowflake
}
// Add is used to add a menu reaction.
func (mr *MenuReactions) Add(reaction MenuReaction) {
Slice := append(mr.ReactionSlice, reaction)
mr.ReactionSlice = Slice
}
// AddParentMenu is used to add a new parent menu.
func (e *EmbedMenu) AddParentMenu(Menu *EmbedMenu) {
e.parent = Menu
}
// Display is used to show a menu. This is un-protected so that people can write their own things on top of embed menus, but you probably want to use ctx.DisplayEmbedMenu(menu).
func (e *EmbedMenu) Display(ChannelID, MessageID disgord.Snowflake, client disgord.Session) error {
_ = client.Channel(ChannelID).Message(MessageID).DeleteAllReactions()
menuCacheLock.Lock()
if len(e.Reactions.ReactionSlice) == 0 {
delete(menuCache, MessageID)
} else {
menuCache[MessageID] = e
}
menuCacheLock.Unlock()
EmbedCopy := disgord.DeepCopy(e.Embed).(*disgord.Embed)
Fields := make([]*disgord.EmbedField, 0)
for _, k := range e.Reactions.ReactionSlice {
if k.Button.Name == "" || k.Button.Description == "" {
continue
}
emojiFormatted := k.Button.Emoji
if strings.Contains(k.Button.Emoji, ":") {
if strings.HasPrefix(k.Button.Emoji, "a:") {
emojiFormatted = "<" + emojiFormatted + ">"
} else {
emojiFormatted = "<:" + emojiFormatted + ">"
}
}
Fields = append(Fields, &disgord.EmbedField{
Name: fmt.Sprintf("%s %s", emojiFormatted, k.Button.Name),
Value: k.Button.Description,
Inline: false,
})
}
EmbedCopy.Fields = append(EmbedCopy.Fields, Fields...)
msgRef := client.Channel(ChannelID).Message(MessageID)
_, err := msgRef.Update().SetContent("").SetEmbed(EmbedCopy).Execute()
if err != nil {
return err
}
for _, k := range e.Reactions.ReactionSlice {
err := msgRef.Reaction(k.Button.Emoji).Create()
if err != nil {
return err
}
}
return nil
}
// ChildMenuOptions are options which can be set when creating a child menu.
type ChildMenuOptions struct {
Embed *disgord.Embed `json:"embed"`
Button *MenuButton `json:"button"`
BeforeAction func() `json:"-"`
AfterAction func() `json:"-"`
}
// NewChildMenu is used to create a new child menu.
func (e *EmbedMenu) NewChildMenu(options *ChildMenuOptions) *EmbedMenu {
NewEmbedMenu := &EmbedMenu{
Reactions: &MenuReactions{
ReactionSlice: []MenuReaction{},
},
Embed: options.Embed,
MenuInfo: e.MenuInfo,
myID: e.myID,
}
NewEmbedMenu.parent = e
Reaction := MenuReaction{
Button: options.Button,
Function: func(ChannelID, MessageID disgord.Snowflake, _ *EmbedMenu, client disgord.Session) {
if options.BeforeAction != nil {
options.BeforeAction()
}
_ = NewEmbedMenu.Display(ChannelID, MessageID, client)
if options.AfterAction != nil {
options.AfterAction()
}
},
}
e.Reactions.Add(Reaction)
return NewEmbedMenu
}
// AddBackButton is used to add a back Button to the page.
func (e *EmbedMenu) AddBackButton() {
Reaction := MenuReaction{
Button: &MenuButton{
Description: "Goes back to the parent menu.",
Name: "Back",
Emoji: "⬆",
},
Function: func(ChannelID, MessageID disgord.Snowflake, _ *EmbedMenu, client disgord.Session) {
_ = e.parent.Display(ChannelID, MessageID, client)
},
}
e.Reactions.Add(Reaction)
}
// AddExitButton is used to add an Exit button to the page, deleting the menu if pressed.
func (e *EmbedMenu) AddExitButton() {
Reaction := MenuReaction{
Button: &MenuButton{
Description: "Exits the current menu.",
Name: "Exit",
Emoji: "❌",
},
Function: func(ChannelID, MessageID disgord.Snowflake, _ *EmbedMenu, client disgord.Session) {
_ = client.Channel(ChannelID).Message(MessageID).Delete()
},
}
e.Reactions.Add(Reaction)
}
// NewEmbedMenu is used to create a new menu handler.
func NewEmbedMenu(embed *disgord.Embed, ctx *Context) *EmbedMenu {
var reactions []MenuReaction
menu := &EmbedMenu{
myID: ctx.BotUser.ID,
Reactions: &MenuReactions{
ReactionSlice: reactions,
},
Embed: embed,
MenuInfo: &MenuInfo{
Author: ctx.Message.Author.ID.String(),
Info: []string{},
},
}
return menu
}
// EmbedLifetimeOptions represents the options used to control the lifetime of a menu, all fields are optional.
type EmbedLifetimeOptions struct {
// The maximum time after which an embed is created for which it can exist.
// After this time has passed, if it hasn't already then the menu will be deleted.
MaximumLifetime time.Duration
// The maximum time after an embed was last interacted with (reacted to) that it can exist for.
// After this time has passed, if it hasn't already then the menu will be deleted.
InactiveLifetime time.Duration
// The function called before the menu should be deleted.
BeforeDelete func()
// The function called after the message is deleted (only if deleted through exceeded lifetime).
// Called regardless of errors returned when deleting the message.
AfterDelete func()
maxLifetimeTimer *time.Timer
inactiveTimer *time.Timer
}
// Start inits the timers for the lifetime.
func (l *EmbedLifetimeOptions) Start(ChannelID, MessageID disgord.Snowflake, client disgord.Session) {
if l.MaximumLifetime == time.Duration(0) && l.InactiveLifetime == time.Duration(0) {
// This is blank, don't bother caching / updating it.
return
}
menuLifetimeCacheLock.Lock()
if l.MaximumLifetime > time.Duration(0) {
// init the maxLifetimeTimer if the MaximumLifetime is a positive non-zero value.
l.maxLifetimeTimer = time.AfterFunc(l.MaximumLifetime, func() {
if l.BeforeDelete != nil {
l.BeforeDelete()
}
err := client.Channel(ChannelID).Message(MessageID).Delete()
if err != nil {
// If there was an error deleting the message, remove from the menu cache anyway.
menuCacheLock.Lock()
delete(menuCache, MessageID)
menuCacheLock.Unlock()
menuLifetimeCacheLock.Lock()
delete(menuLifetimeCache, MessageID)
menuLifetimeCacheLock.Unlock()
}
if l.AfterDelete != nil {
l.AfterDelete()
}
})
}
if l.InactiveLifetime > time.Duration(0) {
l.inactiveTimer = time.AfterFunc(l.InactiveLifetime, func() {
if l.BeforeDelete != nil {
l.BeforeDelete()
}
err := client.Channel(ChannelID).Message(MessageID).Delete()
if err != nil {
// If there was an error deleting the message, remove from the menu cache anyway.
menuCacheLock.Lock()
delete(menuCache, MessageID)
menuCacheLock.Unlock()
menuLifetimeCacheLock.Lock()
delete(menuLifetimeCache, MessageID)
menuLifetimeCacheLock.Unlock()
}
if l.AfterDelete != nil {
l.AfterDelete()
}
})
}
menuLifetimeCache[MessageID] = l
menuLifetimeCacheLock.Unlock()
}
// This is used to handle menu reactions.
func handleMenuReactionEdit(s disgord.Session, evt *disgord.MessageReactionAdd) {
go func() {
// Get the menu if it exists.
menuCacheLock.RLock()
menu := menuCache[evt.MessageID]
if menu == nil {
menuCacheLock.RUnlock()
return
}
menuCacheLock.RUnlock()
// Remove the reaction.
if evt.UserID == menu.myID {
// This is by me! Do not delete!
return
}
_ = s.Channel(evt.ChannelID).Message(evt.MessageID).Reaction(evt.PartialEmoji).DeleteUser(evt.UserID)
// Check the author of the reaction.
if menu.MenuInfo.Author != evt.UserID.String() {
return
}
for _, v := range menu.Reactions.ReactionSlice {
standardized := ""
if evt.PartialEmoji.ID == 0 {
standardized = evt.PartialEmoji.Name
} else {
standardized = evt.PartialEmoji.Name + ":" + evt.PartialEmoji.ID.String()
}
// We use HasSuffix here because of the "a:" that might be attached.
if strings.HasSuffix(v.Button.Emoji, standardized) {
menuLifetimeCacheLock.Lock()
if lifetime, ok := menuLifetimeCache[evt.MessageID]; ok && lifetime.inactiveTimer != nil {
if lifetime.inactiveTimer.Stop() {
// Only reset the timer if it's still "active".
_ = lifetime.inactiveTimer.Reset(lifetime.InactiveLifetime)
}
}
menuLifetimeCacheLock.Unlock()
v.Function(evt.ChannelID, evt.MessageID, menu, s)
return
}
}
}()
}
// Handle messages being deleted to stop memory leaks.
func handleEmbedMenuMessageDelete(s disgord.Session, evt *disgord.MessageDelete) {
go func() {
menuCacheLock.Lock()
delete(menuCache, evt.MessageID)
menuCacheLock.Unlock()
menuLifetimeCacheLock.Lock()
if lifetime, ok := menuLifetimeCache[evt.MessageID]; ok {
if lifetime.inactiveTimer != nil {
_ = lifetime.inactiveTimer.Stop()
}
if lifetime.maxLifetimeTimer != nil {
_ = lifetime.maxLifetimeTimer.Stop()
}
delete(menuLifetimeCache, evt.MessageID)
}
menuLifetimeCacheLock.Unlock()
}()
}