-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions.go
1115 lines (1005 loc) · 36 KB
/
interactions.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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/armon/go-metrics"
"github.com/bwmarrin/discordgo"
"github.com/gocarina/gocsv"
"go.uber.org/zap"
)
var (
// A map of handlers for Discord Interactions. There should be a handler for
// every static command. Keys should not be removed until after the 1-hour
// grace period following changes to the bot's ApplicationCommand lists.
handlers = map[string]func(ctx context.Context){
"roll": RollInteractionCreate,
"secret": RollInteractionCreateEphemeral,
"private": RollInteractionCreatePrivate,
"help": func(ctx context.Context) {
s, i, _ := FromContext(ctx)
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Embeds: []*discordgo.MessageEmbed{
makeEmbedHelp(),
},
},
}); err != nil {
if err := MeasureInteractionRespond(s.InteractionRespond, i,
newEphemeralResponse("Something went wrong!")); err != nil {
logger.Error("error sending response", zap.Error(err))
}
return
}
},
"info": func(ctx context.Context) {
s, i, _ := FromContext(ctx)
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Embeds: []*discordgo.MessageEmbed{
makeEmbedInfo(),
},
},
}); err != nil {
logger.Error("error sending info", zap.Error(err))
if err := MeasureInteractionRespond(s.InteractionRespond, i,
newEphemeralResponse("Something went wrong!"),
); err != nil {
logger.Error("error sending response", zap.Error(err))
}
return
}
},
"invite": InviteInteraction,
"expressions": ExpressionsInteraction,
"buttons": ButtonsInteraction,
"ping": PingInteraction,
"clear": ClearInteraction,
"preferences": PreferencesInteraction,
"settings": SettingsInteraction,
// home-server commands
"health": HealthInteraction,
"stats": StatsInteraction,
"golemancy": InteractionGolemancy,
"debug": DebugInteraction,
// message commands
"Roll Message": RollMessageInteractionCreate,
// "Roll Message (Secret)":
// "Roll Message (Private)":
"Save Expression": SaveRollInteractionCreate,
}
suggesters = map[string]func(ctx context.Context){
"roll:expression": SuggestRollsByString,
"roll:label": SuggestLabel,
"secret:expression": SuggestRollsByString,
"secret:label": SuggestLabel,
"private:expression": SuggestRollsByString,
"private:label": SuggestLabel,
"expressions save:expression": SuggestExpressions,
"expressions save:label": SuggestLabel,
"expressions save:name": SuggestNames,
"expressions unsave:expression": SuggestNames,
}
)
func MergeApplicationCommandOptions(optionSets ...[]*discordgo.ApplicationCommandOption) []*discordgo.ApplicationCommandOption {
var newOpts = []*discordgo.ApplicationCommandOption{}
for _, optionSet := range optionSets {
newOpts = append(newOpts, optionSet...)
}
return newOpts
}
// RollInteractionCreate is the method evaluated against a chat command to roll
// dice.
func RollInteractionCreate(ctx context.Context) {
s, i, _ := FromContext(ctx)
logger.Info("interaction", zap.String("id", i.ID), zap.Int("shard", s.ShardID))
logger.Debug("interaction data", zap.Any("data", i.ApplicationCommandData()))
options := i.ApplicationCommandData().Options
// forward to other roll interaction handlers if certain options are set
if optPrivate := getOptionByName(options, "private"); optPrivate != nil && optPrivate.BoolValue() {
RollInteractionCreatePrivate(ctx)
return // short circuit
} else if optSecret := getOptionByName(options, "secret"); optSecret != nil && optSecret.BoolValue() {
RollInteractionCreateEphemeral(ctx)
return // short circuit
}
// count regular roll
defer metrics.IncrCounter([]string{"roll", "basic"}, 1)
rollData, response, rollErr := NewRollInteractionResponseFromInteraction(ctx)
if response == nil {
return
}
user := UserFromInteraction(i)
if rollErr == nil && len(rollData.Dice) > 0 {
roll := &NamedRollInput{
Expression: rollData.Expression,
Label: rollData.Label,
}
// defer cacheRollInput(s, i, roll)
defer CacheRoll(user, roll)
}
// TODO: check forwarding configuration
if err := MeasureInteractionRespond(s.InteractionRespond, i, response); err != nil {
logger.Error("roll interaction error", zap.Error(err))
}
}
// RollInteractionCreateEphemeral is the method evaluated against an interaction to roll
// dice.
func RollInteractionCreateEphemeral(ctx context.Context) {
s, i, _ := FromContext(ctx)
logger.Info("interaction", zap.String("id", i.ID), zap.Int("shard", s.ShardID))
logger.Debug("interaction data", zap.Any("data", i.ApplicationCommandData()))
rollData, response, rollErr := NewRollInteractionResponseFromInteraction(ctx)
if response == nil {
return
}
user := UserFromInteraction(i)
if rollErr == nil {
roll := &NamedRollInput{
Expression: rollData.Expression,
Label: rollData.Label,
}
defer CacheRoll(user, roll)
}
// count secret/ephemeral roll
defer metrics.IncrCounter([]string{"roll", "ephemeral"}, 1)
// Tweak the InteractionResponse to be ephemeral
response.Data.Flags = discordgo.MessageFlagsEphemeral
if err := MeasureInteractionRespond(s.InteractionRespond, i, response); err != nil {
logger.Error("error sending response", zap.Error(err))
return
}
}
// RollInteractionCreatePrivate is the method evaluated against an interaction
// to roll dice but to DM the user the result.
func RollInteractionCreatePrivate(ctx context.Context) {
s, i, _ := FromContext(ctx)
logger.Info("interaction", zap.String("id", i.ID), zap.Int("shard", s.ShardID))
logger.Debug("interaction data", zap.Any("data", i.ApplicationCommandData()))
uid := UserFromInteraction(i).ID
rollData, response, rollErr := NewRollInteractionResponseFromInteraction(ctx)
if response == nil {
return
}
user := UserFromInteraction(i)
if rollErr == nil {
roll := &NamedRollInput{
Expression: rollData.Expression,
Label: rollData.Label,
}
defer CacheRoll(user, roll)
}
// TODO: if already in a DM, respond as a plain interaction
// create a DM channel, but since we can't respond as an interaction across
// channels convert the response to a regular message
c, _ := s.UserChannelCreate(uid)
m := newMessageSendFromInteractionResponse(response)
_, err := s.ChannelMessageSendComplex(c.ID, m)
if err != nil {
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse(ErrDMError.Error()))
return
}
// count private roll
defer metrics.IncrCounter([]string{"roll", "private"}, 1)
if err := MeasureInteractionRespond(s.InteractionRespond, i,
newEphemeralResponse("Sent you a DM!")); err != nil {
logger.Error("error sending response", zap.Error(err))
}
}
// RollMessageInteractionCreate is called by interaction to roll a message's
// content using the 'Apps' context option. The cache should be checked to
// determine if a message's associated input expression is already available.
func RollMessageInteractionCreate(ctx context.Context) {
s, i, _ := FromContext(ctx)
targetMessage := i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID]
defer metrics.IncrCounter([]string{"roll_message"}, 1)
logger.Info("interaction", zap.String("id", i.ID), zap.String("target", targetMessage.ID))
logger.Debug("interaction data", zap.Any("data", i.ApplicationCommandData()))
// the expression to roll
input := targetMessage.Content
// TODO: clean up input/extract roll from between accents, etc.
rollData, interactionResponse, err := NewRollInteractionResponseFromStringWithContext(ctx, input)
if interactionResponse == nil {
return
}
user := UserFromInteraction(i)
if err == nil && len(rollData.Dice) > 0 {
roll := &NamedRollInput{
Expression: rollData.Expression,
Label: rollData.Label,
}
// defer cacheRollInput(s, i, roll)
defer CacheRoll(user, roll)
}
if resErr := MeasureInteractionRespond(s.InteractionRespond, i, interactionResponse); resErr != nil {
zap.Error(resErr)
return
}
}
func SaveRollInteractionCreate(ctx context.Context) {
s, i, _ := FromContext(ctx)
defer metrics.IncrCounter([]string{"interaction", "save_message"}, 1)
targetMessage := i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID]
logger.Debug("interaction data", zap.Any("data", i.ApplicationCommandData()))
// the expression to roll
input := targetMessage.Content
seed := NewRollInputFromString(input)
modal := makeSaveExpressionModal(seed)
if err := MeasureInteractionRespond(s.InteractionRespond, i, modal); err != nil {
logger.Error("modal send", zap.Error(err))
}
}
// NewRollInteractionResponseFromInteraction is the method evaluated against an
// Interaction to roll dice and create the basic response object.
func NewRollInteractionResponseFromInteraction(ctx context.Context) (*Response, *discordgo.InteractionResponse, error) {
_, i, _ := FromContext(ctx)
options := i.ApplicationCommandData().Options
expression := options[0].StringValue()
input := NewRollInputFromString(expression)
// check if we entered with a no-op expression
if input.Expression == "" {
return nil, nil, nil
}
message, response, err := NewRollInteractionResponseFromStringWithContext(ctx, input.RollableString())
if err != nil {
return message, response, err
}
detailed := HasPreference(UserFromInteraction(i), SettingDetailed)
optDetailed := getOptionByName(options, "detailed")
if optDetailed != nil {
detailed = optDetailed.BoolValue()
}
if detailed {
response.Data.Embeds = MessageEmbeds(ctx, &RollLog{
Entries: []*Response{message},
})
}
return message, response, err
}
// NewRollInteractionResponseFromStringWithContext creates an Interaction
// response and roll response. If an error occurred the error will be returned,
// but the returned InteractionResponse will be an error message response to be
// sent back to Discord.
func NewRollInteractionResponseFromStringWithContext(ctx context.Context, expression string) (*Response, *discordgo.InteractionResponse, error) {
s, i, _ := FromContext(ctx)
if s == nil || i == nil {
panic("context data missing")
}
// add expression to context
roll := NewRollInputFromString(expression)
ctx = context.WithValue(ctx, KeyRollInput, roll)
// check for excessive dice
if excessiveDice(ctx) {
return nil, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral, // ephemeral
Content: createFriendlyError(ErrTooManyDice).Error(),
},
}, ErrTooManyDice
}
options := i.ApplicationCommandData().Options
// if a Slash command, check for a label
if i.Type == discordgo.InteractionApplicationCommand {
optLabel := getOptionByName(options, "label")
if optLabel != nil {
roll.Label = optLabel.StringValue()
}
}
message, err := EvaluateRollInputWithContext(ctx, roll)
if err != nil {
// TODO: better error handling
logger.Info("error response", zap.String("msg", createFriendlyError(err).Error()))
return nil, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral, // ephemeral
Content: createFriendlyError(err).Error(),
},
}, err
}
// mentionableUserIDs := []string{}
if i.Member != nil {
// add user's name if roll is shared to a guild channel
if isRollPublic(i) {
message.Name = UserFromInteraction(i).Mention()
}
// allow mentioning only the user that requested the roll even if others
// are @mentioned (ex. '/roll expression:"3d6" label:"vs @travis' AC"')
// mentionableUserIDs = append(mentionableUserIDs, UserFromInteraction(i).ID)
}
// build the message content using a template
logger.Debug("rendering response", zap.Any("message", message))
var text strings.Builder
responseResultTemplateCompiled.Execute(&text, message)
response := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: text.String(),
AllowedMentions: &discordgo.MessageAllowedMentions{
Users: []string{},
// Users: mentionableUserIDs,
},
},
}
// get user's default preference
detailed := HasPreference(UserFromInteraction(i), SettingDetailed)
if optDetailed := getOptionByName(options, "detailed"); optDetailed != nil {
detailed = optDetailed.BoolValue()
}
if detailed {
response.Data.Embeds = MessageEmbeds(ctx, &RollLog{
Entries: []*Response{message},
})
}
return message, response, nil
}
// NewRollMessageResponse is a wrapper for NewRollMessageResponseFromString that
// uses the message's Content.
func NewMessageResponseFromMessage(ctx context.Context, m *discordgo.Message) (*Response, *discordgo.MessageSend, error) {
return NewRollMessageResponseFromString(ctx, m.Content)
}
// NewRollMessageResponseFromString takes a message's content, lints it, and
// evaluates it as a roll. A bot response and Discord message to send as the
// response to the roll will be returned.
func NewRollMessageResponseFromString(ctx context.Context, content string) (*Response, *discordgo.MessageSend, error) {
if content == "" {
return nil, nil, nil
}
_, i, m := FromContext(ctx)
// strip out bot mentions and clean the roll up
content = strings.NewReplacer(
"<@"+DiceGolem.SelfID+">", "",
"<@!"+DiceGolem.SelfID+">", "",
).Replace(content)
input := strings.TrimSpace(content)
roll := NewRollInputFromString(input)
// if message is empty, do nothing
if roll.Expression == "" {
return nil, nil, nil
}
// add roll to context
ctx = context.WithValue(ctx, KeyRollInput, roll)
logger.Debug("data", zap.String("content", content), zap.Any("roll", roll))
res, err := EvaluateRollInputWithContext(ctx, roll)
if err != nil {
return res, &discordgo.MessageSend{
Content: createFriendlyError(err).Error(),
Reference: &discordgo.MessageReference{
MessageID: m.ID,
ChannelID: m.ChannelID,
},
}, err
}
var user *discordgo.User
// if in a guild @mention the user
if m != nil && m.Author != nil && m.GuildID != "" {
user = m.Author
res.Name = user.Mention()
} else if m != nil {
// if in a DM skip the user mention/res.Name
user = UserFromMessage(m)
} else if i != nil {
user = UserFromInteraction(i)
if i.GuildID != "" {
res.Name = user.Mention()
}
}
var text strings.Builder
executeResponseTemplate(&text, res)
message := &discordgo.MessageSend{
Content: text.String(),
AllowedMentions: &discordgo.MessageAllowedMentions{
Users: []string{},
},
}
if HasPreference(user, SettingDetailed) {
message.Embeds = MessageEmbeds(ctx, &RollLog{
Entries: []*Response{res},
})
}
return res, message, nil
}
// PingInteraction is the handler for checking the bot's rount-trip time with
// Discord.
func PingInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
start := time.Now()
// ACK the ping
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
},
}); err != nil {
logger.Error("ping", zap.Error(err))
}
// measure time to ACK write
done := time.Now()
up := done.Sub(start)
// get message
var m *discordgo.Message
var err error
if m, err = s.InteractionResponseEdit(i, &discordgo.WebhookEdit{}); err != nil {
logger.Error("ping", zap.Error(err))
}
// measure GET time
fetched := time.Now()
down := fetched.Sub(done)
logger.Debug("response message", zap.Any("message", m))
avg := (up + down) / 2
embeds := []*discordgo.MessageEmbed{
{
Fields: []*discordgo.MessageEmbedField{
{
Name: "Heartbeat",
Value: s.HeartbeatLatency().Round(time.Millisecond).String(),
Inline: true,
},
{
Name: "API",
Value: fmt.Sprintf("%s (%s ↑, %s ↓)",
avg.Round(time.Millisecond).String(),
up.Round(time.Millisecond).String(),
down.Round(time.Millisecond).String()),
Inline: true,
},
{
Name: "Shard",
Value: strconv.Itoa(s.ShardID),
Inline: true,
},
},
Footer: makeEmbedFooter(),
},
}
if _, err := s.InteractionResponseEdit(i, &discordgo.WebhookEdit{
Content: Ptr(" "),
Embeds: &embeds,
}); err != nil {
logger.Error("interaction response", zap.Error(err))
return
}
}
func InviteInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Add App",
Style: discordgo.LinkButton,
URL: invite,
Emoji: &discordgo.ComponentEmoji{
Name: "dice_golem",
ID: "1031958619782127616",
},
},
},
},
},
},
}); err != nil {
logger.Error("invite error", zap.Error(err))
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Sorry, an invite link couldn't be sent! Check the bot's Discord profile for alternative links.",
Flags: discordgo.MessageFlagsEphemeral,
},
}); err != nil {
logger.Error("invite error", zap.Error(err))
}
}
}
func ExpressionsInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
logger.Debug("expressions handler called", zap.String("interaction", i.ID))
subcommand := i.ApplicationCommandData().Options
u := UserFromInteraction(i)
switch subcommand[0].Name {
case "save":
key := fmt.Sprintf(KeyUserGlobalExpressionsFmt, u.ID)
count := DiceGolem.Redis.HLen(key).Val()
if DiceGolem.Redis != nil && count >= int64(DiceGolem.MaxExpressions) {
MeasureInteractionRespond(s.InteractionRespond, i,
newEphemeralResponse(fmt.Sprintf("You already have the maximum of %d saved expressions. Please remove one before adding another.", DiceGolem.MaxExpressions)),
)
return
}
options := subcommand[0].Options
var roll = new(NamedRollInput)
if optExpression := getOptionByName(options, "expression"); optExpression != nil {
roll.Expression = optExpression.StringValue()
} else {
panic("expression required")
}
if optName := getOptionByName(options, "name"); optName != nil {
roll.Name = optName.StringValue()
}
if optLabel := getOptionByName(options, "label"); optLabel != nil {
roll.Label = optLabel.StringValue()
}
roll.Clean()
if err := roll.Validate(); err != nil {
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Expression is invalid: "+err.Error()))
return
}
if err := SetNamedRoll(u, i.GuildID, roll); err != nil {
logger.Error("error saving roll", zap.Error(err))
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Something unexpected errored! Please try again later."))
return
}
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse(fmt.Sprintf("Saved `%v`! Total expressions: %d", roll, count+1)))
case "unsave":
key := fmt.Sprintf(KeyUserGlobalExpressionsFmt, u.ID)
if DiceGolem.Redis != nil {
if optExpression := getOptionByName(subcommand[0].Options, "expression"); optExpression != nil {
num, err := DiceGolem.Redis.HDel(key, optExpression.StringValue()).Result()
if err != nil {
panic(err)
}
if num == 1 {
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Removed the expression."))
}
}
return
}
case "export":
rolls, _ := GetNamedRolls(UserFromInteraction(i), "")
if len(rolls) == 0 {
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("You don't have any saved expressions."))
return
}
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "Exported your saved expressions to a CSV. Be sure to download it!",
Files: []*discordgo.File{
ExportExpressions(ctx, rolls),
},
},
}); err != nil {
logger.Error("error sending export", zap.Error(err))
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Something unexpected errored! The bot may be missing the _Attach Files_ permission."))
return
}
case "edit":
rolls, _ := GetNamedRolls(UserFromInteraction(i), "")
csvBytes, err := gocsv.MarshalBytes(&rolls)
if err != nil {
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Something unexpected errored!"))
return
}
modal := makeEditExpressionsModal(string(csvBytes))
if err := MeasureInteractionRespond(s.InteractionRespond, i, modal); err != nil {
logger.Error("error sending modal", zap.Error(err))
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Something unexpected errored!"))
return
}
case "clear":
_ = ExpressionsClearInteraction(ctx, u)
if err := MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Cleared your saved expressions (if any).")); err != nil {
logger.Error("error sending response", zap.Error(err))
}
default:
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Sorry! That subcommand does not have a handler yet."))
}
}
// ButtonsInteraction sends a macro pad of common dice. Presses are handled
// programmatically by HandleInteractionCreate.
func ButtonsInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
logger.Debug("buttons handler called", zap.String("interaction", i.ID))
errRes := newEphemeralResponse(ErrDMError.Error())
// if err != nil {
// MeasureInteractionRespond(s.InteractionRespond, i, errRes)
// return
// }
subcommand := i.ApplicationCommandData().Options
var components []discordgo.MessageComponent
switch subcommand[0].Name {
case "dnd5e":
components = Dnd5ePadComponents
case "fate":
components = FatePadComponents
}
err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Description: fmt.Sprintf("Click or tap to make dice rolls! Results will post to <#%s>.", i.ChannelID),
},
},
Flags: discordgo.MessageFlagsEphemeral,
Components: components,
},
})
// only send the tip message if we're not already in a DM
// if err == nil && !dm {
// MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
// Type: discordgo.InteractionResponseChannelMessageWithSource,
// Data: &discordgo.InteractionResponseData{
// Content: "Sent you a direct message!",
// Flags: discordgo.MessageFlagsEphemeral,
// },
// })
// } else
// if there was an error for the interaction, send a different error response
if err != nil {
logger.Error("error sending message", zap.Error(err))
MeasureInteractionRespond(s.InteractionRespond, i, errRes)
return
}
}
// HealthInteraction sends bot state information.
func HealthInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
logger.Debug("state handler called", zap.String("interaction", i.ID))
if !DiceGolem.IsOwner(UserFromInteraction(i)) {
if err := MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("This command is reserved for bot administrators.")); err != nil {
logger.Error("error sending response", zap.Error(err))
}
} else {
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlags(1 << 12),
Embeds: makeHealthEmbed(ctx),
},
}); err != nil {
logger.Error("error sending response", zap.Error(err))
}
}
}
// StatsInteraction sends bot stats.
func StatsInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
logger.Debug("stats handler called", zap.String("interaction", i.ID))
if !DiceGolem.IsOwner(UserFromInteraction(i)) {
if err := MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("This command is reserved for bot administrators.")); err != nil {
logger.Error("error sending response", zap.Error(err))
}
} else {
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: makeStatsEmbed(ctx),
},
}); err != nil {
logger.Error("error sending response", zap.Error(err))
}
}
}
func ClearInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
options := i.ApplicationCommandData().Options
u := UserFromInteraction(i)
switch options[0].Name {
case "recent":
// clear out recent roll key from the cache
if DiceGolem.Redis != nil {
key := fmt.Sprintf(KeyUserRecentFmt, u.ID)
DiceGolem.Redis.Del(key)
}
if err := MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Cleared your cached roll history (if any).")); err != nil {
logger.Error("error sending response", zap.Error(err))
}
case "expressions":
_ = ExpressionsClearInteraction(ctx, u)
if err := MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Cleared your saved expressions (if any).")); err != nil {
logger.Error("error sending response", zap.Error(err))
}
default:
if err := MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Sorry, an invalid subcommand was received!")); err != nil {
logger.Error("subcommand error", zap.Error(err))
}
}
}
func PreferencesInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
user := UserFromInteraction(i)
options := i.ApplicationCommandData().Options
switch options[0].Name {
case "recent":
option := mustGetOptionByName(options, "enabled")
if option.BoolValue() {
// reset to default 'enabled' setting
UnsetPreference(user, SettingNoRecent)
} else {
SetPreference(user, SettingNoRecent)
DiceGolem.Redis.Del(fmt.Sprintf(KeyUserRecentFmt, user.ID))
}
case "output":
option := mustGetOptionByName(options, "detailed")
if option.BoolValue() {
// set default of 'True'
SetPreference(user, SettingDetailed)
} else {
UnsetPreference(user, SettingDetailed)
}
default:
panic(fmt.Sprintf("unhandled preference: %s", options[0].Name))
}
MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("Updated your preference."))
}
func SettingsInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
options := i.ApplicationCommandData().Options
switch options[0].Name {
case "forward":
logger.Debug("updating forwarding settings")
option := mustGetOptionByName(options, "channel")
c := option.ChannelValue(s)
// if in the same guild (sanity check)
if c.GuildID == i.GuildID {
if c.ID == i.ChannelID {
// set to same channel (clear setting)
DiceGolem.Redis.Del(fmt.Sprintf("setting:%s:%s:%s", i.GuildID, i.ChannelID, SettingKeyForward))
} else {
SetSetting(i.GuildID, i.ChannelID, SettingKeyForward, c.ID)
if err := MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Color: 0x7493f3,
Description: fmt.Sprintf("Roll forwarding configured: <#%s> → %s", i.ChannelID, c.Mention()),
},
},
},
}); err != nil {
logger.Error("response error", zap.Error(err))
}
}
} else {
// unreachable
panic(ErrUnexpectedError)
}
default:
panic(fmt.Sprintf("unhandled setting: %s", options[0].Name))
}
}
func MeasureInteractionRespond(fn func(*discordgo.Interaction, *discordgo.InteractionResponse, ...discordgo.RequestOption) error, i *discordgo.Interaction, r *discordgo.InteractionResponse) error {
defer metrics.MeasureSince([]string{"interaction", "send"}, time.Now())
return fn(i, r)
}
// GetInteractionResponse retrieves the response Message for an Interaction sent
// by the bot. Since Discord's API doesn't return the Message when sent, we
// have to manually fetch it.
func GetInteractionResponse(s *discordgo.Session, i *discordgo.Interaction) (id string, err error) {
uri := discordgo.EndpointWebhookMessage(s.State.User.ID, i.Token, "@original")
body, err := s.RequestWithBucketID("GET", uri, nil, discordgo.EndpointWebhookToken("", ""))
if err != nil {
logger.Error("interaction response fetch", zap.Error(err), zap.String("interaction", i.ID))
return "", err
}
var data map[string]interface{}
if err := json.Unmarshal(body, &data); err != nil {
logger.Error("failed to unmarshal message", zap.Error(err))
return "", err
}
logger.Debug("interaction response", zap.String("interaction", i.ID), zap.Any("body", data))
return data["id"].(string), nil
}
// UserFromInteraction returns the User that spawned the supplied interaction.
// If the interaction was sent in a guild the user will be drawn from the
// interaction Member, otherwise it will be the User.
func UserFromInteraction(i *discordgo.Interaction) (user *discordgo.User) {
if i == nil {
return nil
}
if i.Member != nil {
return i.Member.User
}
return i.User
}
// UserFromInteraction returns the User that sent the supplied message.
// If the message was sent in a guild the user will be drawn from the
// interaction Member, otherwise it will be the Author.
func UserFromMessage(m *discordgo.Message) (user *discordgo.User) {
if m == nil {
return nil
}
if m.Member != nil && m.Member.User != nil {
return m.Member.User
}
return m.Author
}
// isRollPublic returns whether a roll-like interaction would be shown to a
// guild channel.
func isRollPublic(i *discordgo.Interaction) bool {
// if the command wasn't /roll or Roll Message, it's automatically private
if !contains([]string{"roll", "Roll Message"}, i.ApplicationCommandData().Name) {
return false
}
// no guild member data, therefore already from a DM
if i.Member == nil {
return false
}
// determine if the message was sent secretly or privately
options := i.ApplicationCommandData().Options
if optEphemeral := getOptionByName(options, "secret"); optEphemeral != nil && optEphemeral.BoolValue() {
return false
}
if optPrivate := getOptionByName(options, "private"); optPrivate != nil && optPrivate.BoolValue() {
return false
}
return true
}
func InteractionGolemancy(ctx context.Context) {
s, i, _ := FromContext(ctx)
if !DiceGolem.IsOwner(UserFromInteraction(i)) {
if err := MeasureInteractionRespond(s.InteractionRespond, i, newEphemeralResponse("This command is restricted to bot owners.")); err != nil {
logger.Error("error sending response", zap.Error(err))
}
return
}
data := i.ApplicationCommandData()
group := data.Options[0]
logger.Info("golemancy command received", zap.Any("data", data))
switch group.Name {
case "restart":
subcommand := group.Options[0]
switch subcommand.Name {
case "session":
id := getOptionByName(subcommand.Options, "id").IntValue()
if targetSession := DiceGolem.Sessions[id]; targetSession != nil {
MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
})
now := time.Now()
if err := restartSession(targetSession); err != nil {
logger.Error("error restarting session", zap.Error(err), zap.Int("shard", s.ShardID))
}
if _, err := s.InteractionResponseEdit(i, &discordgo.WebhookEdit{
Embeds: Ptr([]*discordgo.MessageEmbed{
{
Description: fmt.Sprintf("Session %d restarted (%s)!", s.ShardID, humanfmt.Sprintf("%s", time.Now().Sub(now))),
Footer: makeEmbedFooter(),
},
}),
},
); err != nil {
logger.Error("error responding to restart", zap.Error(err))
}
}
}
default:
panic(fmt.Errorf("unhandled golemancy subcommand: %s", group.Name))
}
}
func DebugInteraction(ctx context.Context) {
s, i, _ := FromContext(ctx)
MeasureInteractionRespond(s.InteractionRespond, i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("@%s#%s", i.User.Username, i.User.Discriminator),
Flags: discordgo.MessageFlagsEphemeral,
},
})
}