forked from Khaomi/discord-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientUser.ts
More file actions
664 lines (561 loc) · 22.3 KB
/
ClientUser.ts
File metadata and controls
664 lines (561 loc) · 22.3 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import { ActivityType, GatewayActivityButton } from "discord-api-types/v10";
import type { CertifiedDevice } from "./CertifiedDevice";
import { VoiceSettings } from "./VoiceSettings";
import { Channel } from "./Channel";
import { Guild } from "./Guild";
import { User } from "./User";
export enum ActivitySupportedPlatform {
IOS = "ios",
ANDROID = "android",
WEB = "web"
}
export enum ActivityPartyPrivacy {
PRIVATE = 0,
PUBLIC = 1
}
export type SetActivity = {
name?: string;
type?: ActivityType;
url?: string;
state?: string;
details?: string;
startTimestamp?: number | Date;
endTimestamp?: number | Date;
largeImageKey?: string;
smallImageKey?: string;
largeImageText?: string;
smallImageText?: string;
partyId?: string;
partySize?: number;
partyMax?: number;
matchSecret?: string;
joinSecret?: string;
spectateSecret?: string;
instance?: boolean;
buttons?: GatewayActivityButton[];
supportedPlatforms?: (ActivitySupportedPlatform | `${ActivitySupportedPlatform}`)[];
applicationId?: string;
flags?: number;
emoji?: {
name: string;
id?: string;
animated?: boolean;
};
};
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 | null,
timeout: number,
force: boolean,
navigate: boolean
): Promise<Channel> {
return new Channel(
this.client,
(
await this.client.request("SELECT_VOICE_CHANNEL", {
channel_id: channelId,
timeout,
force,
navigate
})
).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 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 | null, 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 formattedActivity: any = {
name: activity.name,
type: activity.type ?? ActivityType.Playing,
created_at: Date.now(),
instance: !!activity.instance,
};
// URL only for Streaming activity
if (activity.type === ActivityType.Streaming && activity.url) {
formattedActivity.url = activity.url;
}
// Details & state
if (activity.details) formattedActivity.details = activity.details;
if (activity.state) formattedActivity.state = activity.state;
// Timestamps (only if any defined)
if (activity.startTimestamp || activity.endTimestamp) {
formattedActivity.timestamps = {};
if (activity.startTimestamp instanceof Date) {
formattedActivity.timestamps.start = activity.startTimestamp.getTime();
} else if (typeof activity.startTimestamp === 'number') {
formattedActivity.timestamps.start = activity.startTimestamp;
}
if (activity.endTimestamp instanceof Date) {
formattedActivity.timestamps.end = activity.endTimestamp.getTime();
} else if (typeof activity.endTimestamp === 'number') {
formattedActivity.timestamps.end = activity.endTimestamp;
}
}
// Assets (only if any defined)
if (
activity.largeImageKey ||
activity.smallImageKey ||
activity.largeImageText ||
activity.smallImageText
) {
formattedActivity.assets = {};
if (activity.largeImageKey) formattedActivity.assets.large_image = activity.largeImageKey;
if (activity.smallImageKey) formattedActivity.assets.small_image = activity.smallImageKey;
if (activity.largeImageText) formattedActivity.assets.large_text = activity.largeImageText;
if (activity.smallImageText) formattedActivity.assets.small_text = activity.smallImageText;
}
// Party (only if any defined)
if (activity.partyId || activity.partySize || activity.partyMax) {
formattedActivity.party = {};
if (activity.partyId) formattedActivity.party.id = activity.partyId;
if (activity.partySize !== undefined && activity.partyMax !== undefined) {
formattedActivity.party.size = [activity.partySize, activity.partyMax];
}
}
// Secrets (only if any defined)
if (activity.joinSecret || activity.spectateSecret || activity.matchSecret) {
formattedActivity.secrets = {};
if (activity.joinSecret) formattedActivity.secrets.join = activity.joinSecret;
if (activity.spectateSecret) formattedActivity.secrets.spectate = activity.spectateSecret;
if (activity.matchSecret) formattedActivity.secrets.match = activity.matchSecret;
}
// Buttons
if (activity.buttons?.length) {
formattedActivity.buttons = activity.buttons;
}
// Supported platforms
if (activity.supportedPlatforms?.length) {
formattedActivity.supported_platforms = activity.supportedPlatforms;
}
return (
await this.client.request("SET_ACTIVITY", {
pid: (pid ?? process?.pid ?? 0),
activity: formattedActivity
})
).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 ?? process) ? (process.pid ?? 0) : 0 });
}
// #region Undocumented
// This region holds method that are not documented by Discord BUT does exist
// Also most of this might not even be correct, use at your own risk
/**
* 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;
}
/**
* Requires RPC and RPC_VOICE_WRITE
* @returns
*/
async getSoundboardSounds(): Promise<any> {
return (await this.client.request("GET_SOUNDBOARD_SOUNDS")).data;
}
/**
* Requires RPC and RPC_VOICE_WRITE
* @returns
*/
async playSoundboardSound(guildId: string, soundId: string): Promise<any> {
return (
await this.client.request("PLAY_SOUNDBOARD_SOUND", {
guild_id: guildId,
sound_id: soundId
})
).data;
}
/**
* Requires RPC and RPC_VIDEO_WRITE
* @returns
*/
async toggleVideo(): Promise<any> {
return (await this.client.request("TOGGLE_VIDEO")).data;
}
/**
* Requires RPC and RPC_SCREENSHARE_WRITE
* @returns
*/
async toggleScreenshare(pid?: number): Promise<any> {
return (await this.client.request("TOGGLE_SCREENSHARE", { pid })).data;
}
/**
* Requires RPC and RPC_VOICE_WRITE
* @returns
*/
async setPushToTalk(active: boolean): Promise<any> {
return (await this.client.request("PUSH_TO_TALK", { active })).data;
}
/**
* Requires RPC and RPC_VOICE_WRITE
* @returns
*/
async setVoiceSettings(req: {
user_id: string;
pan: {
left: number;
right: number;
};
// 0 - 200
volume: number;
mute: boolean;
}): Promise<any> {
return (await this.client.request("SET_VOICE_SETTINGS", req)).data;
}
/**
* Requires RPC and RPC_VOICE_WRITE
* @returns
*/
async setVoiceSettings2(req: {
input_mode: { type: "PUSH_TO_TALK" | "VOICE_ACTIVITY"; shortcut: string };
self_mute: boolean;
self_deaf: boolean;
}): Promise<any> {
return (await this.client.request("SET_VOICE_SETTINGS_2", req)).data;
}
/**
* Requires RPC and RPC_GUILDS_MEMBERS_READ
* @returns
*/
async getChannelPermissions(): Promise<{ permissions: any }> {
return (await this.client.request("GET_CHANNEL_PERMISSIONS")).data;
}
async getActivityInstanceConnectedParticipants(): Promise<{ participants: { nickname: string }[] }> {
return (await this.client.request("GET_ACTIVITY_INSTANCE_CONNECTED_PARTICIPANTS")).data;
}
async navigateToConnections(): Promise<any> {
return (await this.client.request("NAVIGATE_TO_CONNECTIONS")).data;
}
async createChanenlInvite(channelId: string, args: object): Promise<any> {
return (await this.client.request("CREATE_CHANNEL_INVITE", { channel_id: channelId, ...args })).data;
}
async openExternalLink(url: string): Promise<any> {
return (await this.client.request("OPEN_EXTERNAL_LINK", { url })).data;
}
async getPlatformBehaviors(): Promise<{ iosKeyboardResizesView: boolean }> {
return (await this.client.request("GET_PLATFORM_BEHAVIORS")).data;
}
async getProviderAccessToken(provider: string, connectionRedirect: string): Promise<any> {
return (await this.client.request("GET_PROVIDER_ACCESS_TOKEN", { provider, connectionRedirect })).data;
}
async maybeGetProviderAccessToken(provider: string): Promise<any> {
return (await this.client.request("MAYBE_GET_PROVIDER_ACCESS_TOKEN", { provider })).data;
}
async getSKUS(): Promise<any> {
return (await this.client.request("GET_SKUS")).data;
}
async getEntitlements(): Promise<any> {
return (await this.client.request("GET_ENTITLEMENTS")).data;
}
async getSKUsEmbedded(): Promise<{ skus: any }> {
return (await this.client.request("GET_SKUS_EMBEDDED")).data;
}
async getEntitlementsEmbedded(): Promise<{ entitlements: any }> {
return (await this.client.request("GET_ENTITLEMENTS_EMBEDDED")).data;
}
async encourageHardwareAcceleration(): Promise<any> {
return (await this.client.request("ENCOURAGE_HW_ACCELERATION")).data;
}
async captureLog(level: "log" | "warn" | "debug" | "info" | "error", message: string): Promise<any> {
return (await this.client.request("CAPTURE_LOG", { level, message })).data;
}
async sendAnalyticsEvent(eventName: string, eventProperties: object): Promise<any> {
return (await this.client.request("SEND_ANALYTICS_EVENT", { eventName, eventProperties })).data;
}
async getLocale(): Promise<string> {
return (await this.client.request("USER_SETTINGS_GET_LOCALE")).data.locale;
}
async getAchievements(): Promise<any> {
return (await this.client.request("GET_USER_ACHIEVEMENTS")).data;
}
async setAchievement(achievementId: string, percentComplete: number): Promise<any> {
return (
await this.client.request("SET_USER_ACHIEVEMENT", {
achievement_id: achievementId,
percent_complete: percentComplete
})
).data;
}
async createNetworkingToken(): Promise<any> {
return (await this.client.request("NETWORKING_CREATE_TOKEN")).data;
}
async networkingPeerMetrics(): Promise<any> {
return (await this.client.request("NETWORKING_PEER_METRICS")).data;
}
async networkingSystemMetrics(): Promise<any> {
return (await this.client.request("NETWORKING_SYSTEM_METRICS")).data;
}
async getNetworkingConfig(): Promise<{ address: any; token: any }> {
return (await this.client.request("GET_NETWORKING_CONFIG")).data;
}
async startPurchase(skuId: string, pid: number): Promise<any> {
return (await this.client.request("START_PURCHASE", { sku_id: skuId, pid })).data;
}
async startPremiumPurchase(pid: number): Promise<any> {
return (await this.client.request("START_PREMIUM_PURCHASE", { pid })).data;
}
async getApplicationTicket(): Promise<any> {
return (await this.client.request("GET_APPLICATION_TICKET")).data;
}
async getEntitlementTicket(): Promise<any> {
return (await this.client.request("GET_ENTITLEMENT_TICKET")).data;
}
async validateApplication(): Promise<any> {
return (await this.client.request("VALIDATE_APPLICATION")).data;
}
async openOverlayVoiceSettings(pid: number): Promise<any> {
return (await this.client.request("OPEN_OVERLAY_VOICE_SETTINGS", { pid })).data;
}
async openOverlayGuildInvite(code: string, pid: number): Promise<any> {
return (await this.client.request("OPEN_OVERLAY_GUILD_INVITE", { code, pid })).data;
}
async openOverlayActivityInvite(type: "JOIN", pid: number): Promise<any> {
const typeToNumber = {
JOIN: 0
};
return (await this.client.request("OPEN_OVERLAY_ACTIVITY_INVITE", { type: typeToNumber[type], pid })).data;
}
async setOverlayLocked(locked: boolean, pid: number): Promise<any> {
return (await this.client.request("SET_OVERLAY_LOCKED", { locked, pid })).data;
}
async browserHandoff(): Promise<any> {
return (await this.client.request("BROWSER_HANDOFF")).data;
}
async openGuildTemplateBrowser(code: any): Promise<any> {
return (await this.client.request("GUILD_TEMPLATE_BROWSER", { code })).data;
}
async openGiftCodeBrowser(code: any): Promise<any> {
return (await this.client.request("GIFT_CODE_BROWSER", { code })).data;
}
async brainTreePopupBridgeCallback(state: any, path: any, query: any): Promise<any> {
return (await this.client.request("BRAINTREE_POPUP_BRIDGE_CALLBACK", { state, path, query })).data;
}
async billingPopupBridgeCallback(state: any, path: any, query: any, paymentSourceType: any): Promise<any> {
return (
await this.client.request("BILLING_POPUP_BRIDGE_CALLBACK", {
state,
path,
query,
payment_source_type: paymentSourceType
})
).data;
}
async connectionsCallback(providerType: any, code: any, openIdParams: any, state: any): Promise<any> {
return (
await this.client.request("CONNECTIONS_CALLBACK", {
providerType: providerType,
code,
open_id_params: openIdParams,
state
})
).data;
}
async deepLink(type: any, params: any): Promise<any> {
return (await this.client.request("DEEP_LINK", { type, params })).data;
}
async inviteBrowser(code: any): Promise<any> {
return (await this.client.request("INVITE_BROWSER", { code })).data;
}
async initiateImageUpload(): Promise<{ image_url: string }> {
return (await this.client.request("INITIATE_IMAGE_UPLOAD")).data;
}
async openShareMomentDialog(mediaUrl: string): Promise<any> {
return (await this.client.request("OPEN_SHARE_MOMENT_DIALOG", { mediaUrl })).data;
}
async openInviteDialog(): Promise<any> {
return (await this.client.request("OPEN_INVITE_DIALOG")).data;
}
async acceptActivityInvite(
type: "JOIN",
userId: string,
sessionId: string,
channelId: string,
messageId: string
): Promise<any> {
const typeToNumber = {
JOIN: 0
};
return (
await this.client.request("ACCEPT_ACTIVITY_INVITE", {
type: typeToNumber[type],
user_id: userId,
session_id: sessionId,
channel_id: channelId,
message_id: messageId
})
).data;
}
async activityInviteUser(userId: string, type: "JOIN", content: string, pid: number): Promise<any> {
const typeToNumber = {
JOIN: 0
};
return (
await this.client.request("ACTIVITY_INVITE_USER", {
user_id: userId,
type: typeToNumber[type],
content,
pid
})
).data;
}
async closeActivityJoinRequest(userId: string): Promise<any> {
return (await this.client.request("CLOSE_ACTIVITY_JOIN_REQUEST", { user_id: userId })).data;
}
async sendActivityJoinInvite(userId: string, pid: number): Promise<any> {
return (await this.client.request("SEND_ACTIVITY_JOIN_INVITE", { user_id: userId, pid })).data;
}
async setConfig(useInteractivePip: boolean): Promise<any> {
return (await this.client.request("SET_CONFIG", { use_interactive_pip: useInteractivePip })).data;
}
// #endregion
// #endregion
}