Skip to content

Commit ddf646a

Browse files
committed
Add GatewayIntents support
1 parent 5e06e30 commit ddf646a

File tree

3 files changed

+123
-88
lines changed

3 files changed

+123
-88
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "detritus-client-socket",
3-
"version": "0.4.5",
3+
"version": "0.4.6",
44
"description": "A TypeScript NodeJS library to interact with Discord's Gateway",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/constants.ts

Lines changed: 87 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const Package = Object.freeze({
22
URL: 'https://github.com/detritusjs/client-socket',
3-
VERSION: '0.4.5',
3+
VERSION: '0.4.6',
44
});
55

66
function normalize(object: {[key: string]: any}) {
@@ -16,48 +16,48 @@ export const ApiVersions = Object.freeze({
1616
MEDIA_GATEWAY: 4,
1717
});
1818

19-
export const CompressTypes = Object.freeze({
20-
NONE: 'none',
21-
PAYLOAD: 'payload',
22-
ZLIB: 'zlib-stream',
23-
ZSTD: 'zstd-stream',
24-
});
19+
export enum CompressTypes {
20+
NONE = 'none',
21+
PAYLOAD = 'payload',
22+
ZLIB = 'zlib-stream',
23+
ZSTD = 'zstd-stream',
24+
}
2525

2626
export const COMPRESS_TYPES = Object.freeze(Object.values(CompressTypes));
2727

2828
export const DEFAULT_SHARD_COUNT = 1;
2929
export const DEFAULT_SHARD_LAUNCH_DELAY = 5000;
3030
export const DEFAULT_VOICE_TIMEOUT = 30000;
3131

32-
export const EncodingTypes = Object.freeze({
33-
ETF: 'etf',
34-
JSON: 'json',
35-
});
32+
export enum EncodingTypes {
33+
ETF = 'etf',
34+
JSON = 'json',
35+
}
3636

37-
export const GatewayActivityActionTypes = Object.freeze({
38-
JOIN: 1,
39-
SPECTATE: 2,
40-
LISTEN: 3,
41-
WATCH: 4,
42-
JOIN_REQUEST: 5,
43-
});
37+
export enum GatewayActivityActionTypes {
38+
JOIN = 1,
39+
SPECTATE = 2,
40+
LISTEN = 3,
41+
WATCH = 4,
42+
JOIN_REQUEST = 5,
43+
}
4444

45-
export const GatewayActivityFlags = Object.freeze({
46-
INSTANCE: 1 << 0,
47-
JOIN: 1 << 1,
48-
SPECTATE: 1 << 2,
49-
JOIN_REQUEST: 1 << 3,
50-
SYNC: 1 << 4,
51-
PLAY: 1 << 5,
52-
});
45+
export enum GatewayActivityFlags {
46+
INSTANCE = 1 << 0,
47+
JOIN = 1 << 1,
48+
SPECTATE = 1 << 2,
49+
JOIN_REQUEST = 1 << 3,
50+
SYNC = 1 << 4,
51+
PLAY = 1 << 5,
52+
}
5353

54-
export const GatewayActivityTypes = Object.freeze({
55-
PLAYING: 0,
56-
STREAMING: 1,
57-
LISTENING: 2,
58-
WATCHING: 3,
59-
CUSTOM_STATUS: 4,
60-
});
54+
export enum GatewayActivityTypes {
55+
PLAYING = 0,
56+
STREAMING = 1,
57+
LISTENING = 2,
58+
WATCHING = 3,
59+
CUSTOM_STATUS = 4,
60+
}
6161

6262
export const GatewayDispatchEvents = normalize({
6363
READY: null,
@@ -97,6 +97,8 @@ export const GatewayDispatchEvents = normalize({
9797
GUILD_ROLE_CREATE: null,
9898
GUILD_ROLE_DELETE: null,
9999
GUILD_ROLE_UPDATE: null,
100+
INVITE_CREATE: null,
101+
INVITE_DELETE: null,
100102
LIBRARY_APPLICATION_UPDATE: null,
101103
LOBBY_CREATE: null,
102104
LOBBY_DELETE: null,
@@ -114,6 +116,7 @@ export const GatewayDispatchEvents = normalize({
114116
MESSAGE_REACTION_ADD: null,
115117
MESSAGE_REACTION_REMOVE: null,
116118
MESSAGE_REACTION_REMOVE_ALL: null,
119+
MESSAGE_REACTION_REMOVE_EMOJI: null,
117120
MESSAGE_UPDATE: null,
118121
OAUTH2_TOKEN_REMOVE: null,
119122
PRESENCES_REPLACE: null,
@@ -143,40 +146,57 @@ export const GatewayDispatchEvents = normalize({
143146
WEBHOOKS_UPDATE: null,
144147
});
145148

146-
export const GatewayOpCodes = Object.freeze({
147-
DISPATCH: 0,
148-
HEARTBEAT: 1,
149-
IDENTIFY: 2,
150-
PRESENCE_UPDATE: 3,
151-
VOICE_STATE_UPDATE: 4,
152-
VOICE_SERVER_PING: 5,
153-
RESUME: 6,
154-
RECONNECT: 7,
155-
REQUEST_GUILD_MEMBERS: 8,
156-
INVALID_SESSION: 9,
157-
HELLO: 10,
158-
HEARTBEAT_ACK: 11,
159-
SYNC_GUILD: 12,
160-
CALL_CONNECT: 13,
161-
GUILD_SUBSCRIPTIONS: 14,
162-
LOBBY_CONNECT: 15,
163-
LOBBY_DISCONNECT: 16,
164-
LOBBY_VOICE_STATES_UPDATE: 17,
165-
STREAM_CREATE: 18,
166-
STREAM_DELETE: 19,
167-
STREAM_WATCH: 20,
168-
STREAM_PING: 21,
169-
STREAM_SET_PAUSED: 22,
170-
FLUSH_LFG_SUBSCRIPTIONS: 23,
171-
});
149+
export enum GatewayIntents {
150+
GUILDS = 1 << 0,
151+
GUILD_MEMBERS = 1 << 1,
152+
GUILD_BANS = 1 << 2,
153+
GUILD_EMOJIS = 1 << 3,
154+
GUILD_INTEGRATIONS = 1 << 4,
155+
GUILD_WEBHOOKS = 1 << 5,
156+
GUILD_INVITES = 1 << 6,
157+
GUILD_VOICE_STATES = 1 << 7,
158+
GUILD_PRESENCES = 1 << 8,
159+
GUILD_MESSAGES = 1 << 9,
160+
GUILD_MESSAGE_REACTIONS = 1 << 10,
161+
GUILD_MESSAGE_TYPING = 1 << 11,
162+
DIRECT_MESSAGES = 1 << 12,
163+
DIRECT_MESSAGE_REACTIONS = 1 << 13,
164+
DIRECT_MESSAGE_TYPING = 1 << 14,
165+
}
172166

173-
export const GatewayPresenceStatuses = Object.freeze({
174-
ONLINE: 'online',
175-
DND: 'dnd',
176-
IDLE: 'idle',
177-
INVISIBLE: 'invisible',
178-
OFFLINE: 'offline',
179-
});
167+
export enum GatewayOpCodes {
168+
DISPATCH = 0,
169+
HEARTBEAT = 1,
170+
IDENTIFY = 2,
171+
PRESENCE_UPDATE = 3,
172+
VOICE_STATE_UPDATE = 4,
173+
VOICE_SERVER_PING = 5,
174+
RESUME = 6,
175+
RECONNECT = 7,
176+
REQUEST_GUILD_MEMBERS = 8,
177+
INVALID_SESSION = 9,
178+
HELLO = 10,
179+
HEARTBEAT_ACK = 11,
180+
SYNC_GUILD = 12,
181+
CALL_CONNECT = 13,
182+
GUILD_SUBSCRIPTIONS = 14,
183+
LOBBY_CONNECT = 15,
184+
LOBBY_DISCONNECT = 16,
185+
LOBBY_VOICE_STATES_UPDATE = 17,
186+
STREAM_CREATE = 18,
187+
STREAM_DELETE = 19,
188+
STREAM_WATCH = 20,
189+
STREAM_PING = 21,
190+
STREAM_SET_PAUSED = 22,
191+
}
192+
193+
export enum GatewayPresenceStatuses {
194+
ONLINE = 'online',
195+
DND = 'dnd',
196+
IDLE = 'idle',
197+
INVISIBLE = 'invisible',
198+
OFFLINE = 'offline',
199+
}
180200

181201
export const MaxNumbers = Object.freeze({
182202
UINT8: 0xFF,
@@ -330,6 +350,7 @@ export const SocketGatewayCloseCodes = Object.freeze({
330350
SESSION_TIMEOUT: 4009,
331351
INVALID_SHARD: 4010,
332352
SHARDING_REQUIRED: 4011,
353+
INVALID_INTENTS: 4012,
333354
});
334355

335356
export const SocketMediaCloseCodes = Object.freeze({

src/gateway.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
CompressTypes,
1515
EncodingTypes,
1616
GatewayDispatchEvents,
17+
GatewayIntents,
1718
GatewayOpCodes,
1819
GatewayPresenceStatuses,
1920
Package,
@@ -75,6 +76,7 @@ export interface SocketOptions {
7576
encoding?: string,
7677
guildSubscriptions?: boolean,
7778
identifyProperties?: IdentifyDataProperties,
79+
intents?: Array<number> | Array<string> | string | number,
7880
largeThreshold?: number,
7981
presence?: PresenceOptions,
8082
reconnectDelay?: number,
@@ -101,13 +103,14 @@ export class Socket extends EventSpewer {
101103
};
102104
autoReconnect: boolean;
103105
bucket: Bucket;
104-
compress: string;
106+
compress: CompressTypes;
105107
disabledEvents: Array<string>;
106108
discordTrace: Array<any> = [];
107109
decompressor: Decompressor | null;
108-
encoding: string;
110+
encoding: EncodingTypes;
109111
guildSubscriptions: boolean;
110112
identifyProperties: IdentifyDataProperties = Object.assign({}, IdentifyProperties);
113+
intents?: number;
111114
killed: boolean = false;
112115
largeThreshold: number;
113116
mediaGateways = new BaseCollection<string, MediaSocket>();
@@ -141,10 +144,10 @@ export class Socket extends EventSpewer {
141144
options.compress = CompressTypes.NONE;
142145
}
143146
}
144-
147+
145148
this.autoReconnect = !!options.autoReconnect;
146-
this.compress = (<string> options.compress).toLowerCase();
147-
this.encoding = (<string> options.encoding).toLowerCase();
149+
this.compress = <CompressTypes> (<string> options.compress).toLowerCase();
150+
this.encoding = <EncodingTypes> (<string> options.encoding).toLowerCase();
148151
this.disabledEvents = <Array<string>> options.disabledEvents;
149152
this.guildSubscriptions = !!options.guildSubscriptions;
150153
this.largeThreshold = <number> options.largeThreshold;
@@ -198,6 +201,24 @@ export class Socket extends EventSpewer {
198201
};
199202
}
200203

204+
if (options.intents !== undefined) {
205+
this.intents = 0;
206+
207+
const intents = (Array.isArray(options.intents)) ? options.intents : [options.intents];
208+
for (let intent of intents) {
209+
if (typeof(intent) === 'string') {
210+
intent = intent.toUpperCase();
211+
if (intent in GatewayIntents) {
212+
this.intents |= (<any> GatewayIntents)[intent];
213+
}
214+
} else if (typeof(intent) === 'number') {
215+
this.intents |= intent;
216+
} else {
217+
throw new Error(`Invalid intent received: ${intent}`);
218+
}
219+
}
220+
}
221+
201222
Object.defineProperties(this, {
202223
_heartbeat: {enumerable: false, writable: false},
203224
identifyProperties: {enumerable: false},
@@ -259,6 +280,7 @@ export class Socket extends EventSpewer {
259280
metadata: activity.metadata,
260281
name: activity.name,
261282
party: undefined,
283+
platform: activity.platform,
262284
secrets: undefined,
263285
session_id: activity.sessionId,
264286
state: activity.state,
@@ -313,6 +335,7 @@ export class Socket extends EventSpewer {
313335
/* payload compression, rather use transport compression, using the get params overrides this */
314336
compress: (this.compress === CompressTypes.PAYLOAD),
315337
guild_subscriptions: this.guildSubscriptions,
338+
intents: this.intents,
316339
large_threshold: this.largeThreshold,
317340
properties: this.identifyProperties,
318341
token: this.token,
@@ -440,13 +463,8 @@ export class Socket extends EventSpewer {
440463
): void {
441464
const packet = this.decode(data, uncompressed);
442465
if (!packet) {return;}
443-
if (packet.s) {
444-
const oldSequence = this.sequence;
445-
const newSequence = packet.s;
446-
if (oldSequence + 1 < newSequence && !this.resuming) {
447-
return this.resume();
448-
}
449-
this.sequence = newSequence;
466+
if (packet.s !== null) {
467+
this.sequence = packet.s;
450468
}
451469

452470
switch (packet.op) {
@@ -726,15 +744,6 @@ export class Socket extends EventSpewer {
726744
}, callback);
727745
}
728746

729-
flushLfgSubscriptions(
730-
subscriptions: any,
731-
callback?: Function,
732-
): void {
733-
this.send(GatewayOpCodes.FLUSH_LFG_SUBSCRIPTIONS, {
734-
subscriptions,
735-
}, callback);
736-
}
737-
738747
guildStreamCreate(
739748
guildId: string,
740749
channelId: string,
@@ -876,6 +885,7 @@ export class Socket extends EventSpewer {
876885
guildId: null | string = null,
877886
channelId: null | string = null,
878887
options: {
888+
preferredRegion?: string,
879889
selfDeaf?: boolean,
880890
selfMute?: boolean,
881891
selfVideo?: boolean,
@@ -885,6 +895,7 @@ export class Socket extends EventSpewer {
885895
this.send(GatewayOpCodes.VOICE_STATE_UPDATE, {
886896
channel_id: channelId,
887897
guild_id: guildId,
898+
preferred_region: options.preferredRegion,
888899
self_deaf: options.selfDeaf,
889900
self_mute: options.selfMute,
890901
self_video: options.selfVideo,
@@ -990,6 +1001,7 @@ export interface GatewayPacket {
9901001
export interface IdentifyData {
9911002
compress?: boolean,
9921003
guild_subscriptions?: boolean,
1004+
intents?: number,
9931005
large_threshold?: number,
9941006
presence?: RawPresence,
9951007
properties: IdentifyDataProperties,
@@ -1038,6 +1050,7 @@ export interface RawPresenceActivity {
10381050
id?: string,
10391051
size?: Array<[number, number]>,
10401052
},
1053+
platform?: string,
10411054
secrets?: {
10421055
join?: string,
10431056
match?: string,
@@ -1089,6 +1102,7 @@ export interface PresenceActivityOptions {
10891102
id?: string,
10901103
size?: Array<[number, number]>,
10911104
},
1105+
platform?: string,
10921106
secrets?: {
10931107
join?: string,
10941108
match?: string,

0 commit comments

Comments
 (0)