-
Notifications
You must be signed in to change notification settings - Fork 927
/
menu.go
781 lines (640 loc) · 21.2 KB
/
menu.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
package rolecommands
import (
"context"
"database/sql"
"emperror.dev/errors"
"fmt"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
"github.com/jonas747/yagpdb/premium"
"github.com/jonas747/yagpdb/rolecommands/models"
"github.com/volatiletech/null"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/queries/qm"
"sort"
"sync"
"time"
)
func cmdFuncRoleMenuCreate(parsed *dcmd.Data) (interface{}, error) {
group, err := models.RoleGroups(qm.Where("guild_id=?", parsed.GS.ID), qm.Where("name ILIKE ?", parsed.Args[0].Str()), qm.Load("RoleCommands")).OneG(parsed.Context())
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return "Did not find the role command group specified, make sure you typed it right, if you haven't set one up yet you can do so in the control panel.", nil
}
return nil, err
}
skipAmount := parsed.Switches["skip"].Int()
cmdsLen := len(group.R.RoleCommands)
if cmdsLen < 1 {
return "No commands in this group, set them up in the control panel.", nil
}
model := &models.RoleMenu{
GuildID: parsed.GS.ID,
OwnerID: parsed.Msg.Author.ID,
ChannelID: parsed.Msg.ChannelID,
RoleGroupID: null.Int64From(group.ID),
OwnMessage: true,
DisableSendDM: parsed.Switches["nodm"].Value != nil && parsed.Switches["nodm"].Value.(bool),
RemoveRoleOnReactionRemove: true,
SkipAmount: skipAmount,
}
if group != nil {
model.RoleGroupID = null.Int64From(group.ID)
}
var msg *discordgo.Message
if parsed.Switches["m"].Value != nil {
model.OwnMessage = false
id := parsed.Switches["m"].Int64()
msg, err = common.BotSession.ChannelMessage(parsed.CS.ID, id)
if err != nil {
return nil, err
}
model.MessageID = id
} else {
// set up the message if not provided
msg, err = common.BotSession.ChannelMessageSend(parsed.CS.ID, "Role menu\nSetting up...")
if err != nil {
_, dErr := common.DiscordError(err)
errStr := "Failed creating the menu message, check the permissions on the channel"
if dErr != "" {
errStr += ", Discord responded with: " + errStr
}
return errStr, err
}
model.MessageID = msg.ID
}
err = model.InsertG(parsed.Context(), boil.Infer())
if err != nil {
if common.ErrPQIsUniqueViolation(err) {
return "There is already a menu on that message, use `rolemenu update ...` to update it", nil
}
return "Failed setting up menu", err
}
model.R = model.R.NewStruct()
model.R.RoleGroup = group
resp, err := NextRoleMenuSetupStep(parsed.Context(), model, true)
updateSetupMessage(parsed.Context(), model, resp)
return nil, err
}
func cmdFuncRoleMenuUpdate(parsed *dcmd.Data) (interface{}, error) {
mID := parsed.Args[0].Int64()
menu, err := FindRolemenuFull(parsed.Context(), mID, parsed.GS.ID)
if err != nil {
return "Couldn't find menu", nil
}
return UpdateMenu(parsed, menu)
}
func UpdateMenu(parsed *dcmd.Data, menu *models.RoleMenu) (interface{}, error) {
if menu.State == RoleMenuStateSettingUp {
return "Already setting this menu up", nil
}
menu.State = RoleMenuStateSettingUp
if parsed.Switches["nodm"].Value != nil && parsed.Switches["nodm"].Value.(bool) {
menu.DisableSendDM = !menu.DisableSendDM
}
if parsed.Switches["rr"].Value != nil && parsed.Switches["rr"].Value.(bool) {
menu.RemoveRoleOnReactionRemove = !menu.RemoveRoleOnReactionRemove
}
// don't reuse the old setup message
menu.SetupMSGID = 0
menu.OwnerID = parsed.Msg.Author.ID
menu.UpdateG(parsed.Context(), boil.Infer())
if menu.OwnMessage {
UpdateRoleMenuMessage(parsed.Context(), menu)
}
// Add all mising options
resp, err := NextRoleMenuSetupStep(parsed.Context(), menu, false)
if resp != "" {
createSetupMessage(parsed.Context(), menu, resp, true)
}
return nil, err
}
func NextRoleMenuSetupStep(ctx context.Context, rm *models.RoleMenu, first bool) (resp string, err error) {
commands := rm.R.RoleGroup.R.RoleCommands
sort.Slice(commands, RoleCommandsLessFunc(commands))
var nextCmd *models.RoleCommand
numDone := 0
OUTER:
for i, cmd := range commands {
if i < rm.SkipAmount {
continue
}
for _, option := range rm.R.RoleMenuOptions {
if cmd.ID == option.RoleCommandID.Int64 {
numDone++
continue OUTER
}
}
// New command is cmd
if nextCmd == nil {
nextCmd = cmd
}
}
if nextCmd == nil || len(rm.R.RoleMenuOptions) >= 20 {
extra := ""
if len(rm.R.RoleMenuOptions) >= 20 && nextCmd != nil {
extra = fmt.Sprintf("\n\nMessages can contain max 20 reactions, couldn't fit them all into this one, you can add the remaining to another menu using `rolemenu create %s -skip %d`", rm.R.RoleGroup.Name, rm.SkipAmount+20)
rm.FixedAmount = true
}
rm.State = RoleMenuStateDone
rm.UpdateG(ctx, boil.Infer())
flagHelp := StrFlags(rm)
return fmt.Sprintf("Done setting up! You can delete all the messages now (except for the menu itself)\n\nFlags:\n%s%s", flagHelp, extra), nil
}
rm.NextRoleCommandID = null.Int64From(nextCmd.ID)
rm.UpdateG(ctx, boil.Whitelist(models.RoleMenuColumns.NextRoleCommandID))
totalCommands := len(commands) - rm.SkipAmount
resp = fmt.Sprintf("[%d/%d]\n", numDone, totalCommands)
return resp + "React with the emoji for the role command: `" + nextCmd.Name + "`\nNote: The bot has to be on the server where the emoji is from, otherwise it won't be able to use it", nil
}
func StrFlags(rm *models.RoleMenu) string {
nodmFlagHelp := fmt.Sprintf("`-nodm: %t` toggle with `rolemenu update -nodm %d`: disables dm messages.", rm.DisableSendDM, rm.MessageID)
rrFlagHelp := fmt.Sprintf("`-rr: %t` toggle with `rolemenu update -rr %d`: removing reactions removes the role.", rm.RemoveRoleOnReactionRemove, rm.MessageID)
return nodmFlagHelp + "\n" + rrFlagHelp
}
func UpdateRoleMenuMessage(ctx context.Context, rm *models.RoleMenu) error {
newMsg := "**Role Menu: " + rm.R.RoleGroup.Name + "**\nReact to give yourself a role.\n\n"
opts := rm.R.RoleMenuOptions
sort.Slice(opts, OptionsLessFunc(opts))
for _, opt := range opts {
cmd := opt.R.RoleCommand
emoji := opt.UnicodeEmoji
if opt.EmojiID != 0 {
if opt.EmojiAnimated {
emoji = fmt.Sprintf("<a:yagpdb:%d>", opt.EmojiID)
} else {
emoji = fmt.Sprintf("<:yagpdb:%d>", opt.EmojiID)
}
}
newMsg += fmt.Sprintf("%s : `%s`\n\n", emoji, cmd.Name)
}
_, err := common.BotSession.ChannelMessageEdit(rm.ChannelID, rm.MessageID, newMsg)
return err
}
func ContinueRoleMenuSetup(ctx context.Context, rm *models.RoleMenu, emoji *discordgo.Emoji, userID int64) (resp string, err error) {
if userID != rm.OwnerID {
common.BotSession.MessageReactionRemove(rm.ChannelID, rm.MessageID, emoji.APIName(), userID)
return "This menu is still being set up, wait until the owner of this menu is done.", nil
}
// next role command id can be null if the relevant role command was deleted during setup
if rm.NextRoleCommandID.Valid {
currentOpts := rm.R.RoleMenuOptions
// Make sure this emoji isnt used to another option
for _, option := range currentOpts {
if emoji.ID != 0 {
if emoji.ID == option.EmojiID && option.EmojiID != 0 {
return "Emoji already used for another option", nil
}
} else {
if emoji.Name == option.UnicodeEmoji && option.UnicodeEmoji != "" {
return "Emoji already used for another option", nil
}
}
}
model := &models.RoleMenuOption{
RoleMenuID: rm.MessageID,
RoleCommandID: rm.NextRoleCommandID,
EmojiID: emoji.ID,
EmojiAnimated: emoji.Animated,
}
if emoji.ID == 0 {
model.UnicodeEmoji = emoji.Name
}
err = model.InsertG(ctx, boil.Infer())
if err != nil {
return "Failed inserting option", err
}
err = common.BotSession.MessageReactionAdd(rm.ChannelID, rm.MessageID, emoji.APIName())
if err != nil {
logger.WithError(err).WithField("emoji", emoji.APIName()).Error("Failed reacting")
}
model.R = model.R.NewStruct()
for _, cmd := range rm.R.RoleGroup.R.RoleCommands {
if cmd.ID == model.RoleCommandID.Int64 {
model.R.RoleCommand = cmd
break
}
}
rm.R.RoleMenuOptions = append(rm.R.RoleMenuOptions, model)
if rm.OwnMessage {
err = UpdateRoleMenuMessage(ctx, rm)
if err != nil {
return "Failed updating message", err
}
}
}
// return rm.NextSetupStep(false)
return NextRoleMenuSetupStep(ctx, rm, false)
}
func getReactionDetails(evt *eventsystem.EventData) (emoji *discordgo.Emoji, cID, gID, uID, mID int64, add bool) {
if evt.Type == eventsystem.EventMessageReactionAdd {
ra := evt.MessageReactionAdd()
cID = ra.ChannelID
uID = ra.UserID
gID = ra.GuildID
mID = ra.MessageID
emoji = &ra.Emoji
add = true
} else {
rr := evt.MessageReactionRemove()
cID = rr.ChannelID
uID = rr.UserID
gID = rr.GuildID
mID = rr.MessageID
emoji = &rr.Emoji
}
return
}
func findOptionFromEmoji(emoji *discordgo.Emoji, opts []*models.RoleMenuOption) *models.RoleMenuOption {
if emoji.ID != 0 {
// This is a custom emoji
for _, v := range opts {
if v.EmojiID != 0 && v.EmojiID == emoji.ID {
return v
}
}
} else {
// Unicode emoji
for _, v := range opts {
if v.UnicodeEmoji == emoji.Name && v.EmojiID == 0 {
return v
}
}
}
return nil
}
func handleReactionAddRemove(evt *eventsystem.EventData) {
emoji, _, gID, uID, mID, raAdd := getReactionDetails(evt)
if uID == common.BotUser.ID {
return
}
menu, err := FindRolemenuFull(evt.Context(), mID, gID)
if err != nil {
if err != sql.ErrNoRows {
logger.WithError(err).Error("RoleCommandsMenu: Failed finding menu")
}
return
}
// Continue setup if were doing that
if menu.State != RoleMenuStateDone {
if !raAdd {
// ignore reaction removes in this state
return
}
resp, err := MenuReactedNotDone(evt.Context(), menu, emoji, uID)
if err != nil {
logger.WithError(err).Error("RoleCommandsMenu: Failed continuing role menu setup, or editing menu")
}
if resp != "" {
if err != nil {
createSetupMessage(evt.Context(), menu, resp, false)
} else {
updateSetupMessage(evt.Context(), menu, resp)
}
}
return
}
if !menu.RemoveRoleOnReactionRemove && !raAdd {
return // only go further is this flag is enabled
}
if mID != menu.MessageID {
return // reacted on the seutp message id, only allow setup actions there
}
// Find the option model from the reaction
option := findOptionFromEmoji(emoji, menu.R.RoleMenuOptions)
if option == nil {
return
}
gs := evt.GS
gs.RLock()
name := gs.Guild.Name
gs.RUnlock()
resp, err := MemberChooseOption(evt.Context(), menu, gs, option, uID, emoji, raAdd)
if err != nil && !common.IsDiscordErr(err, discordgo.ErrCodeUnknownRole, discordgo.ErrCodeMissingPermissions) {
logger.WithError(err).WithField("option", option.ID).WithField("guild", menu.GuildID).Error("Failed applying role from menu")
}
if resp != "" {
bot.SendDM(uID, "**"+name+"**: "+resp)
}
}
func MemberChooseOption(ctx context.Context, rm *models.RoleMenu, gs *dstate.GuildState, option *models.RoleMenuOption, userID int64, emoji *discordgo.Emoji, raAdd bool) (resp string, err error) {
cmd := option.R.RoleCommand
cmd.R.RoleGroup = rm.R.RoleGroup
member, err := bot.GetMember(gs.ID, userID)
if err != nil {
if common.IsDiscordErr(err, discordgo.ErrCodeUnknownMember) {
return "", nil
}
return "An error occured giving you the role", err
}
if member.Bot {
// ignore bots
return "", nil
}
var given bool
if rm.RemoveRoleOnReactionRemove {
// Strictly assign or remove based on wether the reaction was added or removed
if raAdd {
given, err = AssignRole(ctx, member, cmd)
if err == nil && given {
resp = "Gave you the role!"
}
} else {
var removed bool
removed, err = RemoveRole(ctx, member, cmd)
if err == nil && removed {
resp = "Took away the role!"
}
}
} else {
// Just toggle...
given, err = CheckToggleRole(ctx, member, cmd)
if err == nil {
if given {
resp = "Gave you the role!"
} else {
resp = "Took away the role!"
}
}
}
if rm.DisableSendDM {
resp = ""
}
if err != nil {
if raAdd {
common.BotSession.MessageReactionRemove(rm.ChannelID, rm.MessageID, emoji.APIName(), userID)
}
resp, err = HumanizeAssignError(gs, err)
} else if rm.R.RoleGroup.Mode == GroupModeSingle && given {
go removeOtherReactions(rm, option, userID)
}
if resp != "" {
resp = cmd.Name + ": " + resp
}
return
}
// track reaction removal loop so that we can cancel them
type reactionRemovalOccurence struct {
MessageID int64
UserID int64
Stopmu sync.Mutex
Stop bool
}
var (
activeReactionRemovals = make([]*reactionRemovalOccurence, 0)
activeReactionRemovalsmu sync.Mutex
confDisableReactionRemovalSingleMode = config.RegisterOption("yagpdb.rolecommands.disable_reaction_removal_single_mode", "Disable reaction removal in single mode, could be heavy on number of requests", false)
)
func removeOtherReactions(rm *models.RoleMenu, option *models.RoleMenuOption, userID int64) {
if confDisableReactionRemovalSingleMode.GetBool() {
// since this is an experimental feature
return
}
isPremium, err := premium.IsGuildPremiumCached(rm.GuildID)
if err != nil {
logger.WithError(err).WithField("guild", rm.GuildID).Error("Failed checking if guild is premium")
return
}
if !isPremium {
return
}
activeReactionRemovalsmu.Lock()
// cancel existing ones
for _, v := range activeReactionRemovals {
if v.MessageID == rm.MessageID && v.UserID == userID {
v.Stopmu.Lock()
v.Stop = true
v.Stopmu.Unlock()
}
}
// add the new one
cur := &reactionRemovalOccurence{
MessageID: rm.MessageID,
UserID: userID,
}
activeReactionRemovals = append(activeReactionRemovals, cur)
activeReactionRemovalsmu.Unlock()
// make sure to remove it when we return
defer func() {
activeReactionRemovalsmu.Lock()
for i, v := range activeReactionRemovals {
if v == cur {
activeReactionRemovals = append(activeReactionRemovals[:i], activeReactionRemovals[i+1:]...)
break
}
}
activeReactionRemovalsmu.Unlock()
}()
// actually start the reaction removal process
for _, v := range rm.R.RoleMenuOptions {
if v.ID == option.ID {
continue
}
// check if we were cancelled
cur.Stopmu.Lock()
stop := cur.Stop
cur.Stopmu.Unlock()
if stop {
break
}
emoji := v.UnicodeEmoji
if v.EmojiID != 0 {
emoji = "aaa:" + discordgo.StrID(v.EmojiID)
}
common.BotSession.MessageReactionRemove(rm.ChannelID, rm.MessageID, emoji, userID)
}
}
func OptionsLessFunc(slice []*models.RoleMenuOption) func(int, int) bool {
return func(i, j int) bool {
// Compare timestamps if positions are equal, for deterministic output
if slice[i].R.RoleCommand.Position == slice[j].R.RoleCommand.Position {
return slice[i].R.RoleCommand.CreatedAt.After(slice[j].R.RoleCommand.CreatedAt)
}
if slice[i].R.RoleCommand.Position > slice[j].R.RoleCommand.Position {
return false
}
return true
}
}
func handleMessageRemove(evt *eventsystem.EventData) {
if evt.Type == eventsystem.EventMessageDelete {
messageRemoved(evt.Context(), evt.MessageDelete().Message.ID)
} else if evt.Type == eventsystem.EventMessageDeleteBulk {
for _, v := range evt.MessageDeleteBulk().Messages {
messageRemoved(evt.Context(), v)
}
}
}
func messageRemoved(ctx context.Context, id int64) {
_, err := models.RoleMenus(qm.Where("message_id=?", id)).DeleteAll(ctx, common.PQ)
if err != nil {
logger.WithError(err).Error("Failed removing old role menus")
}
}
func FindRolemenuFull(ctx context.Context, mID int64, guildID int64) (*models.RoleMenu, error) {
return models.RoleMenus(qm.Where("guild_id = ? AND (message_id = ? OR setup_msg_id = ?)", guildID, mID, mID), qm.Load("RoleMenuOptions.RoleCommand"), qm.Load("RoleGroup.RoleCommands")).OneG(ctx)
}
func cmdFuncRoleMenuResetReactions(data *dcmd.Data) (interface{}, error) {
mID := data.Args[0].Int64()
menu, err := FindRolemenuFull(data.Context(), mID, data.GS.ID)
if err != nil {
return "Couldn't find menu", nil
}
err = common.BotSession.MessageReactionsRemoveAll(menu.ChannelID, menu.MessageID)
if err != nil {
return nil, err
}
sort.Slice(menu.R.RoleMenuOptions, OptionsLessFunc(menu.R.RoleMenuOptions))
for _, option := range menu.R.RoleMenuOptions {
emoji := option.UnicodeEmoji
if option.EmojiID != 0 {
emoji = "aaa:" + discordgo.StrID(option.EmojiID)
}
err := common.BotSession.MessageReactionAdd(menu.ChannelID, menu.MessageID, emoji)
if err != nil {
return nil, err
}
}
return nil, nil
}
func cmdFuncRoleMenuRemove(data *dcmd.Data) (interface{}, error) {
mID := data.Args[0].Int64()
menu, err := FindRolemenuFull(data.Context(), mID, data.GS.ID)
if err != nil {
return "Couldn't find menu", nil
}
_, err = menu.DeleteG(data.Context())
if err != nil {
return nil, err
}
return "Deleted. The bot will no longer listen for reactions on this message, you can even make another menu on it.", nil
}
func cmdFuncRoleMenuEditOption(data *dcmd.Data) (interface{}, error) {
mID := data.Args[0].Int64()
menu, err := FindRolemenuFull(data.Context(), mID, data.GS.ID)
if err != nil {
return "Couldn't find menu", nil
}
if menu.State != RoleMenuStateDone {
return "This menu isn't 'done' (still being edited, or made)", nil
}
menu.State = RoleMenuStateEditingOptionSelecting
menu.OwnerID = data.Msg.Author.ID
menu.SetupMSGID = 0
_, err = menu.UpdateG(data.Context(), boil.Whitelist("state", "owner_id", "setup_msg_id"))
if err != nil {
return "", err
}
createSetupMessage(data.Context(), menu, "React on the emoji for the option you want to change", true)
return nil, nil
}
func cmdFuncRoleMenuComplete(data *dcmd.Data) (interface{}, error) {
mID := data.Args[0].Int64()
menu, err := FindRolemenuFull(data.Context(), mID, data.GS.ID)
if err != nil {
return "Couldn't find menu", nil
}
if menu.State == RoleMenuStateDone {
return "This menu is already marked as done", nil
}
menu.State = RoleMenuStateDone
menu.SetupMSGID = 0
_, err = menu.UpdateG(data.Context(), boil.Whitelist("state", "setup_msg_id"))
if err != nil {
return nil, err
}
return "Menu marked as done", nil
}
func MenuReactedNotDone(ctx context.Context, rm *models.RoleMenu, emoji *discordgo.Emoji, userID int64) (resp string, err error) {
if userID != rm.OwnerID {
return "Someone is currently editing or setting up this menu, please wait", nil
}
switch rm.State {
case RoleMenuStateSettingUp:
return ContinueRoleMenuSetup(ctx, rm, emoji, userID)
case RoleMenuStateEditingOptionSelecting:
option := findOptionFromEmoji(emoji, rm.R.RoleMenuOptions)
if option == nil {
return "", nil
}
rm.State = RoleMenuStateEditingOptionReplacing
rm.EditingOptionID = null.Int64From(option.ID)
_, err := rm.UpdateG(ctx, boil.Whitelist("state", "editing_option_id"))
if err != nil {
return "", err
}
return "Editing `" + option.R.RoleCommand.Name + "`, select the new emoji for it", nil
case RoleMenuStateEditingOptionReplacing:
option, err := rm.EditingOption().OneG(ctx)
if err != nil {
// possible they might have deleted the role in the meantime, so set it to done to prevent a deadlock for this menu
rm.State = RoleMenuStateDone
rm.UpdateG(ctx, boil.Whitelist("state"))
return "", err
}
option.EmojiID = emoji.ID
option.EmojiAnimated = emoji.Animated
if option.EmojiID == 0 {
option.UnicodeEmoji = emoji.Name
} else {
option.UnicodeEmoji = ""
}
_, err = option.UpdateG(ctx, boil.Infer())
if err != nil {
return "", err
}
rm.State = RoleMenuStateDone
rm.UpdateG(ctx, boil.Whitelist("state"))
go common.BotSession.MessageReactionAdd(rm.ChannelID, rm.MessageID, emoji.APIName())
if rm.OwnMessage {
for _, v := range rm.R.RoleMenuOptions {
if v.ID == option.ID {
v.EmojiAnimated = option.EmojiAnimated
v.EmojiID = option.EmojiID
v.UnicodeEmoji = option.UnicodeEmoji
}
}
UpdateRoleMenuMessage(ctx, rm)
}
return fmt.Sprintf("Sucessfully edited menu, tip: run `rolemenu resetreactions %d` to clear all reactions so that the order is fixed.", rm.MessageID), nil
}
return "", nil
}
func updateSetupMessage(ctx context.Context, rm *models.RoleMenu, msgContents string) {
msgContents = msgContents + "\n\n*This message will be updated with new info throughout the setup.*"
if rm.SetupMSGID == 0 {
createSetupMessage(ctx, rm, msgContents, true)
return
}
// if this is a old message, then don't reuse it
msgAge := bot.SnowflakeToTime(rm.SetupMSGID)
if msgAge.Before(time.Now().Add(-time.Hour)) {
createSetupMessage(ctx, rm, msgContents, true)
return
}
_, err := common.BotSession.ChannelMessageEdit(rm.ChannelID, rm.SetupMSGID, msgContents)
if err != nil {
createSetupMessage(ctx, rm, msgContents, true)
return
}
}
func createSetupMessage(ctx context.Context, rm *models.RoleMenu, msgContents string, updateModel bool) {
msgContents = "**Rolemenu setup:** " + msgContents
msg, err := common.BotSession.ChannelMessageSend(rm.ChannelID, msgContents)
if err != nil {
logger.WithError(err).WithField("rm_id", rm.MessageID).WithField("guild", rm.GuildID).Error("failed creating setup message for menu")
return
}
if updateModel {
rm.SetupMSGID = msg.ID
_, err = rm.UpdateG(ctx, boil.Whitelist("setup_msg_id"))
if err != nil {
logger.WithError(err).WithField("rm_id", rm.MessageID).WithField("guild", rm.GuildID).Error("failed upating menu model")
}
}
}