-
Notifications
You must be signed in to change notification settings - Fork 128
/
discord.d
989 lines (797 loc) · 29.1 KB
/
discord.d
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
/++
Module for interacting with the Discord chat service. I use it to run a discord bot providing some slash commands.
$(LIST
* Real time gateway
See [DiscordGatewayConnection]
You can use [SlashCommandHandler] subclasses registered with a gateway connection to easily add slash commands to your app.
* REST api
See [DiscordRestApi]
* Local RPC server
See [DiscordRpcConnection] (not implemented)
* Voice connections
not implemented
* Login with Discord
OAuth2 is easy enough without the lib, see bingo.d line 340ish-380ish.
)
History:
Started April 20, 2024.
+/
module arsd.discord;
// FIXME: Secure Connect Failed sometimes on trying to reconnect, should prolly just try again after a short period, or ditch the whole thing if reconnectAndResume and try fresh
// FIXME: User-Agent: DiscordBot ($url, $versionNumber)
import arsd.http2;
import arsd.jsvar;
import arsd.core;
import core.time;
static assert(use_arsd_core);
/++
Base class to represent some object on Discord, e.g. users, channels, etc., through its subclasses.
Among its implementations are:
$(LIST
* [DiscordChannel]
* [DiscordUser]
* [DiscordRole]
)
+/
abstract class DiscordEntity {
private DiscordRestApi api;
private string id_;
protected this(DiscordRestApi api, string id) {
this.api = api;
this.id_ = id;
}
override string toString() {
return restType ~ "/" ~ id;
}
/++
+/
abstract string restType();
/++
+/
final string id() {
return id_;
}
/++
Gives easy access to its rest api through [arsd.http2.HttpApiClient]'s dynamic dispatch functions.
+/
DiscordRestApi.RestBuilder rest() {
return api.rest[restType()][id()];
}
}
/++
Represents something mentionable on Discord with `@name` - roles and users.
+/
abstract class DiscordMentionable : DiscordEntity {
this(DiscordRestApi api, string id) {
super(api, id);
}
}
/++
https://discord.com/developers/docs/resources/channel
+/
class DiscordChannel : DiscordEntity {
this(DiscordRestApi api, string id) {
super(api, id);
}
override string restType() {
return "channels";
}
void sendMessage(string message) {
if(message.length == 0)
message = "empty message specified";
var msg = var.emptyObject;
msg.content = message;
rest.messages.POST(msg).result;
}
}
/++
+/
class DiscordRole : DiscordMentionable {
this(DiscordRestApi api, DiscordGuild guild, string id) {
this.guild_ = guild;
super(api, id);
}
private DiscordGuild guild_;
/++
+/
DiscordGuild guild() {
return guild_;
}
override string restType() {
return "roles";
}
}
/++
https://discord.com/developers/docs/resources/user
+/
class DiscordUser : DiscordMentionable {
this(DiscordRestApi api, string id) {
super(api, id);
}
private var cachedData;
// DiscordGuild selectedGuild;
override string restType() {
return "users";
}
void addRole(DiscordRole role) {
// PUT /guilds/{guild.id}/members/{user.id}/roles/{role.id}
auto thing = api.rest.guilds[role.guild.id].members[this.id].roles[role.id];
writeln(thing.toUri);
auto result = api.rest.guilds[role.guild.id].members[this.id].roles[role.id].PUT().result;
}
void removeRole(DiscordRole role) {
// DELETE /guilds/{guild.id}/members/{user.id}/roles/{role.id}
auto thing = api.rest.guilds[role.guild.id].members[this.id].roles[role.id];
writeln(thing.toUri);
auto result = api.rest.guilds[role.guild.id].members[this.id].roles[role.id].DELETE().result;
}
private DiscordChannel dmChannel_;
DiscordChannel dmChannel() {
if(dmChannel_ is null) {
var obj = var.emptyObject;
obj.recipient_id = this.id;
var result = this.api.rest.users["@me"].channels.POST(obj).result;
dmChannel_ = new DiscordChannel(api, result.id.get!string);//, result);
}
return dmChannel_;
}
void sendMessage(string what) {
dmChannel.sendMessage(what);
}
}
/++
+/
class DiscordGuild : DiscordEntity {
this(DiscordRestApi api, string id) {
super(api, id);
}
override string restType() {
return "guilds";
}
}
enum InteractionType {
PING = 1,
APPLICATION_COMMAND = 2, // the main one
MESSAGE_COMPONENT = 3,
APPLICATION_COMMAND_AUTOCOMPLETE = 4,
MODAL_SUBMIT = 5,
}
/++
You can create your own slash command handlers by subclassing this and writing methods like
It will register for you when you connect and call your function when things come in.
See_Also:
https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-global-application-commands
+/
class SlashCommandHandler {
enum ApplicationCommandOptionType {
INVALID = 0, // my addition
SUB_COMMAND = 1,
SUB_COMMAND_GROUP = 2,
STRING = 3,
INTEGER = 4, // double's int part
BOOLEAN = 5,
USER = 6,
CHANNEL = 7,
ROLE = 8,
MENTIONABLE = 9,
NUMBER = 10, // double
ATTACHMENT = 11,
}
/++
This takes the child type into the parent so we can reflect over your added methods.
to initialize the reflection info to send to Discord. If you subclass your subclass,
make sure the grandchild constructor does `super(); registerAll(this);` to add its method
to the list too, but if you only have one level of child, the compiler will auto-generate
a constructor for you that calls this.
+/
protected this(this This)() {
registerAll(cast(This) this);
}
/++
+/
static class InteractionReplyHelper {
private DiscordRestApi api;
private CommandArgs commandArgs;
private this(DiscordRestApi api, CommandArgs commandArgs) {
this.api = api;
this.commandArgs = commandArgs;
}
/++
+/
void reply(string message, bool ephemeral = false) scope {
replyLowLevel(message, ephemeral);
}
/++
+/
void replyWithError(scope const(char)[] message) scope {
if(message.length == 0)
message = "I am error.";
replyLowLevel(message.idup, true);
}
enum MessageFlags : uint {
SUPPRESS_EMBEDS = (1 << 2), // skip the embedded content
EPHEMERAL = (1 << 6), // only visible to you
LOADING = (1 << 7), // the bot is "thinking"
SUPPRESS_NOTIFICATIONS = (1 << 12) // skip push/desktop notifications
}
void replyLowLevel(string message, bool ephemeral) scope {
if(message.length == 0)
message = "empty message";
var reply = var.emptyObject;
reply.type = 4; // chat response in message. 5 can be answered quick and edited later if loading, 6 if quick answer, no loading message
var replyData = var.emptyObject;
replyData.content = message;
replyData.flags = ephemeral ? (1 << 6) : 0;
reply.data = replyData;
try {
var result = api.rest.
interactions[commandArgs.interactionId][commandArgs.interactionToken].callback
.POST(reply).result;
writeln(result.toString);
} catch(Exception e) {
import std.stdio; writeln(commandArgs);
writeln(e.toString());
}
}
}
private bool alreadyRegistered;
private void register(DiscordRestApi api, string appId) {
if(alreadyRegistered)
return;
auto result = api.rest.applications[appId].commands.PUT(jsonArrayForDiscord).result;
alreadyRegistered = true;
}
private static struct CommandArgs {
InteractionType interactionType;
string interactionToken;
string interactionId;
string guildId;
string channelId;
var interactionData;
var member;
var channel;
}
private {
static void validateDiscordSlashCommandName(string name) {
foreach(ch; name) {
if(ch != '_' && !(ch >= 'a' && ch <= 'z'))
throw new InvalidArgumentsException("name", "discord names must be all lower-case with only letters and underscores", LimitedVariant(name));
}
}
static HandlerInfo makeHandler(alias handler, T)(T slashThis) {
HandlerInfo info;
// must be all lower case!
info.name = __traits(identifier, handler);
validateDiscordSlashCommandName(info.name);
var cmd = var.emptyObject();
cmd.name = info.name;
version(D_OpenD)
cmd.description = __traits(docComment, handler);
else
cmd.description = "";
if(cmd.description == "")
cmd.description = "Can't be blank for CHAT_INPUT";
cmd.type = 1; // CHAT_INPUT
var optionsArray = var.emptyArray;
static if(is(typeof(handler) Params == __parameters)) {}
string[] names;
// extract parameters
foreach(idx, param; Params) {
var option = var.emptyObject;
auto name = __traits(identifier, Params[idx .. idx + 1]);
validateDiscordSlashCommandName(name);
names ~= name;
option.name = name;
option.description = "desc";
option.type = cast(int) applicationComandOptionTypeFromDType!(param);
// can also add "choices" which limit it to just specific members
if(option.type) {
optionsArray ~= option;
}
}
cmd.options = optionsArray;
info.jsonObjectForDiscord = cmd;
info.handler = (CommandArgs args, scope InteractionReplyHelper replyHelper, DiscordRestApi api) {
// extract args
// call the function
// send the appropriate reply
static if(is(typeof(handler) Return == return)) {
static if(is(Return == void)) {
__traits(child, slashThis, handler)(fargsFromJson!Params(api, names, args.interactionData, args).tupleof);
sendHandlerReply("OK", replyHelper, true);
} else {
sendHandlerReply(__traits(child, slashThis, handler)(fargsFromJson!Params(api, names, args.interactionData, args).tupleof), replyHelper, false);
}
} else static assert(0);
};
return info;
}
static auto fargsFromJson(Params...)(DiscordRestApi api, string[] names, var obj, CommandArgs args/*, Params defaults*/) {
static struct Holder {
// FIXME: default params no work
Params params;// = defaults;
}
Holder holder;
foreach(idx, ref param; holder.params) {
setParamFromJson(param, names[idx], api, obj, args);
}
return holder;
/+
ync def something(interaction:discord.Interaction):
await interaction.response.send_message("NOTHING",ephemeral=True)
# optional (if you want to edit the response later,delete it, or send a followup)
await interaction.edit_original_response(content="Something")
await interaction.followup.send("This is a message too.",ephemeral=True)
await interaction.delete_original_response()
# if you have deleted the original response you can't edit it or send a followup after it
+/
}
// {"t":"INTERACTION_CREATE","s":7,"op":0,"d":{"version":1,"type":2,"token":"aW50ZXJhY3Rpb246MTIzMzIyNzE0OTU0NTE3NzE2OTp1Sjg5RE0wMzJiWER2UDRURk5XSWRaUTJtMExBeklWNEtpVEZocTQ4a0VZQ3NWUm9ta3g2SG1JbTBzUm1yWmlUNzQ3eWxpc0FnM0RzUzZHaWtENnRXUDBsdUhERElKSWlaYlFWMlNsZlZXTlFkU3VVQUVWU01PNU9TNFQ5cmFQSw",
// "member":{"user":{"username":"wrathful_vengeance_god_unleashed","public_flags":0,"id":"395786107780071424","global_name":"adr","discriminator":"0","clan":null,"avatar_decoration_data":null,"avatar":"e3c2aacef7920d3a661a19aaab969337"},"unusual_dm_activity_until":null,"roles":[],"premium_since":null,"permissions":"1125899906842623","pending":false,"nick":"adr","mute":false,"joined_at":"2022-08-24T12:37:21.252000+00:00","flags":0,"deaf":false,"communication_disabled_until":null,"avatar":null},
// "locale":"en-US","id":"1233227149545177169","guild_locale":"en-US","guild_id":"1011977515109187704",
// "guild":{"locale":"en-US","id":"1011977515109187704","features":[]},
// "entitlements":[],"entitlement_sku_ids":[],
// "data":{"type":1,"name":"hello","id":"1233221536522174535"},"channel_id":"1011977515109187707",
// "channel":{"type":0,"topic":null,"rate_limit_per_user":0,"position":0,"permissions":"1125899906842623","parent_id":"1011977515109187705","nsfw":false,"name":"general","last_message_id":"1233227103844171806","id":"1011977515109187707","guild_id":"1011977515109187704","flags":0},
// "application_id":"1223724819821105283","app_permissions":"1122573558992465"}}
template applicationComandOptionTypeFromDType(T) {
static if(is(T == SendingUser) || is(T == SendingChannel))
enum applicationComandOptionTypeFromDType = ApplicationCommandOptionType.INVALID; // telling it to skip sending this to discord, it purely internal
else static if(is(T == DiscordRole))
enum applicationComandOptionTypeFromDType = ApplicationCommandOptionType.ROLE;
else static if(is(T == string))
enum applicationComandOptionTypeFromDType = ApplicationCommandOptionType.STRING;
else static if(is(T == bool))
enum applicationComandOptionTypeFromDType = ApplicationCommandOptionType.BOOLEAN;
else static if(is(T : const long))
enum applicationComandOptionTypeFromDType = ApplicationCommandOptionType.INTEGER;
else static if(is(T : const double))
enum applicationComandOptionTypeFromDType = ApplicationCommandOptionType.NUMBER;
else
static assert(0, T.stringof);
}
static var getOptionForName(var obj, string name) {
foreach(option; obj.options)
if(option.name == name)
return option;
return var.init;
}
static void setParamFromJson(T)(ref T param, string name, DiscordRestApi api, var obj, CommandArgs args) {
static if(is(T == SendingUser)) {
param = new SendingUser(api, args.member.user.id.get!string, obj.member.user);
} else static if(is(T == SendingChannel)) {
param = new SendingChannel(api, args.channel.id.get!string, obj.channel);
} else static if(is(T == string)) {
var option = getOptionForName(obj, name);
if(option.type == cast(int) ApplicationCommandOptionType.STRING)
param = option.value.get!(typeof(param));
} else static if(is(T == bool)) {
var option = getOptionForName(obj, name);
if(option.type == cast(int) ApplicationCommandOptionType.BOOLEAN)
param = option.value.get!(typeof(param));
} else static if(is(T : const long)) {
var option = getOptionForName(obj, name);
if(option.type == cast(int) ApplicationCommandOptionType.INTEGER)
param = option.value.get!(typeof(param));
} else static if(is(T : const double)) {
var option = getOptionForName(obj, name);
if(option.type == cast(int) ApplicationCommandOptionType.NUMBER)
param = option.value.get!(typeof(param));
} else static if(is(T == DiscordRole)) {
//"data":{"type":1,"resolved":{"roles":{"1223727548295544865":{"unicode_emoji":null,"tags":{"bot_id":"1223724819821105283"},"position":1,"permissions":"3088","name":"OpenD","mentionable":false,"managed":true,"id":"1223727548295544865","icon":null,"hoist":false,"flags":0,"description":null,"color":0}}},"options":[{"value":"1223727548295544865","type":8,"name":"role"}],"name":"add_role","id":"1234130839315677226"},"channel_id":"1011977515109187707","channel":{"type":0,"topic":null,"rate_limit_per_user":0,"position":0,"permissions":"1125899906842623","parent_id":"1011977515109187705","nsfw":false,"name":"general","last_message_id":"1234249771745804399","id":"1011977515109187707","guild_id":"1011977515109187704","flags":0},"application_id":"1223724819821105283","app_permissions":"1122573558992465"}}
// resolved gives you some precache info
var option = getOptionForName(obj, name);
if(option.type == cast(int) ApplicationCommandOptionType.ROLE)
param = new DiscordRole(api, new DiscordGuild(api, args.guildId), option.value.get!string);
else
param = null;
} else {
static assert(0, "Bad type " ~ T.stringof);
}
}
static void sendHandlerReply(T)(T ret, scope InteractionReplyHelper replyHelper, bool ephemeral) {
import std.conv; // FIXME
replyHelper.reply(to!string(ret), ephemeral);
}
void registerAll(T)(T t) {
assert(t !is null);
foreach(memberName; __traits(derivedMembers, T))
static if(memberName != "__ctor") { // FIXME
HandlerInfo hi = makeHandler!(__traits(getMember, T, memberName))(t);
registerFromRuntimeInfo(hi);
}
}
void registerFromRuntimeInfo(HandlerInfo info) {
handlers[info.name] = info.handler;
if(jsonArrayForDiscord is var.init)
jsonArrayForDiscord = var.emptyArray;
jsonArrayForDiscord ~= info.jsonObjectForDiscord;
}
alias InternalHandler = void delegate(CommandArgs args, scope InteractionReplyHelper replyHelper, DiscordRestApi api);
struct HandlerInfo {
string name;
InternalHandler handler;
var jsonObjectForDiscord;
}
InternalHandler[string] handlers;
var jsonArrayForDiscord;
}
}
/++
A SendingUser is a special DiscordUser type that just represents the person who sent the message.
It exists so you can use it in a function parameter list that is auto-mapped to a message handler.
+/
class SendingUser : DiscordUser {
private this(DiscordRestApi api, string id, var initialCache) {
super(api, id);
}
}
class SendingChannel : DiscordChannel {
private this(DiscordRestApi api, string id, var initialCache) {
super(api, id);
}
}
// SendingChannel
// SendingMessage
/++
Use as a UDA
A file of choices for the given option. The exact interpretation depends on the type but the general rule is one option per line, id or name.
FIXME: watch the file for changes for auto-reload and update on the discord side
FIXME: NOT IMPLEMENTED
+/
struct ChoicesFromFile {
string filename;
}
/++
Most the magic is inherited from [arsd.http2.HttpApiClient].
+/
class DiscordRestApi : HttpApiClient!() {
/++
Creates an API client.
Params:
token = the bot authorization token you got from Discord
yourBotUrl = a URL for your bot, used to identify the user-agent. Discord says it should not be null, but that seems to work.
yourBotVersion = version number (or whatever) for your bot, used as part of the user-agent. Should not be null according to the docs but it doesn't seem to matter in practice.
+/
this(string botToken, string yourBotUrl, string yourBotVersion) {
this.authType = "Bot";
super("https://discord.com/api/v10/", botToken);
}
}
/++
+/
class DiscordGatewayConnection {
private WebSocket websocket_;
private long lastSequenceNumberReceived;
private string token;
private DiscordRestApi api_;
/++
An instance to the REST api object associated with your connection.
+/
public final DiscordRestApi api() {
return this.api_;
}
/++
+/
protected final WebSocket websocket() {
return websocket_;
}
// https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-opcodes
enum OpCode {
Dispatch = 0, // recv
Heartbeat = 1, // s/r
Identify = 2, // s
PresenceUpdate = 3, // s
VoiceStateUpdate = 4, // s
Resume = 6, // s
Reconnect = 7, // r
RequestGuildMembers = 8, // s
InvalidSession = 9, // r - you should reconnect and identify/resume
Hello = 10, // r
HeartbeatAck = 11, // r
}
enum DisconnectCodes {
UnknownError = 4000, // t
UnknownOpcode = 4001, // t (user error)
DecodeError = 4002, // t (user error)
NotAuthenticated = 4003, // t (user error)
AuthenticationFailed = 4004, // f (user error)
AlreadyAuthenticated = 4005, // t (user error)
InvalidSeq = 4007, // t
RateLimited = 4008, // t
SessionTimedOut = 4009, // t
InvalidShard = 4010, // f (user error)
ShardingRequired = 4011, // f
InvalidApiVersion = 4012, // f
InvalidIntents = 4013, // f
DisallowedIntents = 4014, // f
}
private string cachedGatewayUrl;
/++
Prepare a gateway connection. After you construct it, you still need to call [connect].
Params:
token = the bot authorization token you got from Discord
yourBotUrl = a URL for your bot, used to identify the user-agent. Discord says it should not be null, but that seems to work.
yourBotVersion = version number (or whatever) for your bot, used as part of the user-agent. Should not be null according to the docs but it doesn't seem to matter in practice.
+/
public this(string token, string yourBotUrl, string yourBotVersion) {
this.token = token;
this.api_ = new DiscordRestApi(token, yourBotUrl, yourBotVersion);
}
/++
Allows you to set up a subclass of [SlashCommandHandler] for handling discord slash commands.
+/
final void slashCommandHandler(SlashCommandHandler t) {
if(slashCommandHandler_ !is null && t !is null)
throw ArsdException!"SlashCommandHandler is already set"();
slashCommandHandler_ = t;
if(t && applicationId.length)
t.register(api, applicationId);
}
private SlashCommandHandler slashCommandHandler_;
/++
+/
protected void handleWebsocketClose(WebSocket.CloseEvent closeEvent) {
import std.stdio; writeln(closeEvent);
if(heartbeatTimer)
heartbeatTimer.cancel();
if(closeEvent.code == 1006 || closeEvent.code == 1001) {
reconnectAndResume();
} else {
// otherwise, unless we were asked by the api user to close, let's try reconnecting
// since discord just does discord things.
connect();
}
}
/++
+/
void close() {
close(1000, null);
}
/// ditto
void close(int reason, string reasonText) {
if(heartbeatTimer)
heartbeatTimer.cancel();
websocket_.onclose = null;
websocket_.ontextmessage = null;
websocket_.onbinarymessage = null;
websocket.close(reason, reasonText);
websocket_ = null;
}
/++
+/
protected void handleWebsocketMessage(in char[] msg) {
var m = var.fromJson(msg.idup);
OpCode op = cast(OpCode) m.op.get!int;
var data = m.d;
switch(op) {
case OpCode.Dispatch:
// these are null if op != 0
string eventName = m.t.get!string;
long seqNumber = m.s.get!long;
if(seqNumber > lastSequenceNumberReceived)
lastSequenceNumberReceived = seqNumber;
eventReceived(eventName, data);
break;
case OpCode.Hello:
// the hello heartbeat_interval is in milliseconds
if(slashCommandHandler_ !is null && applicationId.length)
slashCommandHandler_.register(api, applicationId);
setHeartbeatInterval(data.heartbeat_interval.get!int);
break;
case OpCode.Heartbeat:
sendHeartbeat();
break;
case OpCode.HeartbeatAck:
mostRecentHeartbeatAckRecivedAt = MonoTime.currTime;
break;
case OpCode.Reconnect:
writeln("reconnecting");
this.close(4999, "Reconnect requested");
reconnectAndResume();
break;
case OpCode.InvalidSession:
writeln("starting new session");
close();
connect(); // try starting a brand new session
break;
default:
// ignored
}
}
protected void reconnectAndResume() {
this.websocket_ = new WebSocket(Uri(this.resume_gateway_url));
websocket.onmessage = &handleWebsocketMessage;
websocket.onclose = &handleWebsocketClose;
// FIXME: if the connect fails we should set a timer and try
// again, but if it fails then, quit. at least if it is not a websocket reply
// cuz it could be discord went down or something.
this.websocket_.connect();
var resumeData = var.emptyObject;
resumeData.token = this.token;
resumeData.session_id = this.session_id;
resumeData.seq = lastSequenceNumberReceived;
sendWebsocketCommand(OpCode.Resume, resumeData);
// the close event will cancel the heartbeat and thus we need to restart it
if(requestedHeartbeat)
setHeartbeatInterval(requestedHeartbeat);
}
/++
+/
protected void eventReceived(string eventName, var data) {
// FIXME: any time i get an event i could prolly spin it off into an independent async task
switch(eventName) {
case "INTERACTION_CREATE":
var member = data.member; // {"user":{"username":"wrathful_vengeance_god_unleashed","public_flags":0,"id":"395786107780071424","global_name":"adr","discriminator":"0","clan":null,"avatar_decoration_data":null,"avatar":"e3c2aacef7920d3a661a19aaab969337"},"unusual_dm_activity_until":null,"roles":[],"premium_since":null,"permissions":"1125899906842623","pending":false,"nick":"adr","mute":false,"joined_at":"2022-08-24T12:37:21.252000+00:00","flags":0,"deaf":false,"communication_disabled_until":null,"avatar":null}
SlashCommandHandler.CommandArgs commandArgs;
commandArgs.interactionType = cast(InteractionType) data.type.get!int;
commandArgs.interactionToken = data.token.get!string;
commandArgs.interactionId = data.id.get!string;
commandArgs.guildId = data.guild_id.get!string;
commandArgs.channelId = data.channel_id.get!string;
commandArgs.member = member;
commandArgs.channel = data.channel;
commandArgs.interactionData = data.data;
// data.data : type/name/id. can use this to determine what function to call. prolly will include other info too
// "data":{"type":1,"name":"hello","id":"1233221536522174535"}
// application_id and app_permissions and some others there too but that doesn't seem important
/+
replies:
https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-interaction-callback-type
+/
scope SlashCommandHandler.InteractionReplyHelper replyHelper = new SlashCommandHandler.InteractionReplyHelper(api, commandArgs);
Exception throwExternally;
try {
if(slashCommandHandler_ is null)
throwExternally = ArsdException!"No slash commands registered"();
else {
auto cmdName = commandArgs.interactionData.name.get!string;
if(auto pHandler = cmdName in slashCommandHandler_.handlers) {
(*pHandler)(commandArgs, replyHelper, api);
} else {
throwExternally = ArsdException!"Unregistered slash command"(cmdName);
}
}
} catch(ArsdExceptionBase e) {
const(char)[] msg = e.message;
if(msg.length == 0)
msg = "I am error.";
e.getAdditionalPrintableInformation((string name, in char[] value) {
msg ~= ("\n");
msg ~= (name);
msg ~= (": ");
msg ~= (value);
});
replyHelper.replyWithError(msg);
} catch(Exception e) {
replyHelper.replyWithError(e.message);
}
if(throwExternally !is null)
throw throwExternally;
break;
case "READY":
this.session_id = data.session_id.get!string;
this.resume_gateway_url = data.resume_gateway_url.get!string;
this.applicationId_ = data.application.id.get!string;
if(slashCommandHandler_ !is null && applicationId.length)
slashCommandHandler_.register(api, applicationId);
break;
default:
}
}
private string session_id;
private string resume_gateway_url;
private string applicationId_;
/++
Returns your application id. Only available after the connection is established.
+/
public string applicationId() {
return applicationId_;
}
private arsd.core.Timer heartbeatTimer;
private int requestedHeartbeat;
private bool requestedHeartbeatSet;
//private int heartbeatsSent;
//private int heartbeatAcksReceived;
private MonoTime mostRecentHeartbeatAckRecivedAt;
protected void sendHeartbeat() {
arsd.core.writeln("sendHeartbeat");
sendWebsocketCommand(OpCode.Heartbeat, var(lastSequenceNumberReceived));
}
private final void sendHeartbeatThunk() {
this.sendHeartbeat(); // also virtualizes which wouldn't happen with &sendHeartbeat
if(requestedHeartbeatSet == false) {
heartbeatTimer.changeTime(requestedHeartbeat, true);
requestedHeartbeatSet = true;
} else {
if(MonoTime.currTime - mostRecentHeartbeatAckRecivedAt > 2 * requestedHeartbeat.msecs) {
// throw ArsdException!"connection has no heartbeat"(); // FIXME: pass the info?
websocket.close(1006, "heartbeat unanswered");
reconnectAndResume();
}
}
}
/++
+/
protected void setHeartbeatInterval(int msecs) {
requestedHeartbeat = msecs;
requestedHeartbeatSet = false;
if(heartbeatTimer is null) {
heartbeatTimer = new arsd.core.Timer;
heartbeatTimer.setPulseCallback(&sendHeartbeatThunk);
}
// the first one is supposed to have random jitter
// so we'll do that one-off (but with a non-zero time
// since my timers don't like being run twice in one loop
// iteration) then that first one will set the repeating time
import std.random;
auto firstBeat = std.random.uniform(10, msecs);
heartbeatTimer.changeTime(firstBeat, false);
}
/++
+/
void sendWebsocketCommand(OpCode op, var d) {
assert(websocket !is null, "call connect before sending commands");
var cmd = var.emptyObject;
cmd.d = d;
cmd.op = cast(int) op;
websocket.send(cmd.toJson());
}
/++
+/
void connect() {
assert(websocket is null, "do not call connect twice");
if(cachedGatewayUrl is null) {
auto obj = api.rest.gateway.bot.GET().result;
cachedGatewayUrl = obj.url.get!string;
}
this.websocket_ = new WebSocket(Uri(cachedGatewayUrl));
websocket.onmessage = &handleWebsocketMessage;
websocket.onclose = &handleWebsocketClose;
websocket.connect();
var d = var.emptyObject;
d.token = token;
// FIXME?
d.properties = [
"os": "linux",
"browser": "arsd.discord",
"device": "arsd.discord",
];
sendWebsocketCommand(OpCode.Identify, d);
}
}
class DiscordRpcConnection {
// this.websocket_ = new WebSocket(Uri("ws://127.0.0.1:6463/?v=1&client_id=XXXXXXXXXXXXXXXXX&encoding=json"), config);
// websocket.send(`{ "nonce": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "args": { "access_token": "XXXXXXXXXXXXXXXXXXXXX" }, "cmd": "AUTHENTICATE" }`);
// writeln(websocket.waitForNextMessage.textData);
// these would tell me user names and ids when people join/leave but it needs authentication alas
/+
websocket.send(`{ "nonce": "ce9a6de3-31d0-4767-a8e9-4818c5690015", "args": {
"guild_id": "SSSSSSSSSSSSSSSS",
"channel_id": "CCCCCCCCCCCCCCCCC"
},
"evt": "VOICE_STATE_CREATE",
"cmd": "SUBSCRIBE"
}`);
writeln(websocket.waitForNextMessage.textData);
websocket.send(`{ "nonce": "de9a6de3-31d0-4767-a8e9-4818c5690015", "args": {
"guild_id": "SSSSSSSSSSSSSSSS",
"channel_id": "CCCCCCCCCCCCCCCCC"
},
"evt": "VOICE_STATE_DELETE",
"cmd": "SUBSCRIBE"
}`);
websocket.onmessage = delegate(in char[] msg) {
writeln(msg);
import arsd.jsvar;
var m = var.fromJson(msg.idup);
if(m.cmd == "DISPATCH") {
if(m.evt == "SPEAKING_START") {
//setSpeaking(m.data.user_id.get!ulong, true);
} else if(m.evt == "SPEAKING_STOP") {
//setSpeaking(m.data.user_id.get!ulong, false);
}
}
};
+/
}