disgroove 2.2.7 → 3.0.0-dev.32616ef

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/LICENSE +9 -9
  2. package/README.md +48 -48
  3. package/dist/lib/Client.d.ts +252 -254
  4. package/dist/lib/Client.js +472 -471
  5. package/dist/lib/gateway/Shard.d.ts +7 -4
  6. package/dist/lib/gateway/Shard.js +279 -198
  7. package/dist/lib/index.d.ts +4 -0
  8. package/dist/lib/index.js +4 -0
  9. package/dist/lib/rest/Endpoints.d.ts +94 -94
  10. package/dist/lib/rest/Endpoints.js +104 -104
  11. package/dist/lib/rest/RequestManager.js +17 -7
  12. package/dist/lib/rest/index.js +17 -7
  13. package/dist/lib/transformers/ApplicationCommands.js +4 -4
  14. package/dist/lib/transformers/Applications.d.ts +2 -0
  15. package/dist/lib/transformers/Applications.js +110 -4
  16. package/dist/lib/transformers/AuditLogs.js +10 -10
  17. package/dist/lib/transformers/AutoModeration.js +6 -6
  18. package/dist/lib/transformers/Channels.js +16 -16
  19. package/dist/lib/transformers/Components.js +16 -16
  20. package/dist/lib/transformers/Entitlements.d.ts +2 -2
  21. package/dist/lib/transformers/Entitlements.js +16 -16
  22. package/dist/lib/transformers/GuildScheduledEvents.js +8 -8
  23. package/dist/lib/transformers/GuildTemplates.js +4 -4
  24. package/dist/lib/transformers/Guilds.js +28 -28
  25. package/dist/lib/transformers/Interactions.js +44 -44
  26. package/dist/lib/transformers/Lobbies.js +2 -2
  27. package/dist/lib/transformers/Messages.js +18 -18
  28. package/dist/lib/transformers/Polls.js +2 -2
  29. package/dist/lib/transformers/Presences.js +6 -6
  30. package/dist/lib/transformers/Roles.js +6 -6
  31. package/dist/lib/transformers/SKUs.js +2 -2
  32. package/dist/lib/transformers/Soundboards.js +6 -6
  33. package/dist/lib/transformers/StageInstances.js +6 -6
  34. package/dist/lib/transformers/Stickers.js +3 -3
  35. package/dist/lib/transformers/Subscriptions.js +8 -8
  36. package/dist/lib/transformers/Teams.js +4 -4
  37. package/dist/lib/transformers/Users.js +6 -6
  38. package/dist/lib/transformers/Voice.js +8 -8
  39. package/dist/lib/transformers/Webhooks.js +6 -6
  40. package/dist/lib/types/application-command.d.ts +9 -4
  41. package/dist/lib/types/application-role-connection-metadata.d.ts +1 -0
  42. package/dist/lib/types/application.d.ts +12 -7
  43. package/dist/lib/types/audit-log.d.ts +9 -5
  44. package/dist/lib/types/auto-moderation.d.ts +7 -3
  45. package/dist/lib/types/channel.d.ts +17 -23
  46. package/dist/lib/types/common.d.ts +2 -0
  47. package/dist/lib/types/components.d.ts +43 -15
  48. package/dist/lib/types/emoji.d.ts +1 -0
  49. package/dist/lib/types/entitlements.d.ts +5 -4
  50. package/dist/lib/types/gateway-events.d.ts +181 -130
  51. package/dist/lib/types/guild-scheduled-event.d.ts +10 -5
  52. package/dist/lib/types/guild-template.d.ts +3 -2
  53. package/dist/lib/types/guild.d.ts +33 -17
  54. package/dist/lib/types/interaction.d.ts +23 -10
  55. package/dist/lib/types/invite.d.ts +3 -0
  56. package/dist/lib/types/lobby.d.ts +3 -1
  57. package/dist/lib/types/message.d.ts +29 -15
  58. package/dist/lib/types/poll.d.ts +7 -1
  59. package/dist/lib/types/role.d.ts +6 -3
  60. package/dist/lib/types/sku.d.ts +2 -1
  61. package/dist/lib/types/soundboard.d.ts +4 -3
  62. package/dist/lib/types/stage-instance.d.ts +4 -3
  63. package/dist/lib/types/sticker.d.ts +8 -5
  64. package/dist/lib/types/subscription.d.ts +6 -5
  65. package/dist/lib/types/team.d.ts +4 -2
  66. package/dist/lib/types/user.d.ts +10 -3
  67. package/dist/lib/types/voice.d.ts +6 -4
  68. package/dist/lib/types/webhook.d.ts +4 -3
  69. package/dist/lib/utils/CDN.d.ts +22 -22
  70. package/dist/lib/utils/CDN.js +22 -22
  71. package/dist/lib/utils/formatters.d.ts +6 -6
  72. package/dist/lib/utils/formatters.js +27 -28
  73. package/dist/lib/utils/index.js +17 -7
  74. package/dist/package.json +1 -1
  75. package/package.json +2 -2
  76. package/dist/lib/types/message-components.d.ts +0 -450
  77. package/dist/lib/types/message-components.js +0 -2
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
37
  };
