-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClientUser.ts
517 lines (477 loc) · 14.1 KB
/
ClientUser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// deno-lint-ignore-file no-explicit-any
import type { ActivityType, GatewayActivityButton } from "../../deps.ts";
import { CertifiedDevice } from "./CertifiedDevice.ts";
import { VoiceSettings } from "./VoiceSettings.ts";
import { Lobby, type LobbyType } from "./Lobby.ts";
import { Channel } from "./Channel.ts";
import { Guild } from "./Guild.ts";
import { User } from "./User.ts";
export enum ActivitySupportedPlatform {
IOS = "ios",
ANDROID = "android",
WEB = "web",
}
export enum ActivityPartyPrivacy {
PRIVATE = 0,
PUBLIC = 1,
}
export type SetActivity = {
/**
* Minimum of 2 characters and maximum of 128 characters
*/
state?: string;
/**
* Minimum of 2 characters and maximum of 128 characters
*/
details?: string;
startTimestamp?: number | Date;
endTimestamp?: number | Date;
/**
* Minimum of 1 characters and maximum of 128 characters
*/
largeImageKey?: string;
/**
* Minimum of 1 characters and maximum of 128 characters
*/
smallImageKey?: string;
/**
* Minimum of 2 characters and maximum of 128 characters
*/
largeImageText?: string;
/**
* Minimum of 2 characters and maximum of 128 characters
*/
smallImageText?: string;
/**
* Minimum of 2 characters and maximum of 128 characters
*/
partyId?: string;
/**
* Default: ActivityPartyPrivacy.PRIVATE
*/
partyPrivacy?: ActivityPartyPrivacy;
partySize?: number;
partyMax?: number;
/**
* Minimum of 2 characters and maximum of 128 characters
*/
matchSecret?: string;
/**
* Minimum of 2 characters and maximum of 128 characters
*/
joinSecret?: string;
/**
* Minimum of 2 characters and maximum of 128 characters
*/
spectateSecret?: string;
instance?: boolean;
buttons?: Array<GatewayActivityButton>;
supportedPlatforms?: (
| ActivitySupportedPlatform
| `${ActivitySupportedPlatform}`
)[];
/**
* Default: ActivityTypes.PLAYING
*/
type?:
| ActivityType.Playing
| ActivityType.Listening
| ActivityType.Watching;
};
export type SetActivityResponse = {
state?: string;
buttons?: string[];
name: string;
application_id: string;
type: number;
metadata: {
button_urls?: string[];
};
};
export class ClientUser extends User {
// #region Helper function
async fetchUser(userId: string): Promise<User> {
return new User(
this.client,
(await this.client.request("GET_USER", { id: userId })).data,
);
}
/**
* Used to get a guild the client is in.
*
* @param guildId - id of the guild to get
* @param timeout - asynchronously get guild with time to wait before timing out
* @returns partial guild
*/
async fetchGuild(guildId: string, timeout?: number): Promise<Guild> {
return new Guild(
this.client,
(
await this.client.request("GET_GUILD", {
guild_id: guildId,
timeout,
})
).data,
);
}
/**
* Used to get a list of guilds the client is in.
* @returns the guilds the user is in
*/
async fetchGuilds(): Promise<Guild[]> {
return (await this.client.request("GET_GUILDS")).data.guilds.map(
(guildData: any) => new Guild(this.client, guildData),
);
}
/**
* Used to get a channel the client is in.
* @param channelId - id of the channel to get
* @returns partial channel
*/
async fetchChannel(channelId: string): Promise<Channel> {
return new Channel(
this.client,
(
await this.client.request("GET_CHANNEL", {
channel_id: channelId,
})
).data,
);
}
/**
* Used to get a guild's channels the client is in.
* @param guildId - id of the guild to get channels for
* @returns guild channels the user is in
*/
async fetchChannels(guildId: string): Promise<Channel> {
return (
await this.client.request("GET_CHANNELS", { guild_id: guildId })
).data.channels.map(
(channelData: any) => new Channel(this.client, channelData),
);
}
/**
* Used to get the client's current voice channel. There are no arguments for this command. Returns the [Get Channel](https://discord.com/developers/docs/topics/rpc#getchannel) response, or `null` if none.
* @returns the client's current voice channel, `null` if none
*/
async getSelectedVoiceChannel(): Promise<Channel | null> {
const response = await this.client.request("GET_SELECTED_VOICE_CHANNEL");
return response.data !== null
? new Channel(this.client, response.data)
: null;
}
/**
* Used to join voice channels, group dms, or dms. Returns the [Get Channel](https://discord.com/developers/docs/topics/rpc#getchannel) response, `null` if none.
* @param channelId - channel id to join
* @param timeout - asynchronously join channel with time to wait before timing out
* @param force - forces a user to join a voice channel
* @returns the channel that the user joined, `null` if none
*/
async selectVoiceChannel(
channelId: string,
timeout?: number,
force?: boolean,
): Promise<Channel> {
return new Channel(
this.client,
(
await this.client.request("SELECT_VOICE_CHANNEL", {
channel_id: channelId,
timeout,
force,
})
).data,
);
}
/**
* Used to leave voice channels, group dms, or dms
* @param timeout - asynchronously join channel with time to wait before timing out
* @param force - forces a user to join a voice channel
*/
async leaveVoiceChannel(timeout?: number, force?: boolean): Promise<void> {
await this.client.request("SELECT_VOICE_CHANNEL", {
channel_id: null,
timeout,
force,
});
}
/**
* Used to get current client's voice settings
* @returns the voice setting
*/
async getVoiceSettings(): Promise<VoiceSettings> {
return new VoiceSettings(
this.client,
(await this.client.request("GET_VOICE_SETTINGS")).data,
);
}
/**
* Used to change voice settings of users in voice channels
* @param voiceSettings - the settings
* @returns the settings that have been set
*/
async setVoiceSettings(
voiceSettings: Partial<VoiceSettings>,
): Promise<VoiceSettings> {
return new VoiceSettings(
this.client,
(await this.client.request("SET_VOICE_SETTINGS", voiceSettings)).data,
);
}
/**
* Used by hardware manufacturers to send information about the current state of their certified devices that are connected to Discord.
* @param devices - a list of devices for your manufacturer, in order of priority
* @returns
*/
async setCeritfiedDevices(devices: CertifiedDevice[]): Promise<void> {
await this.client.request("SET_CERTIFIED_DEVICES", { devices });
}
/**
* Used to accept an Ask to Join request.
* @param userId - the id of the requesting user
*/
async sendJoinInvite(userId: string): Promise<void> {
await this.client.request("SEND_ACTIVITY_JOIN_INVITE", {
user_id: userId,
});
}
/**
* Used to reject an Ask to Join request.
* @param userId - the id of the requesting user
*/
async closeJoinRequest(userId: string): Promise<void> {
await this.client.request("CLOSE_ACTIVITY_JOIN_REQUEST", {
user_id: userId,
});
}
/**
* Used to join text channels, group dms, or dms. Returns the [Get Channel](https://discord.com/developers/docs/topics/rpc#getchannel) response, or `null` if none.
* @param channelId - channel id to join
* @param timeout - asynchronously join channel with time to wait before timing out
* @returns the text channel that user joined
*/
async selectTextChannel(
channelId: string,
timeout?: number,
): Promise<Channel | null> {
return new Channel(
this.client,
(
await this.client.request("SELECT_TEXT_CHANNEL", {
channel_id: channelId,
timeout,
})
).data,
);
}
/**
* Used to leave text channels, group dms, or dms.
* @param timeout - asynchronously join channel with time to wait before timing out
*/
async leaveTextChannel(timeout?: number): Promise<void> {
await this.client.request("SELECT_TEXT_CHANNEL", {
channel_id: null,
timeout,
});
}
async getRelationships(): Promise<Array<User>> {
return (
await this.client.request("GET_RELATIONSHIPS")
).data.relationships.map((data: any) => {
return new User(this.client, { ...data.user, presence: data.presence });
});
}
/**
* Used to update a user's Rich Presence.
*
* @param activity - the rich presence to assign to the user
* @param pid - the application's process id
* @returns The activity that have been set
*/
async setActivity(
activity: SetActivity,
pid?: number,
): Promise<SetActivityResponse> {
const formattedAcitivity: any = {
...activity,
assets: {},
timestamps: {},
party: {},
secrets: {},
};
if (activity.startTimestamp instanceof Date) {
formattedAcitivity.timestamps.start = Math.round(
activity.startTimestamp.getTime(),
);
} else if (typeof activity.startTimestamp === "number") {
formattedAcitivity.timestamps.start = activity.startTimestamp;
}
if (activity.endTimestamp instanceof Date) {
formattedAcitivity.timestamps.end = Math.round(
activity.endTimestamp.getTime(),
);
} else if (typeof activity.endTimestamp === "number") {
formattedAcitivity.timestamps.end = activity.endTimestamp;
}
if (activity.largeImageKey) {
formattedAcitivity.assets.large_image = activity.largeImageKey;
}
if (activity.smallImageKey) {
formattedAcitivity.assets.small_image = activity.smallImageKey;
}
if (activity.largeImageText) {
formattedAcitivity.assets.large_text = activity.largeImageText;
}
if (activity.smallImageText) {
formattedAcitivity.assets.small_text = activity.smallImageText;
}
if (activity.partyId) formattedAcitivity.party.id = activity.partyId;
if (activity.partyPrivacy) {
formattedAcitivity.party.privacy = activity.partyPrivacy;
}
if (activity.partySize && activity.partyMax) {
formattedAcitivity.party.size = [activity.partySize, activity.partyMax];
}
if (activity.joinSecret) {
formattedAcitivity.secrets.join = activity.joinSecret;
}
if (activity.spectateSecret) {
formattedAcitivity.secrets.spectate = activity.spectateSecret;
}
if (activity.matchSecret) {
formattedAcitivity.secrets.match = activity.matchSecret;
}
if (activity.supportedPlatforms) {
formattedAcitivity.supported_platforms = activity.supportedPlatforms;
}
if (Object.keys(formattedAcitivity.assets).length === 0) {
delete formattedAcitivity["assets"];
}
if (Object.keys(formattedAcitivity.timestamps).length === 0) {
delete formattedAcitivity["timestamps"];
}
if (Object.keys(formattedAcitivity.party).length === 0) {
delete formattedAcitivity["party"];
}
if (Object.keys(formattedAcitivity.secrets).length === 0) {
delete formattedAcitivity["secrets"];
}
formattedAcitivity.instance = !!activity.instance;
// Clean-up
delete formattedAcitivity["startTimestamp"];
delete formattedAcitivity["endTimestamp"];
delete formattedAcitivity["largeImageKey"];
delete formattedAcitivity["smallImageKey"];
delete formattedAcitivity["largeImageText"];
delete formattedAcitivity["smallImageText"];
delete formattedAcitivity["partyId"];
delete formattedAcitivity["partyPrivacy"];
delete formattedAcitivity["partySize"];
delete formattedAcitivity["partyMax"];
delete formattedAcitivity["joinSecret"];
delete formattedAcitivity["spectateSecret"];
delete formattedAcitivity["matchSecret"];
delete formattedAcitivity["supportedPlatforms"];
return (
await this.client.request("SET_ACTIVITY", {
pid: pid ?? process ? process.pid ?? 0 : 0,
activity: formattedAcitivity,
})
).data;
}
/**
* Used to clear a user's Rich Presence.
*
* @param pid - the application's process id
*/
async clearActivity(pid?: number): Promise<void> {
await this.client.request("SET_ACTIVITY", {
pid: pid ?? Deno.pid,
});
}
// #region Undocumented
// This region holds method that are not documented by Discord BUT does exist
/**
* Create a new lobby
* @param type - lobby type
* @param capacity - lobby size
* @param locked - is lobby locked
* @param metadata - additional data?
* @returns lobby that user created
*/
async createLobby(
type: LobbyType,
capacity?: number,
locked?: boolean,
metadata?: any,
): Promise<Lobby> {
return new Lobby(
this.client,
(
await this.client.request("CREATE_LOBBY", {
type,
capacity,
locked,
metadata,
})
).data,
);
}
/**
* Used to join a new lobby.
* @param lobbyId - the id of the lobby to join
* @param secret - the secret of the lobby to join
* @returns the lobby that the user joined
*/
async connectToLobby(lobbyId: string, secret: string): Promise<Lobby> {
return new Lobby(
this.client,
(
await this.client.request("CONNECT_TO_LOBBY", {
id: lobbyId,
secret,
})
).data,
);
}
/**
* Used to join a new lobby.
* @param lobbyId - the id of the lobby to join
* @param data - additional data to send to lobby
* @returns the lobby that the user joined
*/
async sendToLobby(lobbyId: string, data: string): Promise<Lobby> {
return new Lobby(
this.client,
(
await this.client.request("SEND_TO_LOBBY", {
lobby_id: lobbyId,
data,
})
).data,
);
}
/**
* Used to get a user's avatar
* @param userId - id of the user to get the avatar of
* @param format - image format
* @param size - image size
* @return base64 encoded image data
*/
async getImage(
userId: string,
format: "png" | "webp" | "jpg" = "png",
size: 16 | 32 | 64 | 128 | 256 | 512 | 1024 = 1024,
): Promise<string> {
return (
await this.client.request("GET_IMAGE", {
type: "user",
id: userId,
format,
size,
})
).data.data_url;
}
// #endregion
// #endregion
}