forked from OceanicJS/Oceanic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplications.ts
581 lines (549 loc) · 27.3 KB
/
Applications.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
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
/** @module REST/ApplicationCommands */
import * as Routes from "../util/Routes";
import type {
AnyApplicationCommand,
ApplicationCommandOptionConversion,
CreateApplicationCommandOptions,
CreateChatInputApplicationCommandOptions,
EditApplicationCommandOptions,
EditApplicationCommandPermissionsOptions,
EditChatInputApplicationCommandOptions,
RESTGuildApplicationCommandPermissions,
RawApplicationCommand,
RawGuildApplicationCommandPermissions,
CreateGuildApplicationCommandOptions,
EditGuildApplicationCommandOptions,
GetApplicationCommandOptions,
CreateTestEntitlementOptions,
RawEntitlement,
RawSKU,
RawTestEntitlement,
SearchEntitlementsOptions
} from "../types/applications";
import ApplicationCommand from "../structures/ApplicationCommand";
import type { RequestOptions } from "../types/request-handler";
import type RESTManager from "../rest/RESTManager";
import SKU from "../structures/SKU";
import Entitlement from "../structures/Entitlement";
import TestEntitlement from "../structures/TestEntitlement";
import ClientApplication from "../structures/ClientApplication";
import type {
ApplicationEmoji,
ApplicationEmojis,
CreateApplicationEmojiOptions,
EditApplicationEmojiOptions,
EditApplicationOptions,
RESTApplication,
RawApplicationEmoji,
RawApplicationEmojis,
RawClientApplication
} from "../types";
import Application from "../structures/Application";
/** Various methods for interacting with application commands. Located at {@link Client#rest | Client#rest}{@link RESTManager#applications | .applications}. */
export default class Applications {
private _manager: RESTManager;
constructor(manager: RESTManager) {
this._manager = manager;
}
/**
* Overwrite all existing global application commands.
* @param applicationID The ID of the application.
* @param options The commands.
* @caching This method **does not** cache its result.
*/
async bulkEditGlobalCommands(applicationID: string, options: Array<CreateApplicationCommandOptions>): Promise<Array<ApplicationCommand>> {
const opts = options as Array<CreateChatInputApplicationCommandOptions>;
return this._manager.authRequest<Array<RawApplicationCommand>>({
method: "PUT",
path: Routes.APPLICATION_COMMANDS(applicationID),
json: opts.map(opt => ({
contexts: opt.contexts,
description: opt.description,
default_member_permissions: opt.defaultMemberPermissions,
description_localizations: opt.descriptionLocalizations,
dm_permission: opt.dmPermission,
integration_types: opt.integrationTypes,
name: opt.name,
name_localizations: opt.nameLocalizations,
nsfw: opt.nsfw,
options: opt.options?.map(o => this._manager.client.util.optionToRaw(o)),
type: opt.type
}))
}).then(data => data.map(d => new ApplicationCommand(d, this._manager.client)));
}
/**
* Overwrite all existing application commands in a guild.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @param options The commands.
* @caching This method **does not** cache its result.
*/
async bulkEditGuildCommands(applicationID: string, guildID: string, options: Array<CreateGuildApplicationCommandOptions>): Promise<Array<ApplicationCommand>> {
const opts = options as Array<CreateChatInputApplicationCommandOptions>;
return this._manager.authRequest<Array<RawApplicationCommand>>({
method: "PUT",
path: Routes.GUILD_APPLICATION_COMMANDS(applicationID, guildID),
json: opts.map(opt => ({
id: opt.id,
default_member_permissions: opt.defaultMemberPermissions,
description: opt.description,
description_localizations: opt.descriptionLocalizations,
dm_permission: opt.dmPermission,
name: opt.name,
name_localizations: opt.nameLocalizations,
nsfw: opt.nsfw,
options: opt.options?.map(o => this._manager.client.util.optionToRaw(o)),
type: opt.type
}))
}).then(data => data.map(d => new ApplicationCommand(d, this._manager.client)));
}
/**
* Mark an entitlement as consumed.
* @param applicationID The ID of the application to the entitlement is for.
* @param entitlementID The ID of the entitlement to consume.
*/
async consumeEntitlement(applicationID: string, entitlementID: string): Promise<void> {
await this._manager.authRequest<void>({
method: "POST",
path: Routes.CONSUME_ENTITLEMENT(applicationID, entitlementID)
});
}
/**
* Create an emoji for an application.
* @param applicationID The ID of the application.
* @param options The options for creating the emoji.
* @caching This method **does not** cache its result.
*/
async createEmoji(applicationID: string, options: CreateApplicationEmojiOptions): Promise<ApplicationEmoji> {
if (options.image) {
options.image = this._manager.client.util._convertImage(options.image, "image");
}
return this._manager.authRequest<RawApplicationEmoji>({
method: "POST",
path: Routes.APPLICATION_EMOJIS(applicationID),
json: {
name: options.name,
image: options.image
}
}).then(emoji => this._manager.client.util.convertApplicationEmoji(emoji));
}
/**
* Create a global application command.
* @param applicationID The ID of the application.
* @param options The options for the command.
* @caching This method **does not** cache its result.
*/
async createGlobalCommand<T extends CreateApplicationCommandOptions = CreateApplicationCommandOptions>(applicationID: string, options: T): Promise<ApplicationCommandOptionConversion<T>> {
const opt = options as CreateChatInputApplicationCommandOptions;
return this._manager.authRequest<RawApplicationCommand>({
method: "POST",
path: Routes.APPLICATION_COMMANDS(applicationID),
json: {
contexts: opt.contexts,
default_member_permissions: opt.defaultMemberPermissions,
description: opt.description,
description_localizations: opt.descriptionLocalizations,
dm_permission: opt.dmPermission,
integration_types: opt.integrationTypes,
name: opt.name,
name_localizations: opt.nameLocalizations,
nsfw: opt.nsfw,
options: opt.options?.map(o => this._manager.client.util.optionToRaw(o)),
type: opt.type
}
}).then(data => new ApplicationCommand(data, this._manager.client) as never);
}
/**
* Create a guild application command.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @param options The options for the command.
* @caching This method **does not** cache its result.
*/
async createGuildCommand<T extends CreateGuildApplicationCommandOptions = CreateGuildApplicationCommandOptions>(applicationID: string, guildID: string, options: T): Promise<ApplicationCommandOptionConversion<T>> {
const opt = options as CreateChatInputApplicationCommandOptions;
return this._manager.authRequest<RawApplicationCommand>({
method: "POST",
path: Routes.GUILD_APPLICATION_COMMANDS(applicationID, guildID),
json: {
default_member_permissions: opt.defaultMemberPermissions,
description: opt.description,
description_localizations: opt.descriptionLocalizations,
dm_permission: opt.dmPermission,
name: opt.name,
name_localizations: opt.nameLocalizations,
nsfw: opt.nsfw,
options: opt.options?.map(o => this._manager.client.util.optionToRaw(o)),
type: opt.type
}
}).then(data => new ApplicationCommand(data, this._manager.client) as never);
}
/**
* Create a test entitlement.
* @param applicationID The ID of the application to create the entitlement for.
* @param options The options for creating the test entitlement.
*/
async createTestEntitlement(applicationID: string, options: CreateTestEntitlementOptions): Promise<TestEntitlement> {
return this._manager.authRequest<RawTestEntitlement>({
method: "POST",
path: Routes.ENTITLEMENTS(applicationID),
json: {
owner_id: options.ownerID,
owner_type: options.ownerType,
sku_id: options.skuID
}
}).then(data => new TestEntitlement(data, this._manager.client));
}
/**
* Delete an emoji for an application.
* @param applicationID The ID of the application.
* @param emojiID The ID of the emoji to be deleted.
* @caching This method **does not** cache its result.
*/
async deleteEmoji(applicationID: string, emojiID: string): Promise<void> {
await this._manager.authRequest<null>({
method: "DELETE",
path: Routes.APPLICATION_EMOJI(applicationID, emojiID)
});
}
/**
* Delete a global application command.
* @param applicationID The ID of the application.
* @param commandID The ID the command to delete.
* @caching This method **does not** cache its result.
*/
async deleteGlobalCommand(applicationID: string, commandID: string): Promise<void> {
await this._manager.authRequest<null>({
method: "DELETE",
path: Routes.APPLICATION_COMMAND(applicationID, commandID)
});
}
/**
* Delete a guild application command.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @param commandID The ID of the command to delete.
* @caching This method **does not** cache its result.
*/
async deleteGuildCommand(applicationID: string, guildID: string, commandID: string): Promise<void> {
await this._manager.authRequest<null>({
method: "DELETE",
path: Routes.GUILD_APPLICATION_COMMAND(applicationID, guildID, commandID)
});
}
/**
* Delete an entitlement.
* @param applicationID The ID of the application to delete the entitlement from.
* @param entitlementID The ID of the entitlement to delete.
*/
async deleteTestEntitlement(applicationID: string, entitlementID: string): Promise<void> {
await this._manager.authRequest<null>({
method: "DELETE",
path: Routes.ENTITLEMENT(applicationID, entitlementID)
});
}
/**
* Edit the currently authenticated bot's application info.
* @param options The options for editing the application.
* @caching This method **does not** cache its result.
*/
async editCurrent(options: EditApplicationOptions): Promise<Application> {
if (options.coverImage) {
options.coverImage = this._manager.client.util._convertImage(options.coverImage, "cover image");
}
if (options.icon) {
options.icon = this._manager.client.util._convertImage(options.icon, "cover image");
}
return this._manager.authRequest<RESTApplication>({
method: "PATCH",
path: Routes.APPLICATION,
json: {
cover_image: options.coverImage,
custom_install_url: options.customInstallURL,
description: options.description,
flags: options.flags,
icon: options.icon,
install_params: options.installParams,
integration_types_config: options.integrationTypesConfig,
interactions_endpoint_url: options.interactionsEndpointURL,
role_connections_verification_url: options.roleConnectionsVerificationURL,
tags: options.tags
}
}).then(data => new Application(data, this._manager.client));
}
/**
* Edit an existing emoji for an application.
* @param applicationID The ID of the application.
* @param emojiID The ID of the emoji to be edited.
* @param options The options for editing the emoji.
* @caching This method **does not** cache its result.
*/
async editEmoji(applicationID: string, emojiID: string, options: EditApplicationEmojiOptions): Promise<ApplicationEmoji> {
return this._manager.authRequest<RawApplicationEmoji>({
method: "PATCH",
path: Routes.APPLICATION_EMOJI(applicationID, emojiID),
json: { name: options.name }
}).then(emoji => this._manager.client.util.convertApplicationEmoji(emoji));
}
/**
* Edit a global application command.
* @param applicationID The ID of the application.
* @param commandID The ID of the command to edit.
* @param options The options for editing the command.
* @caching This method **does not** cache its result.
*/
async editGlobalCommand<T extends EditApplicationCommandOptions = EditApplicationCommandOptions>(applicationID: string, commandID: string, options: T): Promise<ApplicationCommandOptionConversion<T>> {
const opt = options as EditChatInputApplicationCommandOptions;
return this._manager.authRequest<RawApplicationCommand>({
method: "PATCH",
path: Routes.APPLICATION_COMMAND(applicationID, commandID),
json: {
contexts: opt.contexts,
default_member_permissions: opt.defaultMemberPermissions,
description: opt.description,
description_localizations: opt.descriptionLocalizations,
dm_permission: opt.dmPermission,
integration_types: opt.integrationTypes,
name: opt.name,
name_localizations: opt.nameLocalizations,
nsfw: opt.nsfw,
options: opt.options?.map(o => this._manager.client.util.optionToRaw(o))
}
}).then(data => new ApplicationCommand(data, this._manager.client) as never);
}
/**
* Edit a guild application command.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @param commandID The ID of the command to edit.
* @param options The options for editing the command.
* @caching This method **does not** cache its result.
*/
async editGuildCommand<T extends EditGuildApplicationCommandOptions = EditGuildApplicationCommandOptions>(applicationID: string, guildID: string, commandID: string, options: T): Promise<ApplicationCommandOptionConversion<T>> {
const opt = options as EditChatInputApplicationCommandOptions;
return this._manager.authRequest<RawApplicationCommand>({
method: "PATCH",
path: Routes.GUILD_APPLICATION_COMMAND(applicationID, guildID, commandID),
json: {
default_member_permissions: opt.defaultMemberPermissions,
description: opt.description,
description_localizations: opt.descriptionLocalizations,
dm_permission: opt.dmPermission,
name: opt.name,
name_localizations: opt.nameLocalizations,
nsfw: opt.nsfw,
options: opt.options?.map(o => this._manager.client.util.optionToRaw(o))
}
}).then(data => new ApplicationCommand(data, this._manager.client) as never);
}
/**
* Edit a guild application command's permissions. This requires a bearer token with the `applications.commands.permissions.update` scope.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @param commandID The ID of the command.
* @param options The options for editing the permissions.
* @caching This method **does not** cache its result.
*/
async editGuildCommandPermissions(applicationID: string, guildID: string, commandID: string, options: EditApplicationCommandPermissionsOptions): Promise<RESTGuildApplicationCommandPermissions> {
return (options.accessToken ? this._manager.request.bind(this._manager) : this._manager.authRequest.bind(this._manager))({
method: "PATCH",
path: Routes.GUILD_APPLICATION_COMMAND_PERMISSION(applicationID, guildID, commandID),
json: { permissions: options.permissions },
auth: options.accessToken
} as Omit<RequestOptions, "auth">).then(data => {
const d = data as RawGuildApplicationCommandPermissions;
return {
applicationID: d.application_id,
guildID: d.guild_id,
id: d.id,
permissions: d.permissions
};
});
}
/**
* Get the currently authenticated bot's application info as a bare {@link ClientApplication | ClientApplication}.
* @caching This method **does not** cache its result.
*/
async getClient(): Promise<ClientApplication> {
return this._manager.authRequest<RawClientApplication>({
method: "GET",
path: Routes.APPLICATION
}).then(data => new ClientApplication(data, this._manager.client));
}
/**
* Get the currently authenticated bot's application info.
* @caching This method **does not** cache its result.
*/
async getCurrent(): Promise<Application> {
return this._manager.authRequest<RESTApplication>({
method: "GET",
path: Routes.APPLICATION
}).then(data => new Application(data, this._manager.client));
}
/**
* Get an emoji for an application.
* @param applicationID The ID of the application to get the emojis of.
* @param emojiID The ID of the emoji to get.
* @caching This method **does not** cache its result.
*/
async getEmoji(applicationID: string, emojiID: string): Promise<ApplicationEmoji> {
return this._manager.authRequest<RawApplicationEmoji>({
method: "GET",
path: Routes.APPLICATION_EMOJI(applicationID, emojiID)
}).then(emoji => this._manager.client.util.convertApplicationEmoji(emoji));
}
/**
* Get the emojis for an application.
* @param applicationID The ID of the application to get the emojis of.
* @caching This method **does not** cache its result.
*/
async getEmojis(applicationID: string): Promise<ApplicationEmojis> {
return this._manager.authRequest<RawApplicationEmojis>({
method: "GET",
path: Routes.APPLICATION_EMOJIS(applicationID)
}).then(({ items }) => ({
items: items.map(item => this._manager.client.util.convertApplicationEmoji(item))
}));
}
/**
* Get the entitlements for an application.
* @param applicationID The ID of the application to get the entitlements of.
* @param options The options for getting the entitlements.
*/
async getEntitlements(applicationID: string, options: SearchEntitlementsOptions = {}): Promise<Array<Entitlement | TestEntitlement>> {
const query = new URLSearchParams();
if (options.after !== undefined) query.set("after", options.after);
if (options.before !== undefined) query.set("before", options.before);
if (options.excludeEnded !== undefined) query.set("exclude_ended", String(options.excludeEnded));
if (options.guildID !== undefined) query.set("guild_id", options.guildID);
if (options.limit !== undefined) query.set("limit", String(options.limit));
if (options.skuIDs !== undefined) query.set("sku_ids", options.skuIDs.join(","));
if (options.userID !== undefined) query.set("subscription_id", options.userID);
return this._manager.authRequest<Array<RawEntitlement | RawTestEntitlement>>({
method: "GET",
path: Routes.ENTITLEMENTS(applicationID),
query
}).then(data => data.map(d => "subscription_id" in d && d.subscription_id ? new Entitlement(d, this._manager.client) : new TestEntitlement(d, this._manager.client)));
}
/**
* Get a global application command.
* @param applicationID The ID of the application.
* @param commandID The ID of the command.
* @param options The options for getting the command.
* @caching This method **does not** cache its result.
*/
async getGlobalCommand<T extends AnyApplicationCommand = AnyApplicationCommand>(applicationID: string, commandID: string, options?: GetApplicationCommandOptions): Promise<T> {
const query = new URLSearchParams();
if (options?.withLocalizations !== undefined) {
query.set("with_localizations", options.withLocalizations.toString());
}
return this._manager.authRequest<RawApplicationCommand>({
method: "GET",
path: Routes.APPLICATION_COMMAND(applicationID, commandID),
query,
headers: options?.locale === undefined ? undefined : { "X-Discord-Locale": options.locale }
}).then(data => new ApplicationCommand(data, this._manager.client) as never);
}
/**
* Get an application's global commands.
* @param applicationID The ID of the application.
* @param options The options for getting the command.
* @caching This method **does not** cache its result.
*/
async getGlobalCommands(applicationID: string, options?: GetApplicationCommandOptions): Promise<Array<AnyApplicationCommand>> {
const query = new URLSearchParams();
if (options?.withLocalizations !== undefined) {
query.set("with_localizations", options.withLocalizations.toString());
}
return this._manager.authRequest<Array<RawApplicationCommand>>({
method: "GET",
path: Routes.APPLICATION_COMMANDS(applicationID),
query,
headers: options?.locale === undefined ? undefined : { "X-Discord-Locale": options.locale }
}).then(data => data.map(d => new ApplicationCommand(d, this._manager.client)) as never);
}
/**
* Get a global application command.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @param commandID The ID of the command.
* @param options The options for getting the command.
* @caching This method **does not** cache its result.
*/
async getGuildCommand<T extends AnyApplicationCommand = AnyApplicationCommand>(applicationID: string, guildID: string, commandID: string, options?: GetApplicationCommandOptions): Promise<T> {
const query = new URLSearchParams();
if (options?.withLocalizations !== undefined) {
query.set("with_localizations", options.withLocalizations.toString());
}
return this._manager.authRequest<RawApplicationCommand>({
method: "GET",
path: Routes.GUILD_APPLICATION_COMMAND(applicationID, commandID, guildID),
query,
headers: options?.locale === undefined ? undefined : { "X-Discord-Locale": options.locale }
}).then(data => new ApplicationCommand(data, this._manager.client) as never);
}
/**
* Get an application's application commands in a specific guild.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @param options The options for getting the command.
* @caching This method **does not** cache its result.
*/
async getGuildCommands(applicationID: string, guildID: string, options?: GetApplicationCommandOptions): Promise<Array<AnyApplicationCommand>> {
const query = new URLSearchParams();
if (options?.withLocalizations !== undefined) {
query.set("with_localizations", options.withLocalizations.toString());
}
return this._manager.authRequest<Array<RawApplicationCommand>>({
method: "GET",
path: Routes.GUILD_APPLICATION_COMMANDS(applicationID, guildID),
query,
headers: options?.locale === undefined ? undefined : { "X-Discord-Locale": options.locale }
}).then(data => data.map(d => new ApplicationCommand(d, this._manager.client)) as never);
}
/**
* Get an application command's permissions in a guild.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @param commandID The ID of the command.
* @caching This method **does not** cache its result.
*/
async getGuildPermission(applicationID: string, guildID: string, commandID: string): Promise<RESTGuildApplicationCommandPermissions> {
return this._manager.authRequest<RawGuildApplicationCommandPermissions>({
method: "GET",
path: Routes.GUILD_APPLICATION_COMMAND_PERMISSION(applicationID, guildID, commandID)
}).then(data => ({
applicationID: data.application_id,
guildID: data.guild_id,
id: data.id,
permissions: data.permissions
}));
}
/**
* Get the permissions for all application commands in a guild.
* @param applicationID The ID of the application.
* @param guildID The ID of the guild.
* @caching This method **does not** cache its result.
*/
async getGuildPermissions(applicationID: string, guildID: string): Promise<Array<RESTGuildApplicationCommandPermissions>> {
return this._manager.authRequest<Array<RawGuildApplicationCommandPermissions>>({
method: "GET",
path: Routes.GUILD_APPLICATION_COMMAND_PERMISSIONS(applicationID, guildID)
}).then(data => data.map(d => ({
applicationID: d.application_id,
guildID: d.guild_id,
id: d.id,
permissions: d.permissions
})));
}
/**
* Get the SKUs for an application.
* @param applicationID The ID of the application to get the SKUs of.
*/
async getSKUs(applicationID: string): Promise<Array<SKU>> {
return this._manager.authRequest<Array<RawSKU>>({
method: "GET",
path: Routes.SKUS(applicationID)
}).then(data => data.map(d => new SKU(d, this._manager.client)));
}
}