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
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClientUser = void 0;
const VoiceSettings_1 = require("./VoiceSettings");
const Lobby_1 = require("./Lobby");
const Channel_1 = require("./Channel");
const Guild_1 = require("./Guild");
const User_1 = require("./User");
class ClientUser extends User_1.User {
// #region Helper function
async fetchUser(userId) {
return new User_1.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, timeout) {
return new Guild_1.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() {
return (await this.client.request("GET_GUILDS")).data.guilds.map((guildData) => new Guild_1.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) {
return new Channel_1.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) {
return (await this.client.request("GET_CHANNELS", { guild_id: guildId })).data.channels.map((channelData) => new Channel_1.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() {
const response = await this.client.request("GET_SELECTED_VOICE_CHANNEL");
return response.data !== null ? new Channel_1.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, timeout, force) {
return new Channel_1.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, force) {
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() {
return new VoiceSettings_1.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) {
return new VoiceSettings_1.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) {
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) {
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) {
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, timeout) {
return new Channel_1.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) {
await this.client.request("SELECT_TEXT_CHANNEL", { channel_id: null, timeout });
}
async getRelationships() {
return (await this.client.request("GET_RELATIONSHIPS")).data.relationships.map((data) => {
return new User_1.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, pid) {
const formattedAcitivity = {
...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.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 (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["partySize"];
delete formattedAcitivity["partyMax"];
delete formattedAcitivity["joinSecret"];
delete formattedAcitivity["spectateSecret"];
delete formattedAcitivity["matchSecret"];
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) {
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
/**
* 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, capacity, locked, metadata) {
return new Lobby_1.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, secret) {
return new Lobby_1.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, data) {
return new Lobby_1.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, format = "png", size = 1024) {
return (await this.client.request("GET_IMAGE", { type: "user", id: userId, format, size })).data.data_url;
}
}
exports.ClientUser = ClientUser;
|