cloudstorm 0.4.0 → 0.4.1
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.
- package/README.md +2 -4
- package/dist/Client.js +6 -16
- package/dist/Intents.d.ts +1 -0
- package/dist/Intents.js +2 -1
- package/dist/Shard.d.ts +0 -1
- package/dist/Shard.js +2 -9
- package/dist/ShardManager.js +12 -30
- package/dist/Types.d.ts +4 -31
- package/dist/connector/DiscordConnector.d.ts +4 -4
- package/dist/connector/DiscordConnector.js +63 -93
- package/dist/structures/BetterWs.d.ts +17 -54
- package/dist/structures/BetterWs.js +500 -154
- package/dist/structures/RatelimitBucket.js +3 -7
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -14
package/README.md
CHANGED
|
@@ -17,9 +17,7 @@ const { Client } = require("cloudstorm");
|
|
|
17
17
|
const bot = new Client(token, { intents: ["GUILDS"] });
|
|
18
18
|
const startup = async () => {
|
|
19
19
|
await bot.connect();
|
|
20
|
-
bot.on("ready", () =>
|
|
21
|
-
console.log("Bot received ready event");
|
|
22
|
-
});
|
|
20
|
+
bot.on("ready", () => console.log("Bot received ready event"););
|
|
23
21
|
};
|
|
24
22
|
startup().catch(e => {
|
|
25
23
|
console.error("Error on startup!");
|
|
@@ -44,7 +42,7 @@ So an event you receive may look like this:
|
|
|
44
42
|
"roles": [],
|
|
45
43
|
"status": "offline",
|
|
46
44
|
"user": {
|
|
47
|
-
|
|
45
|
+
"id": "id"
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
48
|
}
|
package/dist/Client.js
CHANGED
|
@@ -4,13 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
const version = require("../package.json").version;
|
|
6
6
|
const events_1 = require("events");
|
|
7
|
-
let Erlpack;
|
|
8
|
-
try {
|
|
9
|
-
Erlpack = require("erlpack");
|
|
10
|
-
}
|
|
11
|
-
catch (e) {
|
|
12
|
-
Erlpack = null;
|
|
13
|
-
}
|
|
14
7
|
const Constants_1 = __importDefault(require("./Constants"));
|
|
15
8
|
const snowtransfer_1 = require("snowtransfer");
|
|
16
9
|
const ShardManager_1 = __importDefault(require("./ShardManager"));
|
|
@@ -24,9 +17,8 @@ class Client extends events_1.EventEmitter {
|
|
|
24
17
|
*/
|
|
25
18
|
constructor(token, options = {}) {
|
|
26
19
|
super();
|
|
27
|
-
if (!token)
|
|
20
|
+
if (!token)
|
|
28
21
|
throw new Error("Missing token!");
|
|
29
|
-
}
|
|
30
22
|
this.options = {
|
|
31
23
|
largeGuildThreshold: 250,
|
|
32
24
|
firstShardId: 0,
|
|
@@ -37,7 +29,7 @@ class Client extends events_1.EventEmitter {
|
|
|
37
29
|
token: "",
|
|
38
30
|
ws: {
|
|
39
31
|
compress: true,
|
|
40
|
-
|
|
32
|
+
encoding: "json"
|
|
41
33
|
}
|
|
42
34
|
};
|
|
43
35
|
this._restClient = options.snowtransferInstance ? options.snowtransferInstance : new snowtransfer_1.SnowTransfer(token);
|
|
@@ -95,8 +87,7 @@ class Client extends events_1.EventEmitter {
|
|
|
95
87
|
* });
|
|
96
88
|
*/
|
|
97
89
|
async presenceUpdate(data) {
|
|
98
|
-
|
|
99
|
-
void undefined;
|
|
90
|
+
return this.shardManager.presenceUpdate(data);
|
|
100
91
|
}
|
|
101
92
|
/**
|
|
102
93
|
* Send an OP 3 PRESENCE_UPDATE to Discord, which updates the status of a single shard facilitated by this client's ShardManager.
|
|
@@ -159,9 +150,8 @@ class Client extends events_1.EventEmitter {
|
|
|
159
150
|
* });
|
|
160
151
|
*/
|
|
161
152
|
requestGuildMembers(shardId, data) {
|
|
162
|
-
if (!data.guild_id)
|
|
153
|
+
if (!data.guild_id)
|
|
163
154
|
throw new Error("You need to pass a guild_id");
|
|
164
|
-
}
|
|
165
155
|
return this.shardManager.requestGuildMembers(shardId, data);
|
|
166
156
|
}
|
|
167
157
|
/**
|
|
@@ -169,8 +159,8 @@ class Client extends events_1.EventEmitter {
|
|
|
169
159
|
* @param gatewayUrl Base gateway wss url to update the cached endpoint to.
|
|
170
160
|
*/
|
|
171
161
|
_updateEndpoint(gatewayUrl) {
|
|
172
|
-
var _a;
|
|
173
|
-
this.options.endpoint = `${gatewayUrl}?v=${Constants_1.default.GATEWAY_VERSION}&encoding=${
|
|
162
|
+
var _a, _b;
|
|
163
|
+
this.options.endpoint = `${gatewayUrl}?v=${Constants_1.default.GATEWAY_VERSION}&encoding=${((_a = this.options.ws) === null || _a === void 0 ? void 0 : _a.encoding) === "etf" ? "etf" : "json"}${((_b = this.options.ws) === null || _b === void 0 ? void 0 : _b.compress) ? "&compress=zlib-stream" : ""}`;
|
|
174
164
|
}
|
|
175
165
|
}
|
|
176
166
|
Client.default = Client;
|
package/dist/Intents.d.ts
CHANGED
package/dist/Intents.js
CHANGED
|
@@ -17,8 +17,9 @@ exports.flags = {
|
|
|
17
17
|
DIRECT_MESSAGES: 1 << 12,
|
|
18
18
|
DIRECT_MESSAGE_REACTIONS: 1 << 13,
|
|
19
19
|
DIRECT_MESSAGE_TYPING: 1 << 14,
|
|
20
|
+
MESSAGE_CONTENT: 1 << 15,
|
|
20
21
|
};
|
|
21
|
-
exports.privileged = exports.flags.GUILD_MEMBERS | exports.flags.GUILD_PRESENCES | exports.flags.GUILD_MESSAGES;
|
|
22
|
+
exports.privileged = exports.flags.GUILD_MEMBERS | exports.flags.GUILD_PRESENCES | exports.flags.GUILD_MESSAGES | exports.flags.MESSAGE_CONTENT;
|
|
22
23
|
exports.all = Object.values(exports.flags).reduce((acc, p) => acc | p, 0);
|
|
23
24
|
exports.non_privileged = exports.all & ~exports.privileged;
|
|
24
25
|
function resolve(bit = 0) {
|
package/dist/Shard.d.ts
CHANGED
package/dist/Shard.js
CHANGED
|
@@ -40,15 +40,8 @@ class Shard extends events_1.EventEmitter {
|
|
|
40
40
|
this.ready = false;
|
|
41
41
|
this.emit("disconnect", ...args);
|
|
42
42
|
});
|
|
43
|
-
this.connector.on("
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
this.connector.on("ready", (resume) => {
|
|
47
|
-
this.emit("ready", resume);
|
|
48
|
-
});
|
|
49
|
-
this.connector.on("queueIdentify", () => {
|
|
50
|
-
this.emit("queueIdentify", this.id);
|
|
51
|
-
});
|
|
43
|
+
this.connector.on("ready", (resume) => this.emit("ready", resume));
|
|
44
|
+
this.connector.on("queueIdentify", () => this.emit("queueIdentify", this.id));
|
|
52
45
|
}
|
|
53
46
|
/**
|
|
54
47
|
* Time in ms it took for Discord to ackknowledge an OP 1 HEARTBEAT.
|
package/dist/ShardManager.js
CHANGED
|
@@ -15,9 +15,8 @@ class ShardManager {
|
|
|
15
15
|
constructor(client) {
|
|
16
16
|
this.client = client;
|
|
17
17
|
this.options = client.options;
|
|
18
|
-
if (!this.options.connectQueueInterval)
|
|
18
|
+
if (!this.options.connectQueueInterval)
|
|
19
19
|
this.options.connectQueueInterval = 1000 * 5;
|
|
20
|
-
}
|
|
21
20
|
this.shards = {};
|
|
22
21
|
this.connectQueue = [];
|
|
23
22
|
this.lastConnectionAttempt = null;
|
|
@@ -92,9 +91,6 @@ class ShardManager {
|
|
|
92
91
|
this.client.emit("shardReady", { id: shard.id, ready: !resume });
|
|
93
92
|
this._checkReady();
|
|
94
93
|
});
|
|
95
|
-
shard.on("error", (error) => {
|
|
96
|
-
this.client.emit("error", error);
|
|
97
|
-
});
|
|
98
94
|
shard.on("disconnect", (code, reason, gracefulClose) => {
|
|
99
95
|
this.client.emit("debug", `Websocket of shard ${shard.id} closed with code ${code} and reason: ${reason ? reason : "None"}`);
|
|
100
96
|
if (code === 1000 && gracefulClose) {
|
|
@@ -117,9 +113,8 @@ class ShardManager {
|
|
|
117
113
|
_checkReady() {
|
|
118
114
|
for (const shardId in this.shards) {
|
|
119
115
|
if (this.shards[shardId]) {
|
|
120
|
-
if (!this.shards[shardId].ready)
|
|
116
|
+
if (!this.shards[shardId].ready)
|
|
121
117
|
return;
|
|
122
|
-
}
|
|
123
118
|
}
|
|
124
119
|
}
|
|
125
120
|
this.client.emit("ready");
|
|
@@ -130,9 +125,8 @@ class ShardManager {
|
|
|
130
125
|
_checkDisconnect() {
|
|
131
126
|
for (const shardId in this.shards) {
|
|
132
127
|
if (this.shards[shardId]) {
|
|
133
|
-
if (this.shards[shardId].connector.status !== "disconnected")
|
|
128
|
+
if (this.shards[shardId].connector.status !== "disconnected")
|
|
134
129
|
return;
|
|
135
|
-
}
|
|
136
130
|
}
|
|
137
131
|
}
|
|
138
132
|
this.client.emit("disconnected");
|
|
@@ -157,14 +151,10 @@ class ShardManager {
|
|
|
157
151
|
shardPresenceUpdate(shardId, data) {
|
|
158
152
|
return new Promise((res, rej) => {
|
|
159
153
|
const shard = this.shards[shardId];
|
|
160
|
-
if (!shard)
|
|
154
|
+
if (!shard)
|
|
161
155
|
rej(new Error(`Shard ${shardId} does not exist`));
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
shard.once("ready", () => {
|
|
165
|
-
shard.presenceUpdate(data).then(result => res(result)).catch(e => rej(e));
|
|
166
|
-
});
|
|
167
|
-
}
|
|
156
|
+
if (!shard.ready)
|
|
157
|
+
shard.once("ready", () => shard.presenceUpdate(data).then(result => res(result)).catch(e => rej(e)));
|
|
168
158
|
shard.presenceUpdate(data).then(result => res(result)).catch(e => rej(e));
|
|
169
159
|
});
|
|
170
160
|
}
|
|
@@ -176,14 +166,10 @@ class ShardManager {
|
|
|
176
166
|
voiceStateUpdate(shardId, data) {
|
|
177
167
|
return new Promise((res, rej) => {
|
|
178
168
|
const shard = this.shards[shardId];
|
|
179
|
-
if (!shard)
|
|
169
|
+
if (!shard)
|
|
180
170
|
rej(new Error(`Shard ${shardId} does not exist`));
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
shard.once("ready", () => {
|
|
184
|
-
shard.voiceStateUpdate(data).then(result => res(result)).catch(e => rej(e));
|
|
185
|
-
});
|
|
186
|
-
}
|
|
171
|
+
if (!shard.ready)
|
|
172
|
+
shard.once("ready", () => shard.voiceStateUpdate(data).then(result => res(result)).catch(e => rej(e)));
|
|
187
173
|
shard.voiceStateUpdate(data).then(result => res(result)).catch(e => rej(e));
|
|
188
174
|
});
|
|
189
175
|
}
|
|
@@ -195,14 +181,10 @@ class ShardManager {
|
|
|
195
181
|
requestGuildMembers(shardId, data) {
|
|
196
182
|
return new Promise((res, rej) => {
|
|
197
183
|
const shard = this.shards[shardId];
|
|
198
|
-
if (!shard)
|
|
184
|
+
if (!shard)
|
|
199
185
|
rej(new Error(`Shard ${shardId} does not exist`));
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
shard.once("ready", () => {
|
|
203
|
-
shard.requestGuildMembers(data).then(result => res(result)).catch(e => rej(e));
|
|
204
|
-
});
|
|
205
|
-
}
|
|
186
|
+
if (!shard.ready)
|
|
187
|
+
shard.once("ready", () => shard.requestGuildMembers(data).then(result => res(result)).catch(e => rej(e)));
|
|
206
188
|
shard.requestGuildMembers(data).then(result => res(result)).catch(e => rej(e));
|
|
207
189
|
});
|
|
208
190
|
}
|
package/dist/Types.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import Constants from "./Constants";
|
|
2
1
|
export interface IntentFlags {
|
|
3
2
|
GUILDS: number;
|
|
4
3
|
GUILD_MEMBERS: number;
|
|
@@ -18,53 +17,27 @@ export interface IntentFlags {
|
|
|
18
17
|
}
|
|
19
18
|
export declare type IntentResolvable = number | Array<number> | keyof IntentFlags | Array<keyof IntentFlags>;
|
|
20
19
|
export interface IWSMessage {
|
|
21
|
-
op:
|
|
20
|
+
op: import("discord-typings").GatewayOpcode;
|
|
22
21
|
d?: any;
|
|
23
22
|
s?: number;
|
|
24
|
-
t?:
|
|
23
|
+
t?: import("discord-typings").GatewayEvent;
|
|
25
24
|
}
|
|
26
25
|
export interface IGatewayMessage extends IWSMessage {
|
|
27
26
|
shard_id: number;
|
|
28
27
|
}
|
|
29
|
-
export interface IPresenceActivity {
|
|
30
|
-
name: string;
|
|
31
|
-
type?: 0 | 1 | 2 | 3 | 5;
|
|
32
|
-
url?: string;
|
|
33
|
-
}
|
|
34
|
-
export interface IPresence {
|
|
35
|
-
status?: "online" | "idle" | "dnd" | "offline";
|
|
36
|
-
afk?: boolean;
|
|
37
|
-
since?: boolean;
|
|
38
|
-
activities?: Array<IPresenceActivity> | null;
|
|
39
|
-
}
|
|
40
28
|
export interface IClientOptions {
|
|
41
29
|
largeGuildThreshold?: number;
|
|
42
30
|
firstShardId?: number;
|
|
43
31
|
lastShardId?: number;
|
|
44
32
|
shardAmount?: number;
|
|
45
33
|
reconnect?: boolean;
|
|
46
|
-
initialPresence?:
|
|
34
|
+
initialPresence?: import("discord-typings").GatewayPresenceUpdate;
|
|
47
35
|
intents?: IntentResolvable;
|
|
48
36
|
connectQueueInterval?: number;
|
|
49
37
|
snowtransferInstance?: import("snowtransfer").SnowTransfer;
|
|
50
38
|
ws?: IClientWSOptions;
|
|
51
39
|
}
|
|
52
|
-
export interface IVoiceStateUpdate {
|
|
53
|
-
guild_id: string;
|
|
54
|
-
channel_id?: string | null;
|
|
55
|
-
self_mute?: boolean;
|
|
56
|
-
self_deaf?: boolean;
|
|
57
|
-
}
|
|
58
|
-
export interface IRequestGuildMembers {
|
|
59
|
-
guild_id: string;
|
|
60
|
-
query?: string | null;
|
|
61
|
-
limit?: number;
|
|
62
|
-
}
|
|
63
|
-
export interface IShardReady {
|
|
64
|
-
id: number;
|
|
65
|
-
ready: boolean;
|
|
66
|
-
}
|
|
67
40
|
export interface IClientWSOptions {
|
|
68
41
|
compress?: boolean;
|
|
69
|
-
|
|
42
|
+
encoding?: "etf" | "json";
|
|
70
43
|
}
|
|
@@ -5,8 +5,8 @@ interface ConnectorEvents {
|
|
|
5
5
|
queueIdentify: [number];
|
|
6
6
|
event: [import("../Types").IWSMessage];
|
|
7
7
|
ready: [boolean];
|
|
8
|
-
error: [string];
|
|
9
8
|
disconnect: [number, string, boolean];
|
|
9
|
+
stateChange: ["connecting" | "identifying" | "resuming" | "ready" | "disconnected"];
|
|
10
10
|
}
|
|
11
11
|
interface DiscordConnector {
|
|
12
12
|
addListener<E extends keyof ConnectorEvents>(event: E, listener: (...args: ConnectorEvents[E]) => any): this;
|
|
@@ -33,12 +33,12 @@ declare class DiscordConnector extends EventEmitter {
|
|
|
33
33
|
client: import("../Client");
|
|
34
34
|
options: import("../Client")["options"];
|
|
35
35
|
reconnect: boolean;
|
|
36
|
-
betterWs: BetterWs
|
|
36
|
+
betterWs: BetterWs;
|
|
37
37
|
heartbeatTimeout: NodeJS.Timeout | null;
|
|
38
38
|
heartbeatInterval: number;
|
|
39
39
|
_trace: string | null;
|
|
40
40
|
seq: number;
|
|
41
|
-
status:
|
|
41
|
+
status: "connecting" | "identifying" | "resuming" | "ready" | "disconnected";
|
|
42
42
|
sessionId: string | null;
|
|
43
43
|
lastACKAt: number;
|
|
44
44
|
lastHeartbeatSend: number;
|
|
@@ -53,7 +53,7 @@ declare class DiscordConnector extends EventEmitter {
|
|
|
53
53
|
/**
|
|
54
54
|
* Connect to Discord.
|
|
55
55
|
*/
|
|
56
|
-
connect(): void
|
|
56
|
+
connect(): Promise<void>;
|
|
57
57
|
/**
|
|
58
58
|
* Close the websocket connection and disconnect.
|
|
59
59
|
*/
|
|
@@ -6,7 +6,6 @@ const events_1 = require("events");
|
|
|
6
6
|
const BetterWs_1 = __importDefault(require("../structures/BetterWs"));
|
|
7
7
|
const Constants_1 = require("../Constants");
|
|
8
8
|
const Intents_1 = __importDefault(require("../Intents"));
|
|
9
|
-
const ws_1 = __importDefault(require("ws"));
|
|
10
9
|
let reconnecting = false;
|
|
11
10
|
/**
|
|
12
11
|
* Class used for acting based on received events.
|
|
@@ -21,12 +20,11 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
21
20
|
*/
|
|
22
21
|
constructor(id, client) {
|
|
23
22
|
super();
|
|
24
|
-
this.betterWs = null;
|
|
25
23
|
this.heartbeatTimeout = null;
|
|
26
24
|
this.heartbeatInterval = 0;
|
|
27
25
|
this._trace = null;
|
|
28
26
|
this.seq = 0;
|
|
29
|
-
this.status = "
|
|
27
|
+
this.status = "disconnected";
|
|
30
28
|
this.sessionId = null;
|
|
31
29
|
this.lastACKAt = 0;
|
|
32
30
|
this.lastHeartbeatSend = 0;
|
|
@@ -35,37 +33,28 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
35
33
|
this.client = client;
|
|
36
34
|
this.options = client.options;
|
|
37
35
|
this.reconnect = this.options.reconnect || true;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Connect to Discord.
|
|
41
|
-
*/
|
|
42
|
-
connect() {
|
|
43
|
-
if (!this.betterWs) {
|
|
44
|
-
this.betterWs = new BetterWs_1.default(this.options.endpoint, this.options.ws);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
this.betterWs.removeAllListeners();
|
|
48
|
-
this.betterWs.recreateWs(this.options.endpoint, this.options.ws);
|
|
49
|
-
}
|
|
36
|
+
this.betterWs = new BetterWs_1.default(this.options.endpoint, this.options.ws);
|
|
50
37
|
this.betterWs.on("ws_open", () => {
|
|
51
38
|
this.status = "connecting";
|
|
39
|
+
this.emit("stateChange", "connecting");
|
|
52
40
|
reconnecting = false;
|
|
53
41
|
});
|
|
54
42
|
this.betterWs.on("ws_message", msg => this.messageAction(msg));
|
|
55
43
|
this.betterWs.on("ws_close", (code, reason) => this.handleWsClose(code, reason));
|
|
56
|
-
this.betterWs.on("debug", event =>
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
44
|
+
this.betterWs.on("debug", event => this.client.emit("debug", event));
|
|
45
|
+
this.betterWs.on("debug_send", data => this.client.emit("rawSend", data));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Connect to Discord.
|
|
49
|
+
*/
|
|
50
|
+
connect() {
|
|
51
|
+
return this.betterWs.connect();
|
|
62
52
|
}
|
|
63
53
|
/**
|
|
64
54
|
* Close the websocket connection and disconnect.
|
|
65
55
|
*/
|
|
66
56
|
async disconnect() {
|
|
67
|
-
|
|
68
|
-
return (_a = this.betterWs) === null || _a === void 0 ? void 0 : _a.close(1000, "Disconnected by User");
|
|
57
|
+
return this.betterWs.close();
|
|
69
58
|
}
|
|
70
59
|
/**
|
|
71
60
|
* Called with a parsed Websocket message to execute further actions.
|
|
@@ -96,9 +85,8 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
96
85
|
this.disconnect();
|
|
97
86
|
break;
|
|
98
87
|
case Constants_1.GATEWAY_OP_CODES.INVALID_SESSION:
|
|
99
|
-
if (message.d && this.sessionId)
|
|
88
|
+
if (message.d && this.sessionId)
|
|
100
89
|
this.resume();
|
|
101
|
-
}
|
|
102
90
|
else {
|
|
103
91
|
this.seq = 0;
|
|
104
92
|
this.sessionId = "";
|
|
@@ -116,9 +104,8 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
116
104
|
else
|
|
117
105
|
this.disconnect();
|
|
118
106
|
}
|
|
119
|
-
else
|
|
107
|
+
else
|
|
120
108
|
this.heartbeat();
|
|
121
|
-
}
|
|
122
109
|
}, this.heartbeatInterval);
|
|
123
110
|
this._trace = message.d._trace;
|
|
124
111
|
this.identify();
|
|
@@ -137,24 +124,15 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
137
124
|
* @param resume Whether or not the client intends to send an OP 6 RESUME later.
|
|
138
125
|
*/
|
|
139
126
|
async _reconnect(resume = false) {
|
|
140
|
-
var _a, _b, _c;
|
|
141
127
|
if (resume)
|
|
142
128
|
reconnecting = true;
|
|
143
|
-
if (
|
|
144
|
-
this.emit("error", `Client was attempting to ${resume ? "resume" : "reconnect"} while the WebSocket was still in the connecting state. This should never happen
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
this.connect();
|
|
148
|
-
}
|
|
149
|
-
// This is for instances where the gateway asks the client to reconnect. The ws would be closed by the time the code reaches here.
|
|
150
|
-
if (((_b = this.betterWs) === null || _b === void 0 ? void 0 : _b.ws.readyState) === ws_1.default.OPEN)
|
|
151
|
-
await ((_c = this.betterWs) === null || _c === void 0 ? void 0 : _c.close(resume ? 4000 : 1012, "reconnecting"));
|
|
152
|
-
if (resume) {
|
|
129
|
+
if (this.betterWs.status === 2)
|
|
130
|
+
void this.client.emit("error", `Client was attempting to ${resume ? "resume" : "reconnect"} while the WebSocket was still in the connecting state. This should never happen.`);
|
|
131
|
+
await this.betterWs.close();
|
|
132
|
+
if (resume)
|
|
153
133
|
this.clearHeartBeat();
|
|
154
|
-
|
|
155
|
-
else {
|
|
134
|
+
else
|
|
156
135
|
this.reset();
|
|
157
|
-
}
|
|
158
136
|
this.connect();
|
|
159
137
|
}
|
|
160
138
|
/**
|
|
@@ -181,10 +159,12 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
181
159
|
* @param force Whether CloudStorm should send an OP 2 IDENTIFY even if there's a session that could be resumed.
|
|
182
160
|
*/
|
|
183
161
|
async identify(force) {
|
|
184
|
-
|
|
185
|
-
|
|
162
|
+
if (this.betterWs.status !== 1)
|
|
163
|
+
void this.client.emit("debug", "Client was attempting to identify when the ws was not open");
|
|
164
|
+
if (this.sessionId && !force)
|
|
186
165
|
return this.resume();
|
|
187
|
-
|
|
166
|
+
this.status = "identifying";
|
|
167
|
+
this.emit("stateChange", "identifying");
|
|
188
168
|
const data = {
|
|
189
169
|
op: Constants_1.GATEWAY_OP_CODES.IDENTIFY,
|
|
190
170
|
d: {
|
|
@@ -201,14 +181,17 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
201
181
|
};
|
|
202
182
|
if (this.options.initialPresence)
|
|
203
183
|
Object.assign(data.d, { presence: this._checkPresenceData(this.options.initialPresence) });
|
|
204
|
-
return
|
|
184
|
+
return this.betterWs.sendMessage(data);
|
|
205
185
|
}
|
|
206
186
|
/**
|
|
207
187
|
* Send an OP 6 RESUME to the gateway.
|
|
208
188
|
*/
|
|
209
189
|
async resume() {
|
|
210
|
-
|
|
211
|
-
|
|
190
|
+
if (this.betterWs.status !== 1)
|
|
191
|
+
void this.client.emit("debug", "Client was attempting to resume when the ws was not open");
|
|
192
|
+
this.status = "resuming";
|
|
193
|
+
this.emit("stateChange", "resuming");
|
|
194
|
+
return this.betterWs.sendMessage({
|
|
212
195
|
op: Constants_1.GATEWAY_OP_CODES.RESUME,
|
|
213
196
|
d: { seq: this.seq, token: this.options.token, session_id: this.sessionId }
|
|
214
197
|
});
|
|
@@ -217,10 +200,9 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
217
200
|
* Send an OP 1 HEARTBEAT to the gateway.
|
|
218
201
|
*/
|
|
219
202
|
heartbeat() {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
(_b = this.betterWs) === null || _b === void 0 ? void 0 : _b.sendMessage({ op: Constants_1.GATEWAY_OP_CODES.HEARTBEAT, d: this.seq });
|
|
203
|
+
if (this.betterWs.status !== 1)
|
|
204
|
+
void this.client.emit("debug", "Client was attempting to heartbeat when the ws was not open");
|
|
205
|
+
this.betterWs.sendMessage({ op: Constants_1.GATEWAY_OP_CODES.HEARTBEAT, d: this.seq });
|
|
224
206
|
this.lastHeartbeatSend = Date.now();
|
|
225
207
|
}
|
|
226
208
|
/**
|
|
@@ -231,10 +213,10 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
231
213
|
switch (message.t) {
|
|
232
214
|
case "READY":
|
|
233
215
|
case "RESUMED":
|
|
234
|
-
if (message.t === "READY")
|
|
216
|
+
if (message.t === "READY")
|
|
235
217
|
this.sessionId = message.d.session_id;
|
|
236
|
-
}
|
|
237
218
|
this.status = "ready";
|
|
219
|
+
this.emit("stateChange", "ready");
|
|
238
220
|
this._trace = message.d._trace;
|
|
239
221
|
this.emit("ready", message.t === "RESUMED");
|
|
240
222
|
this.emit("event", message);
|
|
@@ -249,94 +231,86 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
249
231
|
* @param reason Close reason if any.
|
|
250
232
|
*/
|
|
251
233
|
handleWsClose(code, reason) {
|
|
252
|
-
var _a;
|
|
253
234
|
let gracefulClose = false;
|
|
254
235
|
this.status = "disconnected";
|
|
236
|
+
this.emit("stateChange", "disconnected");
|
|
255
237
|
// Disallowed Intents.
|
|
256
|
-
if (code === 4014)
|
|
257
|
-
this.emit("error", "Disallowed Intents, check your client options and application page.");
|
|
258
|
-
}
|
|
238
|
+
if (code === 4014)
|
|
239
|
+
this.client.emit("error", "Disallowed Intents, check your client options and application page.");
|
|
259
240
|
// Invalid Intents.
|
|
260
|
-
if (code === 4013)
|
|
261
|
-
this.emit("error", "Invalid Intents data, check your client options.");
|
|
262
|
-
}
|
|
241
|
+
if (code === 4013)
|
|
242
|
+
this.client.emit("error", "Invalid Intents data, check your client options.");
|
|
263
243
|
// Invalid API version.
|
|
264
|
-
if (code === 4012)
|
|
265
|
-
this.emit("error", "Invalid API version.");
|
|
266
|
-
}
|
|
244
|
+
if (code === 4012)
|
|
245
|
+
this.client.emit("error", "Invalid API version.");
|
|
267
246
|
// Sharding required.
|
|
268
|
-
if (code === 4011)
|
|
269
|
-
this.emit("error", "Shard would be on over 2500 guilds. Add more shards.");
|
|
270
|
-
}
|
|
247
|
+
if (code === 4011)
|
|
248
|
+
this.client.emit("error", "Shard would be on over 2500 guilds. Add more shards.");
|
|
271
249
|
// Invalid shard.
|
|
272
|
-
if (code === 4010)
|
|
273
|
-
this.emit("error", "Invalid sharding data, check your client options.");
|
|
274
|
-
}
|
|
250
|
+
if (code === 4010)
|
|
251
|
+
this.client.emit("error", "Invalid sharding data, check your client options.");
|
|
275
252
|
// Session timed out.
|
|
276
253
|
// force identify if the session is marked as invalid.
|
|
277
254
|
if (code === 4009) {
|
|
278
|
-
this.emit("error", "Session timed out.");
|
|
255
|
+
this.client.emit("error", "Session timed out.");
|
|
279
256
|
this.clearHeartBeat();
|
|
280
257
|
this.connect();
|
|
281
258
|
}
|
|
282
259
|
// Rate limited.
|
|
283
260
|
if (code === 4008) {
|
|
284
|
-
this.emit("error", "You are being rate limited. Wait before sending more packets.");
|
|
261
|
+
this.client.emit("error", "You are being rate limited. Wait before sending more packets.");
|
|
285
262
|
this.clearHeartBeat();
|
|
286
263
|
this.connect();
|
|
287
264
|
}
|
|
288
265
|
// Invalid sequence.
|
|
289
266
|
if (code === 4007) {
|
|
290
|
-
this.emit("error", "Invalid sequence. Reconnecting and starting a new session.");
|
|
267
|
+
this.client.emit("error", "Invalid sequence. Reconnecting and starting a new session.");
|
|
291
268
|
this.reset();
|
|
292
269
|
this.connect();
|
|
293
270
|
}
|
|
294
271
|
// Already authenticated.
|
|
295
272
|
if (code === 4005) {
|
|
296
|
-
this.emit("error", "You sent more than one OP 2 IDENTIFY payload while the websocket was open.");
|
|
273
|
+
this.client.emit("error", "You sent more than one OP 2 IDENTIFY payload while the websocket was open.");
|
|
297
274
|
this.clearHeartBeat();
|
|
298
275
|
this.connect();
|
|
299
276
|
}
|
|
300
277
|
// Authentication failed.
|
|
301
|
-
if (code === 4004)
|
|
302
|
-
this.emit("error", "Tried to connect with an invalid token");
|
|
303
|
-
}
|
|
278
|
+
if (code === 4004)
|
|
279
|
+
this.client.emit("error", "Tried to connect with an invalid token");
|
|
304
280
|
// Not authenticated.
|
|
305
281
|
if (code === 4003) {
|
|
306
|
-
this.emit("error", "You tried to send a packet before sending an OP 2 IDENTIFY or OP 6 RESUME.");
|
|
282
|
+
this.client.emit("error", "You tried to send a packet before sending an OP 2 IDENTIFY or OP 6 RESUME.");
|
|
307
283
|
this.clearHeartBeat();
|
|
308
284
|
this.connect();
|
|
309
285
|
}
|
|
310
286
|
// Decode error.
|
|
311
287
|
if (code === 4002) {
|
|
312
|
-
this.emit("error", "You sent an invalid payload");
|
|
288
|
+
this.client.emit("error", "You sent an invalid payload");
|
|
313
289
|
this.clearHeartBeat();
|
|
314
290
|
this.connect();
|
|
315
291
|
}
|
|
316
292
|
// Invalid opcode.
|
|
317
293
|
if (code === 4001) {
|
|
318
|
-
this.emit("error", "You sent an invalid opcode or invalid payload for an opcode");
|
|
294
|
+
this.client.emit("error", "You sent an invalid opcode or invalid payload for an opcode");
|
|
319
295
|
this.clearHeartBeat();
|
|
320
296
|
this.connect();
|
|
321
297
|
}
|
|
322
298
|
// Generic error / safe self closing code.
|
|
323
299
|
if (code === 4000) {
|
|
324
|
-
if (reconnecting)
|
|
300
|
+
if (reconnecting)
|
|
325
301
|
gracefulClose = true;
|
|
326
|
-
}
|
|
327
302
|
else {
|
|
328
|
-
this.emit("error", "Error code 4000 received. Attempting to resume");
|
|
303
|
+
this.client.emit("error", "Error code 4000 received. Attempting to resume");
|
|
329
304
|
this.clearHeartBeat();
|
|
330
305
|
this.connect();
|
|
331
306
|
}
|
|
332
307
|
}
|
|
333
308
|
// Don't try to reconnect when true
|
|
334
|
-
if (code === 1000 && reason === "Disconnected by User")
|
|
309
|
+
if (code === 1000 && reason === "Disconnected by User")
|
|
335
310
|
gracefulClose = true;
|
|
336
|
-
}
|
|
337
311
|
if (gracefulClose) {
|
|
338
312
|
this.clearHeartBeat();
|
|
339
|
-
|
|
313
|
+
this.betterWs.removeAllListeners();
|
|
340
314
|
}
|
|
341
315
|
this.emit("disconnect", code, reason, gracefulClose);
|
|
342
316
|
}
|
|
@@ -345,27 +319,23 @@ class DiscordConnector extends events_1.EventEmitter {
|
|
|
345
319
|
* @param data Presence data to send.
|
|
346
320
|
*/
|
|
347
321
|
async presenceUpdate(data) {
|
|
348
|
-
|
|
349
|
-
return (_a = this.betterWs) === null || _a === void 0 ? void 0 : _a.sendMessage({ op: Constants_1.GATEWAY_OP_CODES.PRESENCE_UPDATE, d: this._checkPresenceData(data) });
|
|
322
|
+
return this.betterWs.sendMessage({ op: Constants_1.GATEWAY_OP_CODES.PRESENCE_UPDATE, d: this._checkPresenceData(data) });
|
|
350
323
|
}
|
|
351
324
|
/**
|
|
352
325
|
* Send an OP 4 VOICE_STATE_UPDATE to the gateway.
|
|
353
326
|
* @param data Voice state update data to send.
|
|
354
327
|
*/
|
|
355
328
|
async voiceStateUpdate(data) {
|
|
356
|
-
|
|
357
|
-
if (!data) {
|
|
329
|
+
if (!data)
|
|
358
330
|
return Promise.resolve();
|
|
359
|
-
}
|
|
360
|
-
return (_a = this.betterWs) === null || _a === void 0 ? void 0 : _a.sendMessage({ op: Constants_1.GATEWAY_OP_CODES.VOICE_STATE_UPDATE, d: this._checkVoiceStateUpdateData(data) });
|
|
331
|
+
return this.betterWs.sendMessage({ op: Constants_1.GATEWAY_OP_CODES.VOICE_STATE_UPDATE, d: this._checkVoiceStateUpdateData(data) });
|
|
361
332
|
}
|
|
362
333
|
/**
|
|
363
334
|
* Send an OP 8 REQUEST_GUILD_MEMBERS to the gateway.
|
|
364
335
|
* @param data Data to send.
|
|
365
336
|
*/
|
|
366
337
|
async requestGuildMembers(data) {
|
|
367
|
-
|
|
368
|
-
return (_a = this.betterWs) === null || _a === void 0 ? void 0 : _a.sendMessage({ op: Constants_1.GATEWAY_OP_CODES.REQUEST_GUILD_MEMBERS, d: this._checkRequestGuildMembersData(data) });
|
|
338
|
+
return this.betterWs.sendMessage({ op: Constants_1.GATEWAY_OP_CODES.REQUEST_GUILD_MEMBERS, d: this._checkRequestGuildMembersData(data) });
|
|
369
339
|
}
|
|
370
340
|
/**
|
|
371
341
|
* Checks presence data and fills in missing elements.
|