@@ -37,63 +47,79 @@ class Shard {
37
47
  heartbeatInterval;
38
48
  client;
39
49
  ws;
40
- sessionID;
50
+ sessionId;
41
51
  resumeGatewayURL;
52
+ sequence;
42
53
  constructor(id, client) {
43
54
  this.id = id;
44
55
  this.heartbeatInterval = null;
45
56
  this.client = client;
46
- this.ws = new ws_1.default("wss://gateway.discord.gg/?v=10&encoding=json", client.ws);
47
- this.sessionID = null;
57
+ this.ws = new ws_1.default("wss://gateway.discord.gg/?v=10&encoding=json", this.client.ws);
58
+ this.sessionId = null;
48
59
  this.resumeGatewayURL = null;
60
+ this.sequence = null;
49
61
  }
50
62
  /** https://discord.com/developers/docs/topics/gateway#connections */
51
- connect() {
52
- this.ws.on("open", () => this.onWebSocketOpen());
53
- this.ws.on("message", (data) => this.onWebSocketMessage(data));
54
- this.ws.on("error", (err) => this.onWebSocketError(err));
55
- this.ws.on("close", (code, reason) => this.onWebSocketClose(code, reason));
63
+ connect(reconnect) {
64
+ if (this.ws) {
65
+ this.ws.on("open", () => this.onWebSocketOpen(reconnect));
66
+ this.ws.on("message", (data) => this.onWebSocketMessage(data));
67
+ this.ws.on("error", (err) => this.onWebSocketError(err));
68
+ this.ws.on("close", (code, reason) => this.onWebSocketClose(code, reason));
69
+ }
56
70
  }
57
- /** https://discord.com/developers/docs/topics/gateway#connections */
71
+ /** https://discord.com/developers/docs/events/gateway#initiating-a-disconnect */
58
72
  disconnect() {
59
- if (this.heartbeatInterval) {
60
- clearInterval(this.heartbeatInterval);
61
- this.heartbeatInterval = null;
73
+ if (this.ws) {
74
+ if (this.heartbeatInterval) {
75
+ clearInterval(this.heartbeatInterval);
76
+ this.heartbeatInterval = null;
77
+ }
78
+ if (this.ws.readyState !== ws_1.default.CLOSED) {
79
+ this.ws.removeAllListeners();
80
+ this.ws.close(1000, "Session Invalidated - Disconnect");
81
+ this.ws = null;
82
+ }
62
83
  }
63
84
  }
64
85
  /** https://discord.com/developers/docs/topics/gateway-events#heartbeat */
65
86
  heartbeat(lastSequence) {
66
- this.ws.send(JSON.stringify({
67
- op: constants_1.GatewayOPCodes.Heartbeat,
68
- d: lastSequence,
69
- }));
87
+ if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
88
+ this.ws.send(JSON.stringify({
89
+ op: constants_1.GatewayOPCodes.Heartbeat,
90
+ d: lastSequence,
91
+ }));
92
+ }
70
93
  }
71
94
  /** https://discord.com/developers/docs/topics/gateway-events#identify */
72
95
  identify(options) {
73
- this.ws.send(JSON.stringify({
74
- op: constants_1.GatewayOPCodes.Identify,
75
- d: {
76
- token: options.token,
77
- properties: {
78
- os: options.properties.os,
79
- browser: options.properties.browser,
80
- device: options.properties.device,
96
+ if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
97
+ this.ws.send(JSON.stringify({
98
+ op: constants_1.GatewayOPCodes.Identify,
99
+ d: {
100
+ token: options.token,
101
+ properties: {
102
+ os: options.properties.os,
103
+ browser: options.properties.browser,
104
+ device: options.properties.device,
105
+ },
106
+ compress: options.compress,
107
+ large_threshold: options.largeThreshold,
108
+ shard: options.shard,
109
+ presence: options.presence,
110
+ intents: options.intents,
81
111
  },
82
- compress: options.compress,
83
- large_threshold: options.largeThreshold,
84
- shard: options.shard,
85
- presence: options.presence,
86
- intents: options.intents,
87
- },
88
- }));
112
+ }));
113
+ }
89
114
  }
90
115
  onDispatch(packet) {
116
+ this.sequence = packet.s;
91
117
  this.client.emit("dispatch", packet, this.id);
92
118
  switch (packet.t) {
93
119
  case constants_1.GatewayEvents.Ready:
94
120
  {
95
- this.sessionID = packet.d.session_id;
96
- this.resumeGatewayURL = packet.d.resume_gateway_url;
121
+ this.sessionId = packet.d.session_id;
122
+ this.resumeGatewayURL = `${packet.d.resume_gateway_url}?v=10&encoding=json`;
97
123
  this.client.user = transformers_1.Users.userFromRaw(packet.d.user);
98
124
  this.client.application = packet.d.application;
99
125
  this.client.emit("ready");
@@ -109,7 +135,7 @@ class Shard {
109
135
  opcode: constants_1.GatewayOPCodes.RequestGuildMembers,
110
136
  retryAfter: packet.d.retry_after,
111
137
  meta: {
112
- guildID: packet.d.meta.guild_id,
138
+ guildId: packet.d.meta.guild_id,
113
139
  nonce: packet.d.nonce,
114
140
  },
115
141
  });
@@ -130,21 +156,21 @@ class Shard {
130
156
  break;
131
157
  case constants_1.GatewayEvents.AutoModerationActionExecution:
132
158
  this.client.emit("autoModerationActionExecution", {
133
- guildID: packet.d.guild_id,
159
+ guildId: packet.d.guild_id,
134
160
  action: {
135
161
  type: packet.d.action.type,
136
162
  metadata: {
137
- channelID: packet.d.action.metadata.channel_id,
163
+ channelId: packet.d.action.metadata.channel_id,
138
164
  durationSeconds: packet.d.action.metadata.duration_seconds,
139
165
  customMessage: packet.d.action.metadata.custom_message,
140
166
  },
141
167
  },
142
- ruleID: packet.d.rule_id,
168
+ ruleId: packet.d.rule_id,
143
169
  ruleTriggerType: packet.d.rule_trigger_type,
144
- userID: packet.d.user_id,
145
- channelID: packet.d.channel_id,
146
- messageID: packet.d.message_id,
147
- alertSystemMessageID: packet.d.alert_system_message_id,
170
+ userId: packet.d.user_id,
171
+ channelId: packet.d.channel_id,
172
+ messageId: packet.d.message_id,
173
+ alertSystemMessageId: packet.d.alert_system_message_id,
148
174
  content: packet.d.content,
149
175
  matchedKeyword: packet.d.matched_keyword,
150
176
  matchedContent: packet.d.matched_content,
@@ -161,8 +187,8 @@ class Shard {
161
187
  break;
162
188
  case constants_1.GatewayEvents.ChannelPinsUpdate:
163
189
  this.client.emit("channelPinsUpdate", {
164
- guildID: packet.d.guild_id,
165
- channelID: packet.d.channel_id,
190
+ guildId: packet.d.guild_id,
191
+ channelId: packet.d.channel_id,
166
192
  lastPinTimestamp: packet.d.last_pin_timestamp,
167
193
  });
168
194
  break;
@@ -177,8 +203,8 @@ class Shard {
177
203
  break;
178
204
  case constants_1.GatewayEvents.ThreadListSync:
179
205
  this.client.emit("threadListSync", {
180
- guildID: packet.d.guild_id,
181
- channelIDs: packet.d.channel_ids,
206
+ guildId: packet.d.guild_id,
207
+ channelIds: packet.d.channel_ids,
182
208
  threads: packet.d.threads.map((thread) => transformers_1.Channels.channelFromRaw(thread)),
183
209
  members: packet.d.members.map((threadMember) => transformers_1.Channels.threadMemberFromRaw(threadMember)),
184
210
  });
@@ -186,22 +212,22 @@ class Shard {
186
212
  case constants_1.GatewayEvents.ThreadMemberUpdate:
187
213
  this.client.emit("threadMemberUpdate", {
188
214
  id: packet.d.id,
189
- userID: packet.d.user_id,
215
+ userId: packet.d.user_id,
190
216
  joinTimestamp: packet.d.join_timestamp,
191
217
  flags: packet.d.flags,
192
218
  member: packet.d.member !== undefined
193
219
  ? transformers_1.Guilds.guildMemberFromRaw(packet.d.member)
194
220
  : undefined,
195
- guildID: packet.d.guild_id,
221
+ guildId: packet.d.guild_id,
196
222
  });
197
223
  break;
198
224
  case constants_1.GatewayEvents.ThreadMembersUpdate:
199
225
  this.client.emit("threadMembersUpdate", {
200
226
  id: packet.d.id,
201
- guildID: packet.d.guild_id,
227
+ guildId: packet.d.guild_id,
202
228
  memberCount: packet.d.member_count,
203
229
  addedMembers: packet.d.members.map((threadMember) => transformers_1.Channels.threadMemberFromRaw(threadMember)),
204
- removedMemberIDs: packet.d.removed_member_ids,
230
+ removedMemberIds: packet.d.removed_member_ids,
205
231
  });
206
232
  break;
207
233
  case constants_1.GatewayEvents.EntitlementCreate:
@@ -239,18 +265,18 @@ class Shard {
239
265
  case constants_1.GatewayEvents.GuildAuditLogEntryCreate:
240
266
  this.client.emit("guildAuditLogEntryCreate", {
241
267
  ...transformers_1.AuditLogs.auditLogEntryFromRaw(packet.d),
242
- guildID: packet.d.guild_id,
268
+ guildId: packet.d.guild_id,
243
269
  });
244
270
  break;
245
271
  case constants_1.GatewayEvents.GuildBanAdd:
246
272
  this.client.emit("guildBanAdd", {
247
- guildID: packet.d.guild_id,
273
+ guildId: packet.d.guild_id,
248
274
  user: transformers_1.Users.userFromRaw(packet.d.user),
249
275
  });
250
276
  break;
251
277
  case constants_1.GatewayEvents.GuildBanRemove:
252
278
  this.client.emit("guildBanRemove", {
253
- guildID: packet.d.guild_id,
279
+ guildId: packet.d.guild_id,
254
280
  user: transformers_1.Users.userFromRaw(packet.d.user),
255
281
  });
256
282
  break;
@@ -266,18 +292,18 @@ class Shard {
266
292
  case constants_1.GatewayEvents.GuildMemberAdd:
267
293
  this.client.emit("guildMemberAdd", {
268
294
  ...transformers_1.Guilds.guildMemberFromRaw(packet.d),
269
- guildID: packet.d.guild_id,
295
+ guildId: packet.d.guild_id,
270
296
  });
271
297
  break;
272
298
  case constants_1.GatewayEvents.GuildMemberRemove:
273
299
  this.client.emit("guildMemberRemove", {
274
- guildID: packet.d.guild_id,
300
+ guildId: packet.d.guild_id,
275
301
  user: transformers_1.Users.userFromRaw(packet.d.user),
276
302
  });
277
303
  break;
278
304
  case constants_1.GatewayEvents.GuildMemberUpdate:
279
305
  this.client.emit("guildMemberUpdate", {
280
- guildID: packet.d.guild_id,
306
+ guildId: packet.d.guild_id,
281
307
  roles: packet.d.roles,
282
308
  user: transformers_1.Users.userFromRaw(packet.d.user),
283
309
  nick: packet.d.nick,
@@ -293,14 +319,14 @@ class Shard {
293
319
  avatarDecorationData: packet.d.avatar_decoration_data !== undefined
294
320
  ? {
295
321
  asset: packet.d.asset,
296
- skuID: packet.d.sku_id,
322
+ skuId: packet.d.sku_id,
297
323
  }
298
324
  : undefined,
299
325
  });
300
326
  break;
301
327
  case constants_1.GatewayEvents.GuildMembersChunk:
302
328
  this.client.emit("guildMembersChunk", {
303
- guildID: packet.d.guild_id,
329
+ guildId: packet.d.guild_id,
304
330
  members: packet.d.members.map((guildMember) => transformers_1.Guilds.guildMemberFromRaw(guildMember)),
305
331
  chunkIndex: packet.d.chunk_index,
306
332
  chunkCount: packet.d.chunk_count,
@@ -341,8 +367,8 @@ class Shard {
341
367
  break;
342
368
  case constants_1.GatewayEvents.GuildSoundboardSoundDelete:
343
369
  this.client.emit("guildSoundboardSoundDelete", {
344
- soundID: packet.d.sound_id,
345
- guildID: packet.d.guild_id,
370
+ soundId: packet.d.sound_id,
371
+ guildId: packet.d.guild_id,
346
372
  });
347
373
  break;
348
374
  case constants_1.GatewayEvents.GuildSoundboardSoundsUpdate:
@@ -354,20 +380,20 @@ class Shard {
354
380
  case constants_1.GatewayEvents.IntegrationCreate:
355
381
  this.client.emit("integrationCreate", {
356
382
  ...transformers_1.Guilds.integrationFromRaw(packet.d),
357
- guildID: packet.d.guild_id,
383
+ guildId: packet.d.guild_id,
358
384
  });
359
385
  break;
360
386
  case constants_1.GatewayEvents.IntegrationUpdate:
361
387
  this.client.emit("integrationUpdate", {
362
388
  ...transformers_1.Guilds.integrationFromRaw(packet.d),
363
- guildID: packet.d.guild_id,
389
+ guildId: packet.d.guild_id,
364
390
  });
365
391
  break;
366
392
  case constants_1.GatewayEvents.IntegrationDelete:
367
393
  this.client.emit("integrationDelete", {
368
394
  id: packet.d.id,
369
- guildID: packet.d.guild_id,
370
- applicationID: packet.d.application_id,
395
+ guildId: packet.d.guild_id,
396
+ applicationId: packet.d.application_id,
371
397
  });
372
398
  break;
373
399
  case constants_1.GatewayEvents.InteractionCreate:
@@ -375,10 +401,10 @@ class Shard {
375
401
  break;
376
402
  case constants_1.GatewayEvents.InviteCreate:
377
403
  this.client.emit("inviteCreate", {
378
- channelID: packet.d.channel_id,
404
+ channelId: packet.d.channel_id,
379
405
  code: packet.d.code,
380
406
  createdAt: packet.d.created_at,
381
- guildID: packet.d.guild_id,
407
+ guildId: packet.d.guild_id,
382
408
  inviter: packet.d.inviter !== undefined
383
409
  ? transformers_1.Users.userFromRaw(packet.d.inviter)
384
410
  : undefined,
@@ -398,15 +424,15 @@ class Shard {
398
424
  break;
399
425
  case constants_1.GatewayEvents.InviteDelete:
400
426
  this.client.emit("inviteDelete", {
401
- channelID: packet.d.channel_id,
402
- guildID: packet.d.guild_id,
427
+ channelId: packet.d.channel_id,
428
+ guildId: packet.d.guild_id,
403
429
  code: packet.d.code,
404
430
  });
405
431
  break;
406
432
  case constants_1.GatewayEvents.MessageCreate:
407
433
  this.client.emit("messageCreate", {
408
434
  ...transformers_1.Messages.messageFromRaw(packet.d),
409
- guildID: packet.d.guild_id,
435
+ guildId: packet.d.guild_id,
410
436
  member: packet.d.member !== undefined
411
437
  ? transformers_1.Guilds.guildMemberFromRaw(packet.d.member)
412
438
  : undefined,
@@ -419,28 +445,28 @@ class Shard {
419
445
  case constants_1.GatewayEvents.MessageDelete:
420
446
  this.client.emit("messageDelete", {
421
447
  id: packet.d.id,
422
- channelID: packet.d.channel_id,
423
- guildID: packet.d.guild_id,
448
+ channelId: packet.d.channel_id,
449
+ guildId: packet.d.guild_id,
424
450
  });
425
451
  break;
426
452
  case constants_1.GatewayEvents.MessageDeleteBulk:
427
453
  this.client.emit("messageDeleteBulk", {
428
454
  ids: packet.d.ids,
429
- channelID: packet.d.channel_id,
430
- guildID: packet.d.guild_id,
455
+ channelId: packet.d.channel_id,
456
+ guildId: packet.d.guild_id,
431
457
  });
432
458
  break;
433
459
  case constants_1.GatewayEvents.MessageReactionAdd:
434
460
  this.client.emit("messageReactionAdd", {
435
- userID: packet.d.user_id,
436
- channelID: packet.d.user_id,
437
- messageID: packet.d.user_id,
438
- guildID: packet.d.user_id,
461
+ userId: packet.d.user_id,
462
+ channelId: packet.d.user_id,
463
+ messageId: packet.d.user_id,
464
+ guildId: packet.d.user_id,
439
465
  member: packet.d.member !== undefined
440
466
  ? transformers_1.Guilds.guildMemberFromRaw(packet.d.member)
441
467
  : undefined,
442
468
  emoji: transformers_1.Emojis.emojiFromRaw(packet.d.emoji),
443
- messageAuthorID: packet.d.message_author_id,
469
+ messageAuthorId: packet.d.message_author_id,
444
470
  burst: packet.d.burst,
445
471
  burstColors: packet.d.burst_colors,
446
472
  type: packet.d.type,
@@ -448,10 +474,10 @@ class Shard {
448
474
  break;
449
475
  case constants_1.GatewayEvents.MessageReactionRemove:
450
476
  this.client.emit("messageReactionRemove", {
451
- userID: packet.d.user_id,
452
- channelID: packet.d.user_id,
453
- messageID: packet.d.user_id,
454
- guildID: packet.d.user_id,
477
+ userId: packet.d.user_id,
478
+ channelId: packet.d.user_id,
479
+ messageId: packet.d.user_id,
480
+ guildId: packet.d.user_id,
455
481
  emoji: transformers_1.Emojis.emojiFromRaw(packet.d.emoji),
456
482
  burst: packet.d.burst,
457
483
  type: packet.d.type,
@@ -459,16 +485,16 @@ class Shard {
459
485
  break;
460
486
  case constants_1.GatewayEvents.MessageReactionRemoveAll:
461
487
  this.client.emit("messageReactionRemoveAll", {
462
- channelID: packet.d.channel_id,
463
- messageID: packet.d.message_id,
464
- guildID: packet.d.guild_id,
488
+ channelId: packet.d.channel_id,
489
+ messageId: packet.d.message_id,
490
+ guildId: packet.d.guild_id,
465
491
  });
466
492
  break;
467
493
  case constants_1.GatewayEvents.MessageReactionRemoveEmoji:
468
494
  this.client.emit("messageReactionRemoveEmoji", {
469
- channelID: packet.d.channel_id,
470
- guildID: packet.d.guild_id,
471
- messageID: packet.d.message_id,
495
+ channelId: packet.d.channel_id,
496
+ guildId: packet.d.guild_id,
497
+ messageId: packet.d.message_id,
472
498
  emoji: transformers_1.Emojis.emojiFromRaw(packet.d.emoji),
473
499
  });
474
500
  break;
@@ -486,9 +512,9 @@ class Shard {
486
512
  break;
487
513
  case constants_1.GatewayEvents.TypingStart:
488
514
  this.client.emit("typingStart", {
489
- channelID: packet.d.channel_id,
490
- guildID: packet.d.guild_id,
491
- userID: packet.d.user_id,
515
+ channelId: packet.d.channel_id,
516
+ guildId: packet.d.guild_id,
517
+ userId: packet.d.user_id,
492
518
  timestamp: packet.d.timestamp,
493
519
  member: packet.d.member !== undefined
494
520
  ? transformers_1.Guilds.guildMemberFromRaw(packet.d.member)
@@ -500,15 +526,15 @@ class Shard {
500
526
  break;
501
527
  case constants_1.GatewayEvents.VoiceChannelEffectSend:
502
528
  this.client.emit("voiceChannelEffectSend", {
503
- channelID: packet.d.channel_id,
504
- guildID: packet.d.guild_id,
505
- userID: packet.d.user_id,
529
+ channelId: packet.d.channel_id,
530
+ guildId: packet.d.guild_id,
531
+ userId: packet.d.user_id,
506
532
  emoji: packet.d.emoji !== null
507
533
  ? transformers_1.Emojis.emojiFromRaw(packet.d.emoji)
508
534
  : null,
509
535
  animationType: packet.d.animation_type,
510
- animationID: packet.d.animation_id,
511
- soundID: packet.d.sound_id,
536
+ animationId: packet.d.animation_id,
537
+ soundId: packet.d.sound_id,
512
538
  soundVolume: packet.d.sound_volume,
513
539
  });
514
540
  break;
@@ -519,7 +545,7 @@ class Shard {
519
545
  {
520
546
  this.client.emit("voiceServerUpdate", {
521
547
  token: packet.d.token,
522
- guildID: packet.d.guild_id,
548
+ guildId: packet.d.guild_id,
523
549
  endpoint: packet.d.endpoint,
524
550
  });
525
551
  }
@@ -538,54 +564,63 @@ class Shard {
538
564
  break;
539
565
  case constants_1.GatewayEvents.MessagePollVoteAdd:
540
566
  this.client.emit("messagePollVoteAdd", {
541
- userID: packet.d.user_id,
542
- channelID: packet.d.channel_id,
543
- messageID: packet.d.message_id,
544
- guildID: packet.d.guild_id,
545
- answerID: packet.d.answer_id,
567
+ userId: packet.d.user_id,
568
+ channelId: packet.d.channel_id,
569
+ messageId: packet.d.message_id,
570
+ guildId: packet.d.guild_id,
571
+ answerId: packet.d.answer_id,
546
572
  });
547
573
  break;
548
574
  case constants_1.GatewayEvents.MessagePollVoteRemove:
549
575
  this.client.emit("messagePollVoteRemove", {
550
- userID: packet.d.user_id,
551
- channelID: packet.d.channel_id,
552
- messageID: packet.d.message_id,
553
- guildID: packet.d.guild_id,
554
- answerID: packet.d.answer_id,
576
+ userId: packet.d.user_id,
577
+ channelId: packet.d.channel_id,
578
+ messageId: packet.d.message_id,
579
+ guildId: packet.d.guild_id,
580
+ answerId: packet.d.answer_id,
555
581
  });
556
582
  break;
557
583
  }
558
584
  }
559
- onWebSocketOpen() {
560
- this.identify({
561
- token: this.client.token,
562
- properties: {
563
- os: this.client.properties?.os ?? process.platform,
564
- browser: this.client.properties?.browser ?? pkg.name,
565
- device: this.client.properties?.device ?? pkg.name,
566
- },
567
- compress: this.client.compress,
568
- largeThreshold: this.client.largeThreshold,
569
- shard: [this.id, this.client.shardsCount],
570
- presence: this.client.presence !== undefined
571
- ? {
572
- since: this.client.presence.status === constants_1.StatusTypes.Idle
573
- ? Date.now()
574
- : null,
575
- activities: this.client.presence.activities?.map((activity) => ({
576
- name: activity.type === constants_1.ActivityType.Custom
577
- ? "Custom Status"
578
- : activity.name,
579
- type: activity.type,
580
- url: activity.url,
581
- state: activity.state,
582
- })),
583
- status: this.client.presence.status ?? constants_1.StatusTypes.Online,
584
- afk: !!this.client.presence.afk,
585
- }
586
- : undefined,
587
- intents: this.client.intents,
588
- });
585
+ onWebSocketOpen(reconnect) {
586
+ if (reconnect) {
587
+ this.resume({
588
+ token: this.client.token,
589
+ sessionId: this.sessionId,
590
+ seq: this.sequence,
591
+ });
592
+ }
593
+ else {
594
+ this.identify({
595
+ token: this.client.token,
596
+ properties: {
597
+ os: this.client.properties?.os ?? process.platform,
598
+ browser: this.client.properties?.browser ?? pkg.name,
599
+ device: this.client.properties?.device ?? pkg.name,
600
+ },
601
+ compress: this.client.compress,
602
+ largeThreshold: this.client.largeThreshold,
603
+ shard: [this.id, this.client.shardsCount],
604
+ presence: this.client.presence !== undefined
605
+ ? {
606
+ since: this.client.presence.status === constants_1.StatusTypes.Idle
607
+ ? Date.now()
608
+ : null,
609
+ activities: this.client.presence.activities?.map((activity) => ({
610
+ name: activity.type === constants_1.ActivityType.Custom
611
+ ? "Custom Status"
612
+ : activity.name,
613
+ type: activity.type,
614
+ url: activity.url,
615
+ state: activity.state,
616
+ })),
617
+ status: this.client.presence.status ?? constants_1.StatusTypes.Online,
618
+ afk: !!this.client.presence.afk,
619
+ }
620
+ : undefined,
621
+ intents: this.client.intents,
622
+ });
623
+ }
589
624
  }
590
625
  onWebSocketMessage(data) {
591
626
  const packet = JSON.parse(data.toString());
@@ -594,10 +629,23 @@ class Shard {
594
629
  this.onDispatch(packet);
595
630
  break;
596
631
  case constants_1.GatewayOPCodes.Reconnect:
597
- this.client.emit("reconnect");
632
+ {
633
+ this.client.emit("reconnect");
634
+ this.reconnect();
635
+ }
598
636
  break;
599
637
  case constants_1.GatewayOPCodes.InvalidSession:
600
- this.client.emit("invalidSession");
638
+ {
639
+ this.client.emit("invalidSession");
640
+ if (packet.d) {
641
+ this.reconnect();
642
+ }
643
+ else if (this.ws) {
644
+ this.ws.close(1000, "Invalid Session - Identify required");
645
+ this.ws = new ws_1.default("wss://gateway.discord.gg/?v=10&encoding=json", this.client.ws);
646
+ this.connect(false);
647
+ }
648
+ }
601
649
  break;
602
650
  case constants_1.GatewayOPCodes.Hello:
603
651
  {
@@ -614,74 +662,107 @@ class Shard {
614
662
  throw err;
615
663
  }
616
664
  onWebSocketClose(code, reason) {
617
- if (code === 1000)
618
- return;
619
- throw new utils_1.GatewayError(code, reason.toString());
665
+ switch (code) {
666
+ case 1000:
667
+ this.disconnect();
668
+ break;
669
+ case constants_1.GatewayCloseEventCodes.UnknownError:
670
+ case constants_1.GatewayCloseEventCodes.UnknownOPCode:
671
+ case constants_1.GatewayCloseEventCodes.DecodeError:
672
+ case constants_1.GatewayCloseEventCodes.NotAuthenticated:
673
+ case constants_1.GatewayCloseEventCodes.AlreadyAuthenticated:
674
+ case constants_1.GatewayCloseEventCodes.InvalidSequence:
675
+ case constants_1.GatewayCloseEventCodes.RateLimited:
676
+ case constants_1.GatewayCloseEventCodes.SessionTimedOut:
677
+ this.reconnect();
678
+ break;
679
+ default:
680
+ this.disconnect();
681
+ throw new utils_1.GatewayError(code, reason.toString());
682
+ }
683
+ }
684
+ /** https://discord.com/developers/docs/events/gateway#resuming */
685
+ reconnect() {
686
+ if (this.ws && this.resumeGatewayURL && this.sessionId && this.sequence) {
687
+ this.ws.close(1000, "Resume Attempt - Reconnect");
688
+ this.ws = new ws_1.default(this.resumeGatewayURL, this.client.ws);
689
+ this.connect(true);
690
+ }
620
691
  }
621
692
  /** https://discord.com/developers/docs/topics/gateway-events#request-guild-members */
622
693
  requestGuildMembers(options) {
623
- this.ws.send(JSON.stringify({
624
- op: constants_1.GatewayOPCodes.RequestGuildMembers,
625
- d: {
626
- guild_id: options.guildID,
627
- query: options.query,
628
- limit: options.limit,
629
- presences: options.presences,
630
- user_ids: options.userIDs,
631
- nonce: options.nonce,
632
- },
633
- }));
694
+ if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
695
+ this.ws.send(JSON.stringify({
696
+ op: constants_1.GatewayOPCodes.RequestGuildMembers,
697
+ d: {
698
+ guild_id: options.guildId,
699
+ query: options.query,
700
+ limit: options.limit,
701
+ presences: options.presences,
702
+ user_ids: options.userIds,
703
+ nonce: options.nonce,
704
+ },
705
+ }));
706
+ }
634
707
  }
635
708
  /** https://discord.com/developers/docs/topics/gateway-events#request-soundboard-sounds */
636
709
  requestSoundboardSounds(options) {
637
- this.ws.send(JSON.stringify({
638
- op: constants_1.GatewayOPCodes.RequestSoundboardSounds,
639
- d: {
640
- guild_ids: options.guildIDs,
641
- },
642
- }));
710
+ if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
711
+ this.ws.send(JSON.stringify({
712
+ op: constants_1.GatewayOPCodes.RequestSoundboardSounds,
713
+ d: {
714
+ guild_ids: options.guildIds,
715
+ },
716
+ }));
717
+ }
643
718
  }
644
719
  /** https://discord.com/developers/docs/topics/gateway-events#resume */
645
720
  resume(options) {
646
- this.ws.send(JSON.stringify({
647
- op: constants_1.GatewayOPCodes.Resume,
648
- d: {
649
- token: options.token,
650
- session_id: options.sessionID,
651
- seq: options.seq,
652
- },
653
- }));
721
+ if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
722
+ this.ws.send(JSON.stringify({
723
+ op: constants_1.GatewayOPCodes.Resume,
724
+ d: {
725
+ token: options.token,
726
+ session_id: options.sessionId,
727
+ seq: options.seq,
728
+ },
729
+ }));
730
+ }
654
731
  }
655
732
  /** https://discord.com/developers/docs/topics/gateway-events#update-presence */
656
733
  updatePresence(options) {
657
- this.ws.send(JSON.stringify({
658
- op: constants_1.GatewayOPCodes.PresenceUpdate,
659
- d: {
660
- since: options.status === constants_1.StatusTypes.Idle ? Date.now() : null,
661
- activities: options.activities?.map((activity) => ({
662
- name: activity.type === constants_1.ActivityType.Custom
663
- ? "Custom Status"
664
- : activity.name,
665
- type: activity.type,
666
- url: activity.url,
667
- state: activity.state,
668
- })),
669
- status: options.status ?? constants_1.StatusTypes.Online,
670
- afk: !!options.afk,
671
- },
672
- }));
734
+ if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
735
+ this.ws.send(JSON.stringify({
736
+ op: constants_1.GatewayOPCodes.PresenceUpdate,
737
+ d: {
738
+ since: options.status === constants_1.StatusTypes.Idle ? Date.now() : null,
739
+ activities: options.activities?.map((activity) => ({
740
+ name: activity.type === constants_1.ActivityType.Custom
741
+ ? "Custom Status"
742
+ : activity.name,
743
+ type: activity.type,
744
+ url: activity.url,
745
+ state: activity.state,
746
+ })),
747
+ status: options.status ?? constants_1.StatusTypes.Online,
748
+ afk: !!options.afk,
749
+ },
750
+ }));
751
+ }
673
752
  }
674
753
  /** https://discord.com/developers/docs/topics/gateway-events#update-voice-state */
675
754
  updateVoiceState(options) {
676
- this.ws.send(JSON.stringify({
677
- op: constants_1.GatewayOPCodes.VoiceStateUpdate,
678
- d: {
679
- guild_id: options.guildID,
680
- channel_id: options.channelID,
681
- self_mute: options.selfMute,
682
- self_deaf: options.selfDeaf,
683
- },
684
- }));
755
+ if (this.ws && this.ws.readyState === ws_1.default.OPEN) {
756
+ this.ws.send(JSON.stringify({
757
+ op: constants_1.GatewayOPCodes.VoiceStateUpdate,
758
+ d: {
759
+ guild_id: options.guildId,
760
+ channel_id: options.channelId,
761
+ self_mute: options.selfMute,
762
+ self_deaf: options.selfDeaf,
763
+ },
764
+ }));
765
+ }
685
766
  }
686
767
  }
687
768
  exports.Shard = Shard;