disgroove 2.2.7-dev.fca4921 → 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 (86) hide show
  1. package/LICENSE +9 -9
  2. package/README.md +48 -48
  3. package/dist/lib/Client.d.ts +291 -257
  4. package/dist/lib/Client.js +554 -460
  5. package/dist/lib/constants.d.ts +29 -21
  6. package/dist/lib/constants.js +33 -24
  7. package/dist/lib/gateway/Shard.d.ts +7 -4
  8. package/dist/lib/gateway/Shard.js +293 -197
  9. package/dist/lib/index.d.ts +5 -1
  10. package/dist/lib/index.js +5 -1
  11. package/dist/lib/rest/Endpoints.d.ts +95 -91
  12. package/dist/lib/rest/Endpoints.js +111 -102
  13. package/dist/lib/rest/RequestManager.d.ts +1 -3
  14. package/dist/lib/rest/RequestManager.js +17 -7
  15. package/dist/lib/rest/index.js +17 -7
  16. package/dist/lib/transformers/ApplicationCommands.js +4 -4
  17. package/dist/lib/transformers/Applications.d.ts +2 -0
  18. package/dist/lib/transformers/Applications.js +110 -4
  19. package/dist/lib/transformers/AuditLogs.js +10 -10
  20. package/dist/lib/transformers/AutoModeration.js +6 -6
  21. package/dist/lib/transformers/Channels.js +16 -16
  22. package/dist/lib/transformers/Components.d.ts +13 -3
  23. package/dist/lib/transformers/Components.js +283 -156
  24. package/dist/lib/transformers/Entitlements.d.ts +2 -2
  25. package/dist/lib/transformers/Entitlements.js +16 -16
  26. package/dist/lib/transformers/GuildScheduledEvents.js +8 -8
  27. package/dist/lib/transformers/GuildTemplates.js +4 -4
  28. package/dist/lib/transformers/Guilds.js +28 -28
  29. package/dist/lib/transformers/Interactions.js +300 -55
  30. package/dist/lib/transformers/Lobbies.d.ts +7 -0
  31. package/dist/lib/transformers/Lobbies.js +38 -0
  32. package/dist/lib/transformers/Messages.d.ts +4 -3
  33. package/dist/lib/transformers/Messages.js +38 -52
  34. package/dist/lib/transformers/Polls.js +2 -2
  35. package/dist/lib/transformers/Presences.js +6 -6
  36. package/dist/lib/transformers/Roles.js +8 -8
  37. package/dist/lib/transformers/SKUs.js +2 -2
  38. package/dist/lib/transformers/Soundboards.js +6 -6
  39. package/dist/lib/transformers/StageInstances.js +6 -6
  40. package/dist/lib/transformers/Stickers.js +3 -3
  41. package/dist/lib/transformers/Subscriptions.js +8 -8
  42. package/dist/lib/transformers/Teams.js +4 -4
  43. package/dist/lib/transformers/Users.js +6 -6
  44. package/dist/lib/transformers/Voice.js +8 -8
  45. package/dist/lib/transformers/Webhooks.js +6 -6
  46. package/dist/lib/transformers/index.d.ts +2 -1
  47. package/dist/lib/transformers/index.js +2 -1
  48. package/dist/lib/types/application-command.d.ts +9 -4
  49. package/dist/lib/types/application-role-connection-metadata.d.ts +1 -0
  50. package/dist/lib/types/application.d.ts +12 -7
  51. package/dist/lib/types/audit-log.d.ts +9 -5
  52. package/dist/lib/types/auto-moderation.d.ts +7 -3
  53. package/dist/lib/types/channel.d.ts +17 -23
  54. package/dist/lib/types/common.d.ts +2 -0
  55. package/dist/lib/types/components.d.ts +478 -0
  56. package/dist/lib/types/emoji.d.ts +1 -0
  57. package/dist/lib/types/entitlements.d.ts +5 -4
  58. package/dist/lib/types/gateway-events.d.ts +203 -130
  59. package/dist/lib/types/guild-scheduled-event.d.ts +10 -5
  60. package/dist/lib/types/guild-template.d.ts +3 -2
  61. package/dist/lib/types/guild.d.ts +40 -22
  62. package/dist/lib/types/interaction.d.ts +35 -18
  63. package/dist/lib/types/invite.d.ts +5 -2
  64. package/dist/lib/types/lobby.d.ts +31 -0
  65. package/dist/lib/types/lobby.js +2 -0
  66. package/dist/lib/types/message.d.ts +32 -20
  67. package/dist/lib/types/poll.d.ts +7 -1
  68. package/dist/lib/types/role.d.ts +8 -5
  69. package/dist/lib/types/sku.d.ts +2 -1
  70. package/dist/lib/types/soundboard.d.ts +4 -3
  71. package/dist/lib/types/stage-instance.d.ts +4 -3
  72. package/dist/lib/types/sticker.d.ts +8 -5
  73. package/dist/lib/types/subscription.d.ts +6 -5
  74. package/dist/lib/types/team.d.ts +4 -2
  75. package/dist/lib/types/user.d.ts +10 -3
  76. package/dist/lib/types/voice.d.ts +6 -4
  77. package/dist/lib/types/webhook.d.ts +4 -3
  78. package/dist/lib/utils/CDN.d.ts +22 -22
  79. package/dist/lib/utils/CDN.js +22 -22
  80. package/dist/lib/utils/formatters.d.ts +7 -7
  81. package/dist/lib/utils/formatters.js +28 -29
  82. package/dist/lib/utils/index.js +17 -7
  83. package/dist/package.json +4 -4
  84. package/package.json +5 -5
  85. package/dist/lib/types/message-components.d.ts +0 -234
  86. /package/dist/lib/types/{message-components.js → components.js} +0 -0
