disgroove 3.0.0-dev.bc90599 → 3.0.0-dev.fd521a8

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.
@@ -165,7 +165,7 @@ class Client extends node_events_1.default {
165
165
  : this.shardsCount;
166
166
  for (let i = 0; i < this.shardsCount; i++)
167
167
  this.shards.set(i, new gateway_1.Shard(i, this));
168
- this.shards.forEach((shard) => shard.connect());
168
+ this.shards.forEach((shard) => shard.connect(false));
169
169
  }
170
170
  /** https://discord.com/developers/docs/resources/entitlement#consume-an-entitlement */
171
171
  consumeEntitlement(applicationId, entitlementId) {
@@ -5,14 +5,17 @@ export declare class Shard {
5
5
  id: number;
6
6
  private heartbeatInterval;
7
7
  client: Client;
8
- ws: WebSocket;
8
+ ws: WebSocket | null;
9
9
  sessionId: string | null;
10
10
  resumeGatewayURL: string | null;
11
+ sequence: number | null;
11
12
  constructor(id: number, client: Client);
12
13
  /** https://discord.com/developers/docs/topics/gateway#connections */
13
- connect(): void;
14
+ connect(reconnect: boolean): void;
14
15
  /** https://discord.com/developers/docs/events/gateway#initiating-a-disconnect */
15
16
  disconnect(): void;
17
+ /** https://discord.com/developers/docs/events/gateway#resuming */
18
+ reconnect(): void;
16
19
  /** https://discord.com/developers/docs/topics/gateway-events#heartbeat */
17
20
  heartbeat(lastSequence: number | null): void;
18
21
  /** https://discord.com/developers/docs/topics/gateway-events#identify */
@@ -39,31 +39,51 @@ class Shard {
39
39
  ws;
40
40
  sessionId;
41
41
  resumeGatewayURL;
42
+ sequence;
42
43
  constructor(id, client) {
43
44
  this.id = id;
44
45
  this.heartbeatInterval = null;
45
46
  this.client = client;
46
- this.ws = new ws_1.default("wss://gateway.discord.gg/?v=10&encoding=json", client.ws);
47
+ this.ws = new ws_1.default("wss://gateway.discord.gg/?v=10&encoding=json", this.client.ws);
47
48
  this.sessionId = null;
48
49
  this.resumeGatewayURL = null;
50
+ this.sequence = null;
49
51
  }
50
52
  /** https://discord.com/developers/docs/topics/gateway#connections */
51
- connect() {
52
- this.ws.on("open", () => this.onWebSocketOpen());
53
+ connect(reconnect) {
54
+ if (!this.ws)
55
+ return;
56
+ this.ws.on("open", () => this.onWebSocketOpen(reconnect));
53
57
  this.ws.on("message", (data) => this.onWebSocketMessage(data));
54
58
  this.ws.on("error", (err) => this.onWebSocketError(err));
55
59
  this.ws.on("close", (code, reason) => this.onWebSocketClose(code, reason));
56
60
  }
57
61
  /** https://discord.com/developers/docs/events/gateway#initiating-a-disconnect */
58
62
  disconnect() {
63
+ if (!this.ws)
64
+ return;
59
65
  if (this.heartbeatInterval) {
60
66
  clearInterval(this.heartbeatInterval);
61
67
  this.heartbeatInterval = null;
62
68
  }
63
- this.ws.close(1000);
69
+ if (this.ws.readyState !== ws_1.default.CLOSED) {
70
+ this.ws.removeAllListeners();
71
+ this.ws.close(1000, "Session Invalidated - Disconnect");
72
+ this.ws = null;
73
+ }
74
+ }
75
+ /** https://discord.com/developers/docs/events/gateway#resuming */
76
+ reconnect() {
77
+ if (this.ws && this.resumeGatewayURL && this.sessionId && this.sequence) {
78
+ this.ws.close(1000, "Resume Attempt - Reconnect");
79
+ this.ws = new ws_1.default(this.resumeGatewayURL, this.client.ws);
80
+ this.connect(true);
81
+ }
64
82
  }
65
83
  /** https://discord.com/developers/docs/topics/gateway-events#heartbeat */
66
84
  heartbeat(lastSequence) {
85
+ if (!this.ws)
86
+ return;
67
87
  this.ws.send(JSON.stringify({
68
88
  op: constants_1.GatewayOPCodes.Heartbeat,
69
89
  d: lastSequence,
@@ -71,6 +91,8 @@ class Shard {
71
91
  }
72
92
  /** https://discord.com/developers/docs/topics/gateway-events#identify */
73
93
  identify(options) {
94
+ if (!this.ws)
95
+ return;
74
96
  this.ws.send(JSON.stringify({
75
97
  op: constants_1.GatewayOPCodes.Identify,
76
98
  d: {
@@ -89,12 +111,13 @@ class Shard {
89
111
  }));
90
112
  }
91
113
  onDispatch(packet) {
114
+ this.sequence = packet.s;
92
115
  this.client.emit("dispatch", packet, this.id);
93
116
  switch (packet.t) {
94
117
  case constants_1.GatewayEvents.Ready:
95
118
  {
96
119
  this.sessionId = packet.d.session_id;
97
- this.resumeGatewayURL = packet.d.resume_gateway_url;
120
+ this.resumeGatewayURL = `${packet.d.resume_gateway_url}?v=10&encoding=json`;
98
121
  this.client.user = transformers_1.Users.userFromRaw(packet.d.user);
99
122
  this.client.application = packet.d.application;
100
123
  this.client.emit("ready");
@@ -557,36 +580,45 @@ class Shard {
557
580
  break;
558
581
  }
559
582
  }
560
- onWebSocketOpen() {
561
- this.identify({
562
- token: this.client.token,
563
- properties: {
564
- os: this.client.properties?.os ?? process.platform,
565
- browser: this.client.properties?.browser ?? pkg.name,
566
- device: this.client.properties?.device ?? pkg.name,
567
- },
568
- compress: this.client.compress,
569
- largeThreshold: this.client.largeThreshold,
570
- shard: [this.id, this.client.shardsCount],
571
- presence: this.client.presence !== undefined
572
- ? {
573
- since: this.client.presence.status === constants_1.StatusTypes.Idle
574
- ? Date.now()
575
- : null,
576
- activities: this.client.presence.activities?.map((activity) => ({
577
- name: activity.type === constants_1.ActivityType.Custom
578
- ? "Custom Status"
579
- : activity.name,
580
- type: activity.type,
581
- url: activity.url,
582
- state: activity.state,
583
- })),
584
- status: this.client.presence.status ?? constants_1.StatusTypes.Online,
585
- afk: !!this.client.presence.afk,
586
- }
587
- : undefined,
588
- intents: this.client.intents,
589
- });
583
+ onWebSocketOpen(reconnect) {
584
+ if (reconnect) {
585
+ this.resume({
586
+ token: this.client.token,
587
+ sessionId: this.sessionId,
588
+ seq: this.sequence,
589
+ });
590
+ }
591
+ else {
592
+ this.identify({
593
+ token: this.client.token,
594
+ properties: {
595
+ os: this.client.properties?.os ?? process.platform,
596
+ browser: this.client.properties?.browser ?? pkg.name,
597
+ device: this.client.properties?.device ?? pkg.name,
598
+ },
599
+ compress: this.client.compress,
600
+ largeThreshold: this.client.largeThreshold,
601
+ shard: [this.id, this.client.shardsCount],
602
+ presence: this.client.presence !== undefined
603
+ ? {
604
+ since: this.client.presence.status === constants_1.StatusTypes.Idle
605
+ ? Date.now()
606
+ : null,
607
+ activities: this.client.presence.activities?.map((activity) => ({
608
+ name: activity.type === constants_1.ActivityType.Custom
609
+ ? "Custom Status"
610
+ : activity.name,
611
+ type: activity.type,
612
+ url: activity.url,
613
+ state: activity.state,
614
+ })),
615
+ status: this.client.presence.status ?? constants_1.StatusTypes.Online,
616
+ afk: !!this.client.presence.afk,
617
+ }
618
+ : undefined,
619
+ intents: this.client.intents,
620
+ });
621
+ }
590
622
  }
591
623
  onWebSocketMessage(data) {
592
624
  const packet = JSON.parse(data.toString());
@@ -595,10 +627,23 @@ class Shard {
595
627
  this.onDispatch(packet);
596
628
  break;
597
629
  case constants_1.GatewayOPCodes.Reconnect:
598
- this.client.emit("reconnect");
630
+ {
631
+ this.client.emit("reconnect");
632
+ this.reconnect();
633
+ }
599
634
  break;
600
635
  case constants_1.GatewayOPCodes.InvalidSession:
601
- this.client.emit("invalidSession");
636
+ {
637
+ this.client.emit("invalidSession");
638
+ if (packet.d) {
639
+ this.reconnect();
640
+ }
641
+ else if (this.ws) {
642
+ this.ws.close(1000, "Invalid Session - Identify required");
643
+ this.ws = new ws_1.default("wss://gateway.discord.gg/?v=10&encoding=json", this.client.ws);
644
+ this.connect(false);
645
+ }
646
+ }
602
647
  break;
603
648
  case constants_1.GatewayOPCodes.Hello:
604
649
  {
@@ -615,12 +660,27 @@ class Shard {
615
660
  throw err;
616
661
  }
617
662
  onWebSocketClose(code, reason) {
618
- if (code === 1000)
619
- return;
620
- throw new utils_1.GatewayError(code, reason.toString());
663
+ switch (code) {
664
+ case 1000:
665
+ break;
666
+ case constants_1.GatewayCloseEventCodes.UnknownError:
667
+ case constants_1.GatewayCloseEventCodes.UnknownOPCode:
668
+ case constants_1.GatewayCloseEventCodes.DecodeError:
669
+ case constants_1.GatewayCloseEventCodes.NotAuthenticated:
670
+ case constants_1.GatewayCloseEventCodes.AlreadyAuthenticated:
671
+ case constants_1.GatewayCloseEventCodes.InvalidSequence:
672
+ case constants_1.GatewayCloseEventCodes.RateLimited:
673
+ case constants_1.GatewayCloseEventCodes.SessionTimedOut:
674
+ this.reconnect();
675
+ break;
676
+ default:
677
+ throw new utils_1.GatewayError(code, reason.toString());
678
+ }
621
679
  }
622
680
  /** https://discord.com/developers/docs/topics/gateway-events#request-guild-members */
623
681
  requestGuildMembers(options) {
682
+ if (!this.ws)
683
+ return;
624
684
  this.ws.send(JSON.stringify({
625
685
  op: constants_1.GatewayOPCodes.RequestGuildMembers,
626
686
  d: {
@@ -635,6 +695,8 @@ class Shard {
635
695
  }
636
696
  /** https://discord.com/developers/docs/topics/gateway-events#request-soundboard-sounds */
637
697
  requestSoundboardSounds(options) {
698
+ if (!this.ws)
699
+ return;
638
700
  this.ws.send(JSON.stringify({
639
701
  op: constants_1.GatewayOPCodes.RequestSoundboardSounds,
640
702
  d: {
@@ -644,6 +706,8 @@ class Shard {
644
706
  }
645
707
  /** https://discord.com/developers/docs/topics/gateway-events#resume */
646
708
  resume(options) {
709
+ if (!this.ws)
710
+ return;
647
711
  this.ws.send(JSON.stringify({
648
712
  op: constants_1.GatewayOPCodes.Resume,
649
713
  d: {
@@ -655,6 +719,8 @@ class Shard {
655
719
  }
656
720
  /** https://discord.com/developers/docs/topics/gateway-events#update-presence */
657
721
  updatePresence(options) {
722
+ if (!this.ws)
723
+ return;
658
724
  this.ws.send(JSON.stringify({
659
725
  op: constants_1.GatewayOPCodes.PresenceUpdate,
660
726
  d: {
@@ -674,6 +740,8 @@ class Shard {
674
740
  }
675
741
  /** https://discord.com/developers/docs/topics/gateway-events#update-voice-state */
676
742
  updateVoiceState(options) {
743
+ if (!this.ws)
744
+ return;
677
745
  this.ws.send(JSON.stringify({
678
746
  op: constants_1.GatewayOPCodes.VoiceStateUpdate,
679
747
  d: {
@@ -2,4 +2,6 @@ import type { RawApplication, Application } from "../types/application";
2
2
  export declare class Applications {
3
3
  static applicationFromRaw(application: RawApplication): Application;
4
4
  static applicationToRaw(application: Application): RawApplication;
5
+ static partialApplicationFromRaw(application: Partial<RawApplication>): Partial<Application>;
6
+ static partialApplicationToRaw(application: Partial<Application>): Partial<RawApplication>;
5
7
  }
@@ -103,5 +103,111 @@ class Applications {
103
103
  custom_install_url: application.customInstallURL,
104
104
  };
105
105
  }
106
+ static partialApplicationFromRaw(application) {
107
+ return {
108
+ id: application.id,
109
+ name: application.name,
110
+ icon: application.icon,
111
+ description: application.description,
112
+ rpcOrigins: application.rpc_origins,
113
+ botPublic: application.bot_public,
114
+ botRequireCodeGrant: application.bot_require_code_grant,
115
+ termsOfServiceURL: application.terms_of_service_url,
116
+ privacyPolicyURL: application.privacy_policy_url,
117
+ owner: application.owner !== undefined
118
+ ? Users_1.Users.userFromRaw(application.owner)
119
+ : undefined,
120
+ verifyKey: application.verify_key,
121
+ team: application.team !== undefined
122
+ ? application.team !== null
123
+ ? Teams_1.Teams.teamFromRaw(application.team)
124
+ : null
125
+ : undefined,
126
+ guildId: application.guild_id,
127
+ guild: application.guild !== undefined
128
+ ? Guilds_1.Guilds.guildFromRaw(application.guild)
129
+ : undefined,
130
+ primarySKUId: application.primary_sku_id,
131
+ slug: application.slug,
132
+ coverImage: application.cover_image,
133
+ flags: application.flags,
134
+ approximateGuildCount: application.approximate_guild_count,
135
+ approximateUserInstallCount: application.approximate_user_install_count,
136
+ approximateUserAuthorizationCount: application.approximate_user_authorization_count,
137
+ redirectURIs: application.redirect_uris,
138
+ interactionsEndpointURL: application.interactions_endpoint_url,
139
+ roleConnectionsVerificationURL: application.role_connections_verification_url,
140
+ eventWebhooksURL: application.event_webhooks_url,
141
+ eventWebhooksStatus: application.event_webhooks_status,
142
+ eventWebhooksTypes: application.event_webhooks_types,
143
+ tags: application.tags,
144
+ installParams: application.install_params,
145
+ integrationTypesConfig: application.integration_types_config !== undefined
146
+ ? {
147
+ "0": {
148
+ oauth2InstallParams: application.integration_types_config?.[0]
149
+ .oauth2_install_params,
150
+ },
151
+ "1": {
152
+ oauth2InstallParams: application.integration_types_config?.[1]
153
+ .oauth2_install_params,
154
+ },
155
+ }
156
+ : undefined,
157
+ customInstallURL: application.custom_install_url,
158
+ };
159
+ }
160
+ static partialApplicationToRaw(application) {
161
+ return {
162
+ id: application.id,
163
+ name: application.name,
164
+ icon: application.icon,
165
+ description: application.description,
166
+ rpc_origins: application.rpcOrigins,
167
+ bot_public: application.botPublic,
168
+ bot_require_code_grant: application.botRequireCodeGrant,
169
+ terms_of_service_url: application.termsOfServiceURL,
170
+ privacy_policy_url: application.privacyPolicyURL,
171
+ owner: application.owner !== undefined
172
+ ? Users_1.Users.userToRaw(application.owner)
173
+ : undefined,
174
+ verify_key: application.verifyKey,
175
+ team: application.team !== undefined
176
+ ? application.team !== null
177
+ ? Teams_1.Teams.teamToRaw(application.team)
178
+ : null
179
+ : undefined,
180
+ guild_id: application.guildId,
181
+ guild: application.guild !== undefined
182
+ ? Guilds_1.Guilds.guildToRaw(application.guild)
183
+ : undefined,
184
+ primary_sku_id: application.primarySKUId,
185
+ slug: application.slug,
186
+ cover_image: application.coverImage,
187
+ flags: application.flags,
188
+ approximate_guild_count: application.approximateGuildCount,
189
+ approximate_user_install_count: application.approximateUserInstallCount,
190
+ approximate_user_authorization_count: application.approximateUserAuthorizationCount,
191
+ redirect_uris: application.redirectURIs,
192
+ interactions_endpoint_url: application.interactionsEndpointURL,
193
+ role_connections_verification_url: application.roleConnectionsVerificationURL,
194
+ event_webhooks_url: application.eventWebhooksURL,
195
+ event_webhooks_status: application.eventWebhooksStatus,
196
+ event_webhooks_types: application.eventWebhooksTypes,
197
+ tags: application.tags,
198
+ install_params: application.installParams,
199
+ integration_types_config: application.integrationTypesConfig !== undefined
200
+ ? {
201
+ "0": {
202
+ oauth2_install_params: application.integrationTypesConfig?.[0].oauth2InstallParams,
203
+ },
204
+ "1": {
205
+ oauth2_install_params: application.integrationTypesConfig?.[1].oauth2InstallParams,
206
+ },
207
+ }
208
+ : undefined,
209
+ custom_install_url: application.customInstallURL,
210
+ };
211
+ }
106
212
  }
107
213
  exports.Applications = Applications;
@@ -226,7 +226,7 @@ class Messages {
226
226
  type: message.type,
227
227
  activity: message.activity,
228
228
  application: message.application !== undefined
229
- ? Applications_1.Applications.applicationFromRaw(message.application)
229
+ ? Applications_1.Applications.partialApplicationFromRaw(message.application)
230
230
  : undefined,
231
231
  applicationId: message.application_id,
232
232
  flags: message.flags,
@@ -334,7 +334,7 @@ class Messages {
334
334
  type: message.type,
335
335
  activity: message.activity,
336
336
  application: message.application !== undefined
337
- ? Applications_1.Applications.applicationToRaw(message.application)
337
+ ? Applications_1.Applications.partialApplicationToRaw(message.application)
338
338
  : undefined,
339
339
  application_id: message.applicationId,
340
340
  flags: message.flags,
@@ -29,7 +29,7 @@ export interface RawMessage {
29
29
  webhook_id?: snowflake;
30
30
  type: MessageTypes;
31
31
  activity?: RawMessageActivity;
32
- application?: RawApplication;
32
+ application?: Partial<RawApplication>;
33
33
  application_id?: snowflake;
34
34
  flags?: MessageFlags;
35
35
  message_reference?: RawMessageReference;
@@ -228,7 +228,7 @@ export interface Message {
228
228
  webhookId?: snowflake;
229
229
  type: MessageTypes;
230
230
  activity?: MessageActivity;
231
- application?: Application;
231
+ application?: Partial<Application>;
232
232
  applicationId?: snowflake;
233
233
  flags?: MessageFlags;
234
234
  messageReference?: MessageReference;
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "disgroove",
3
- "version": "3.0.0-dev.bc90599",
3
+ "version": "3.0.0-dev.fd521a8",
4
4
  "description": "A module to interface with Discord",
5
5
  "main": "./dist/lib/index.js",
6
6
  "types": "./dist/lib/index.d.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "disgroove",
3
- "version": "3.0.0-dev.bc90599",
3
+ "version": "3.0.0-dev.fd521a8",
4
4
  "description": "A module to interface with Discord",
5
5
  "main": "./dist/lib/index.js",
6
6
  "types": "./dist/lib/index.d.ts",