@@ -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");
@@ -102,6 +128,20 @@ class Shard {
102
128
  case constants_1.GatewayEvents.Resumed:
103
129
  this.client.emit("resumed");
104
130
  break;
131
+ case constants_1.GatewayEvents.RateLimited:
132
+ switch (packet.d.opcode) {
133
+ case constants_1.GatewayOPCodes.RequestGuildMembers: {
134
+ this.client.emit("rateLimited", {
135
+ opcode: constants_1.GatewayOPCodes.RequestGuildMembers,
136
+ retryAfter: packet.d.retry_after,
137
+ meta: {
138
+ guildId: packet.d.meta.guild_id,
139
+ nonce: packet.d.nonce,
140
+ },
141
+ });
142
+ }
143
+ }
144
+ break;
105
145
  case constants_1.GatewayEvents.ApplicationCommandPermissionsUpdate:
106
146
  this.client.emit("applicationCommandPermissionsUpdate", transformers_1.Guilds.guildApplicationCommandPermissionsFromRaw(packet.d));
107
147
  break;
@@ -116,21 +156,21 @@ class Shard {
116
156
  break;
117
157
  case constants_1.GatewayEvents.AutoModerationActionExecution:
118
158
  this.client.emit("autoModerationActionExecution", {
119
- guildID: packet.d.guild_id,
159
+ guildId: packet.d.guild_id,
120
160
  action: {
121
161
  type: packet.d.action.type,
122
162
  metadata: {
123
- channelID: packet.d.action.metadata.channel_id,
163
+ channelId: packet.d.action.metadata.channel_id,
124
164
  durationSeconds: packet.d.action.metadata.duration_seconds,
125
165
  customMessage: packet.d.action.metadata.custom_message,
126
166
  },
127
167
  },
128
- ruleID: packet.d.rule_id,
168
+ ruleId: packet.d.rule_id,
129
169
  ruleTriggerType: packet.d.rule_trigger_type,
130
- userID: packet.d.user_id,
131
- channelID: packet.d.channel_id,
132
- messageID: packet.d.message_id,
133
- 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,
134
174
  content: packet.d.content,
135
175
  matchedKeyword: packet.d.matched_keyword,
136
176
  matchedContent: packet.d.matched_content,
@@ -147,8 +187,8 @@ class Shard {
147
187
  break;
148
188
  case constants_1.GatewayEvents.ChannelPinsUpdate:
149
189
  this.client.emit("channelPinsUpdate", {
150
- guildID: packet.d.guild_id,
151
- channelID: packet.d.channel_id,
190
+ guildId: packet.d.guild_id,
191
+ channelId: packet.d.channel_id,
152
192
  lastPinTimestamp: packet.d.last_pin_timestamp,
153
193
  });
154
194
  break;
@@ -163,8 +203,8 @@ class Shard {
163
203
  break;
164
204
  case constants_1.GatewayEvents.ThreadListSync:
165
205
  this.client.emit("threadListSync", {
166
- guildID: packet.d.guild_id,
167
- channelIDs: packet.d.channel_ids,
206
+ guildId: packet.d.guild_id,
207
+ channelIds: packet.d.channel_ids,
168
208
  threads: packet.d.threads.map((thread) => transformers_1.Channels.channelFromRaw(thread)),
169
209
  members: packet.d.members.map((threadMember) => transformers_1.Channels.threadMemberFromRaw(threadMember)),
170
210
  });
@@ -172,22 +212,22 @@ class Shard {
172
212
  case constants_1.GatewayEvents.ThreadMemberUpdate:
173
213
  this.client.emit("threadMemberUpdate", {
174
214
  id: packet.d.id,
175
- userID: packet.d.user_id,
215
+ userId: packet.d.user_id,
176
216
  joinTimestamp: packet.d.join_timestamp,
177
217
  flags: packet.d.flags,
178
218
  member: packet.d.member !== undefined
179
219
  ? transformers_1.Guilds.guildMemberFromRaw(packet.d.member)
180
220
  : undefined,
181
- guildID: packet.d.guild_id,
221
+ guildId: packet.d.guild_id,
182
222
  });
183
223
  break;
184
224
  case constants_1.GatewayEvents.ThreadMembersUpdate:
185
225
  this.client.emit("threadMembersUpdate", {
186
226
  id: packet.d.id,
187
- guildID: packet.d.guild_id,
227
+ guildId: packet.d.guild_id,
188
228
  memberCount: packet.d.member_count,
189
229
  addedMembers: packet.d.members.map((threadMember) => transformers_1.Channels.threadMemberFromRaw(threadMember)),
190
- removedMemberIDs: packet.d.removed_member_ids,
230
+ removedMemberIds: packet.d.removed_member_ids,
191
231
  });
192
232
  break;
193
233
  case constants_1.GatewayEvents.EntitlementCreate:
@@ -225,18 +265,18 @@ class Shard {
225
265
  case constants_1.GatewayEvents.GuildAuditLogEntryCreate:
226
266
  this.client.emit("guildAuditLogEntryCreate", {
227
267
  ...transformers_1.AuditLogs.auditLogEntryFromRaw(packet.d),
228
- guildID: packet.d.guild_id,
268
+ guildId: packet.d.guild_id,
229
269
  });
230
270
  break;
231
271
  case constants_1.GatewayEvents.GuildBanAdd:
232
272
  this.client.emit("guildBanAdd", {
233
- guildID: packet.d.guild_id,
273
+ guildId: packet.d.guild_id,
234
274
  user: transformers_1.Users.userFromRaw(packet.d.user),
235
275
  });
236
276
  break;
237
277
  case constants_1.GatewayEvents.GuildBanRemove:
238
278
  this.client.emit("guildBanRemove", {
239
- guildID: packet.d.guild_id,
279
+ guildId: packet.d.guild_id,
240
280
  user: transformers_1.Users.userFromRaw(packet.d.user),
241
281
  });
242
282
  break;
@@ -252,18 +292,18 @@ class Shard {
252
292
  case constants_1.GatewayEvents.GuildMemberAdd:
253
293
  this.client.emit("guildMemberAdd", {
254
294
  ...transformers_1.Guilds.guildMemberFromRaw(packet.d),
255
- guildID: packet.d.guild_id,
295
+ guildId: packet.d.guild_id,
256
296
  });
257
297
  break;
258
298
  case constants_1.GatewayEvents.GuildMemberRemove:
259
299
  this.client.emit("guildMemberRemove", {
260
- guildID: packet.d.guild_id,
300
+ guildId: packet.d.guild_id,
261
301
  user: transformers_1.Users.userFromRaw(packet.d.user),
262
302
  });
263
303
  break;
264
304
  case constants_1.GatewayEvents.GuildMemberUpdate:
265
305
  this.client.emit("guildMemberUpdate", {
266
- guildID: packet.d.guild_id,
306
+ guildId: packet.d.guild_id,
267
307
  roles: packet.d.roles,
268
308
  user: transformers_1.Users.userFromRaw(packet.d.user),
269
309
  nick: packet.d.nick,
@@ -279,14 +319,14 @@ class Shard {
279
319
  avatarDecorationData: packet.d.avatar_decoration_data !== undefined
280
320
  ? {
281
321
  asset: packet.d.asset,
282
- skuID: packet.d.sku_id,
322
+ skuId: packet.d.sku_id,
283
323
  }
284
324
  : undefined,
285
325
  });
286
326
  break;
287
327
  case constants_1.GatewayEvents.GuildMembersChunk:
288
328
  this.client.emit("guildMembersChunk", {
289
- guildID: packet.d.guild_id,
329
+ guildId: packet.d.guild_id,
290
330
  members: packet.d.members.map((guildMember) => transformers_1.Guilds.guildMemberFromRaw(guildMember)),
291
331
  chunkIndex: packet.d.chunk_index,
292
332
  chunkCount: packet.d.chunk_count,
@@ -327,8 +367,8 @@ class Shard {
327
367
  break;
328
368
  case constants_1.GatewayEvents.GuildSoundboardSoundDelete:
329
369
  this.client.emit("guildSoundboardSoundDelete", {
330
- soundID: packet.d.sound_id,
331
- guildID: packet.d.guild_id,
370
+ soundId: packet.d.sound_id,
371
+ guildId: packet.d.guild_id,
332
372
  });
333
373
  break;
334
374
  case constants_1.GatewayEvents.GuildSoundboardSoundsUpdate:
@@ -340,20 +380,20 @@ class Shard {
340
380
  case constants_1.GatewayEvents.IntegrationCreate:
341
381
  this.client.emit("integrationCreate", {
342
382
  ...transformers_1.Guilds.integrationFromRaw(packet.d),
343
- guildID: packet.d.guild_id,
383
+ guildId: packet.d.guild_id,
344
384
  });
345
385
  break;
346
386
  case constants_1.GatewayEvents.IntegrationUpdate:
347
387
  this.client.emit("integrationUpdate", {
348
388
  ...transformers_1.Guilds.integrationFromRaw(packet.d),
349
- guildID: packet.d.guild_id,
389
+ guildId: packet.d.guild_id,
350
390
  });
351
391
  break;
352
392
  case constants_1.GatewayEvents.IntegrationDelete:
353
393
  this.client.emit("integrationDelete", {
354
394
  id: packet.d.id,
355
- guildID: packet.d.guild_id,
356
- applicationID: packet.d.application_id,
395
+ guildId: packet.d.guild_id,
396
+ applicationId: packet.d.application_id,
357
397
  });
358
398
  break;
359
399
  case constants_1.GatewayEvents.InteractionCreate:
@@ -361,10 +401,10 @@ class Shard {
361
401
  break;
362
402
  case constants_1.GatewayEvents.InviteCreate:
363
403
  this.client.emit("inviteCreate", {
364
- channelID: packet.d.channel_id,
404
+ channelId: packet.d.channel_id,
365
405
  code: packet.d.code,
366
406
  createdAt: packet.d.created_at,
367
- guildID: packet.d.guild_id,
407
+ guildId: packet.d.guild_id,
368
408
  inviter: packet.d.inviter !== undefined
369
409
  ? transformers_1.Users.userFromRaw(packet.d.inviter)
370
410
  : undefined,
@@ -379,19 +419,20 @@ class Shard {
379
419
  : undefined,
380
420
  temporary: packet.d.temporary,
381
421
  uses: packet.d.uses,
422
+ expiresAt: packet.d.expires_at,
382
423
  });
383
424
  break;
384
425
  case constants_1.GatewayEvents.InviteDelete:
385
426
  this.client.emit("inviteDelete", {
386
- channelID: packet.d.channel_id,
387
- guildID: packet.d.guild_id,
427
+ channelId: packet.d.channel_id,
428
+ guildId: packet.d.guild_id,
388
429
  code: packet.d.code,
389
430
  });
390
431
  break;
391
432
  case constants_1.GatewayEvents.MessageCreate:
392
433
  this.client.emit("messageCreate", {
393
434
  ...transformers_1.Messages.messageFromRaw(packet.d),
394
- guildID: packet.d.guild_id,
435
+ guildId: packet.d.guild_id,
395
436
  member: packet.d.member !== undefined
396
437
  ? transformers_1.Guilds.guildMemberFromRaw(packet.d.member)
397
438
  : undefined,
@@ -404,28 +445,28 @@ class Shard {
404
445
  case constants_1.GatewayEvents.MessageDelete:
405
446
  this.client.emit("messageDelete", {
406
447
  id: packet.d.id,
407
- channelID: packet.d.channel_id,
408
- guildID: packet.d.guild_id,
448
+ channelId: packet.d.channel_id,
449
+ guildId: packet.d.guild_id,
409
450
  });
410
451
  break;
411
452
  case constants_1.GatewayEvents.MessageDeleteBulk:
412
453
  this.client.emit("messageDeleteBulk", {
413
454
  ids: packet.d.ids,
414
- channelID: packet.d.channel_id,
415
- guildID: packet.d.guild_id,
455
+ channelId: packet.d.channel_id,
456
+ guildId: packet.d.guild_id,
416
457
  });
417
458
  break;
418
459
  case constants_1.GatewayEvents.MessageReactionAdd:
419
460
  this.client.emit("messageReactionAdd", {
420
- userID: packet.d.user_id,
421
- channelID: packet.d.user_id,
422
- messageID: packet.d.user_id,
423
- 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,
424
465
  member: packet.d.member !== undefined
425
466
  ? transformers_1.Guilds.guildMemberFromRaw(packet.d.member)
426
467
  : undefined,
427
468
  emoji: transformers_1.Emojis.emojiFromRaw(packet.d.emoji),
428
- messageAuthorID: packet.d.message_author_id,
469
+ messageAuthorId: packet.d.message_author_id,
429
470
  burst: packet.d.burst,
430
471
  burstColors: packet.d.burst_colors,
431
472
  type: packet.d.type,
@@ -433,10 +474,10 @@ class Shard {
433
474
  break;
434
475
  case constants_1.GatewayEvents.MessageReactionRemove:
435
476
  this.client.emit("messageReactionRemove", {
436
- userID: packet.d.user_id,
437
- channelID: packet.d.user_id,
438
- messageID: packet.d.user_id,
439
- 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,
440
481
  emoji: transformers_1.Emojis.emojiFromRaw(packet.d.emoji),
441
482
  burst: packet.d.burst,
442
483
  type: packet.d.type,
@@ -444,16 +485,16 @@ class Shard {
444
485
  break;
445
486
  case constants_1.GatewayEvents.MessageReactionRemoveAll:
446
487
  this.client.emit("messageReactionRemoveAll", {
447
- channelID: packet.d.channel_id,
448
- messageID: packet.d.message_id,
449
- guildID: packet.d.guild_id,
488
+ channelId: packet.d.channel_id,
489
+ messageId: packet.d.message_id,
490
+ guildId: packet.d.guild_id,
450
491
  });
451
492
  break;
452
493
  case constants_1.GatewayEvents.MessageReactionRemoveEmoji:
453
494
  this.client.emit("messageReactionRemoveEmoji", {
454
- channelID: packet.d.channel_id,
455
- guildID: packet.d.guild_id,
456
- messageID: packet.d.message_id,
495
+ channelId: packet.d.channel_id,
496
+ guildId: packet.d.guild_id,
497
+ messageId: packet.d.message_id,
457
498
  emoji: transformers_1.Emojis.emojiFromRaw(packet.d.emoji),
458
499
  });
459
500
  break;
@@ -471,9 +512,9 @@ class Shard {
471
512
  break;
472
513
  case constants_1.GatewayEvents.TypingStart:
473
514
  this.client.emit("typingStart", {
474
- channelID: packet.d.channel_id,
475
- guildID: packet.d.guild_id,
476
- userID: packet.d.user_id,
515
+ channelId: packet.d.channel_id,
516
+ guildId: packet.d.guild_id,
517
+ userId: packet.d.user_id,
477
518
  timestamp: packet.d.timestamp,
478
519
  member: packet.d.member !== undefined
479
520
  ? transformers_1.Guilds.guildMemberFromRaw(packet.d.member)
@@ -485,15 +526,15 @@ class Shard {
485
526
  break;
486
527
  case constants_1.GatewayEvents.VoiceChannelEffectSend:
487
528
  this.client.emit("voiceChannelEffectSend", {
488
- channelID: packet.d.channel_id,
489
- guildID: packet.d.guild_id,
490
- userID: packet.d.user_id,
529
+ channelId: packet.d.channel_id,
530
+ guildId: packet.d.guild_id,
531
+ userId: packet.d.user_id,
491
532
  emoji: packet.d.emoji !== null
492
533
  ? transformers_1.Emojis.emojiFromRaw(packet.d.emoji)
493
534
  : null,
494
535
  animationType: packet.d.animation_type,
495
- animationID: packet.d.animation_id,
496
- soundID: packet.d.sound_id,
536
+ animationId: packet.d.animation_id,
537
+ soundId: packet.d.sound_id,
497
538
  soundVolume: packet.d.sound_volume,
498
539
  });
499
540
  break;
@@ -504,7 +545,7 @@ class Shard {
504
545
  {
505
546
  this.client.emit("voiceServerUpdate", {
506
547
  token: packet.d.token,
507
- guildID: packet.d.guild_id,
548
+ guildId: packet.d.guild_id,
508
549
  endpoint: packet.d.endpoint,
509
550
  });
510
551
  }
@@ -523,54 +564,63 @@ class Shard {
523
564
  break;
524
565
  case constants_1.GatewayEvents.MessagePollVoteAdd:
525
566
  this.client.emit("messagePollVoteAdd", {
526
- userID: packet.d.user_id,
527
- channelID: packet.d.channel_id,
528
- messageID: packet.d.message_id,
529
- guildID: packet.d.guild_id,
530
- 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,
531
572
  });
532
573
  break;
533
574
  case constants_1.GatewayEvents.MessagePollVoteRemove:
534
575
  this.client.emit("messagePollVoteRemove", {
535
- userID: packet.d.user_id,
536
- channelID: packet.d.channel_id,
537
- messageID: packet.d.message_id,
538
- guildID: packet.d.guild_id,
539
- 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,
540
581
  });
541
582
  break;
542
583
  }
543
584
  }
544
- onWebSocketOpen() {
545
- this.identify({
546
- token: this.client.token,
547
- properties: {
548
- os: this.client.properties?.os ?? process.platform,
549
- browser: this.client.properties?.browser ?? pkg.name,
550
- device: this.client.properties?.device ?? pkg.name,
551
- },
552
- compress: this.client.compress,
553
- largeThreshold: this.client.largeThreshold,
554
- shard: [this.id, this.client.shardsCount],
555
- presence: this.client.presence !== undefined
556
- ? {
557
- since: this.client.presence.status === constants_1.StatusTypes.Idle
558
- ? Date.now()
559
- : null,
560
- activities: this.client.presence.activities?.map((activity) => ({
561
- name: activity.type === constants_1.ActivityType.Custom
562
- ? "Custom Status"
563
- : activity.name,
564
- type: activity.type,
565
- url: activity.url,
566
- state: activity.state,
567
- })),
568
- status: this.client.presence.status ?? constants_1.StatusTypes.Online,
569
- afk: !!this.client.presence.afk,
570
- }
571
- : undefined,
572
- intents: this.client.intents,
573
- });
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
+ }
574
624
  }
575
625
  onWebSocketMessage(data) {
576
626
  const packet = JSON.parse(data.toString());
@@ -579,10 +629,23 @@ class Shard {
579
629
  this.onDispatch(packet);
580
630
  break;
581
631
  case constants_1.GatewayOPCodes.Reconnect:
582
- this.client.emit("reconnect");
632
+ {
633
+ this.client.emit("reconnect");
634
+ this.reconnect();
635
+ }
583
636
  break;
584
637
  case constants_1.GatewayOPCodes.InvalidSession:
585
- 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
+ }
586
649
  break;
587
650
  case constants_1.GatewayOPCodes.Hello:
588
651
  {
@@ -599,74 +662,107 @@ class Shard {
599
662
  throw err;
600
663
  }
601
664
  onWebSocketClose(code, reason) {
602
- if (code === 1000)
603
- return;
604
- 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
+ }
605
691
  }
606
692
  /** https://discord.com/developers/docs/topics/gateway-events#request-guild-members */
607
693
  requestGuildMembers(options) {
608
- this.ws.send(JSON.stringify({
609
- op: constants_1.GatewayOPCodes.RequestGuildMembers,
610
- d: {
611
- guild_id: options.guildID,
612
- query: options.query,
613
- limit: options.limit,
614
- presences: options.presences,
615
- user_ids: options.userIDs,
616
- nonce: options.nonce,
617
- },
618
- }));
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
+ }
619
707
  }
620
708
  /** https://discord.com/developers/docs/topics/gateway-events#request-soundboard-sounds */
621
709
  requestSoundboardSounds(options) {
622
- this.ws.send(JSON.stringify({
623
- op: constants_1.GatewayOPCodes.RequestSoundboardSounds,
624
- d: {
625
- guild_ids: options.guildIDs,
626
- },
627
- }));
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
+ }
628
718
  }
629
719
  /** https://discord.com/developers/docs/topics/gateway-events#resume */
630
720
  resume(options) {
631
- this.ws.send(JSON.stringify({
632
- op: constants_1.GatewayOPCodes.Resume,
633
- d: {
634
- token: options.token,
635
- session_id: options.sessionID,
636
- seq: options.seq,
637
- },
638
- }));
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
+ }
639
731
  }
640
732
  /** https://discord.com/developers/docs/topics/gateway-events#update-presence */
641
733
  updatePresence(options) {
642
- this.ws.send(JSON.stringify({
643
- op: constants_1.GatewayOPCodes.PresenceUpdate,
644
- d: {
645
- since: options.status === constants_1.StatusTypes.Idle ? Date.now() : null,
646
- activities: options.activities?.map((activity) => ({
647
- name: activity.type === constants_1.ActivityType.Custom
648
- ? "Custom Status"
649
- : activity.name,
650
- type: activity.type,
651
- url: activity.url,
652
- state: activity.state,
653
- })),
654
- status: options.status ?? constants_1.StatusTypes.Online,
655
- afk: !!options.afk,
656
- },
657
- }));
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
+ }
658
752
  }
659
753
  /** https://discord.com/developers/docs/topics/gateway-events#update-voice-state */
660
754
  updateVoiceState(options) {
661
- this.ws.send(JSON.stringify({
662
- op: constants_1.GatewayOPCodes.VoiceStateUpdate,
663
- d: {
664
- guild_id: options.guildID,
665
- channel_id: options.channelID,
666
- self_mute: options.selfMute,
667
- self_deaf: options.selfDeaf,
668
- },
669
- }));
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
+ }
670
766
  }
671
767
  }
672
768
  exports.Shard = Shard;