@discordeno/rest 19.0.0-next.40c19da → 19.0.0-next.4384457

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/manager.js CHANGED
@@ -1,16 +1,19 @@
1
1
  /* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable no-const-assign */ import { InteractionResponseTypes } from '@discordeno/types';
2
- import { calculateBits, camelize, camelToSnakeCase, delay, encode, findFiles, getBotIdFromToken, isGetMessagesAfter, isGetMessagesAround, isGetMessagesBefore, isGetMessagesLimit, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
2
+ import { calculateBits, camelize, camelToSnakeCase, delay, getBotIdFromToken, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
3
3
  import fetch from 'node-fetch';
4
4
  import { createInvalidRequestBucket } from './invalidBucket.js';
5
5
  import { Queue } from './queue.js';
6
+ import { createRoutes } from './routes.js';
6
7
  // TODO: make dynamic based on package.json file
7
8
  const version = '19.0.0-alpha.1';
8
9
  export function createRestManager(options) {
9
- // Falsy token string check
10
- if (!options.token) throw new Error('You must provide a valid token.');
10
+ const applicationId = options.applicationId ? BigInt(options.applicationId) : options.token ? getBotIdFromToken(options.token) : undefined;
11
+ if (!applicationId) {
12
+ throw new Error('`applicationId` was not provided and was not able to extract the id from the bots token. Please explicitly pass `applicationId` to the rest manager.');
13
+ }
11
14
  const rest = {
12
15
  token: options.token,
13
- applicationId: options.applicationId ? BigInt(options.applicationId) : getBotIdFromToken(options.token),
16
+ applicationId,
14
17
  version: options.version ?? 10,
15
18
  baseUrl: options.proxy?.baseUrl ?? 'https://discord.com/api',
16
19
  maxRetryCount: Infinity,
@@ -21,488 +24,7 @@ export function createRestManager(options) {
21
24
  rateLimitedPaths: new Map(),
22
25
  invalidBucket: createInvalidRequestBucket({}),
23
26
  authorization: options.proxy?.authorization,
24
- routes: {
25
- webhooks: {
26
- id: (webhookId)=>{
27
- return `/webhooks/${webhookId}`;
28
- },
29
- message: (webhookId, token, messageId, options)=>{
30
- let url = `/webhooks/${webhookId}/${token}/messages/${messageId}?`;
31
- if (options) {
32
- if (options.threadId) url += `thread_id=${options.threadId}`;
33
- }
34
- return url;
35
- },
36
- original: (webhookId, token, options)=>{
37
- let url = `/webhooks/${webhookId}/${token}/messages/@original?`;
38
- if (options) {
39
- if (options.threadId) url += `thread_id=${options.threadId}`;
40
- }
41
- return url;
42
- },
43
- webhook: (webhookId, token, options)=>{
44
- let url = `/webhooks/${webhookId}/${token}?`;
45
- if (options) {
46
- if (options?.wait !== undefined) url += `wait=${options.wait.toString()}`;
47
- if (options.threadId) url += `thread_id=${options.threadId}`;
48
- }
49
- return url;
50
- }
51
- },
52
- // Miscellaneous Endpoints
53
- sessionInfo: ()=>rest.routes.gatewayBot(),
54
- // Channel Endpoints
55
- channels: {
56
- bulk: (channelId)=>{
57
- return `/channels/${channelId}/messages/bulk-delete`;
58
- },
59
- dm: ()=>{
60
- return '/users/@me/channels';
61
- },
62
- pin: (channelId, messageId)=>{
63
- return `/channels/${channelId}/pins/${messageId}`;
64
- },
65
- pins: (channelId)=>{
66
- return `/channels/${channelId}/pins`;
67
- },
68
- reactions: {
69
- bot: (channelId, messageId, emoji)=>{
70
- return `/channels/${channelId}/messages/${messageId}/reactions/${encodeURIComponent(emoji)}/@me`;
71
- },
72
- user: (channelId, messageId, emoji, userId)=>{
73
- return `/channels/${channelId}/messages/${messageId}/reactions/${encodeURIComponent(emoji)}/${userId}`;
74
- },
75
- all: (channelId, messageId)=>{
76
- return `/channels/${channelId}/messages/${messageId}/reactions`;
77
- },
78
- emoji: (channelId, messageId, emoji, options)=>{
79
- let url = `/channels/${channelId}/messages/${messageId}/reactions/${encodeURIComponent(emoji)}?`;
80
- if (options) {
81
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
82
- if (options.after) url += `after=${options.after}`;
83
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
84
- if (options.limit) url += `&limit=${options.limit}`;
85
- }
86
- return url;
87
- },
88
- message: (channelId, messageId, emoji, options)=>{
89
- let url = `/channels/${channelId}/messages/${messageId}/reactions/${encodeURIComponent(emoji)}?`;
90
- if (options) {
91
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
92
- if (options.after) url += `after=${options.after}`;
93
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
94
- if (options.limit) url += `&limit=${options.limit}`;
95
- }
96
- return url;
97
- }
98
- },
99
- webhooks: (channelId)=>{
100
- return `/channels/${channelId}/webhooks`;
101
- },
102
- channel: (channelId)=>{
103
- return `/channels/${channelId}`;
104
- },
105
- follow: (channelId)=>{
106
- return `/channels/${channelId}/followers`;
107
- },
108
- forum: (channelId)=>{
109
- return `/channels/${channelId}/threads?has_message=true`;
110
- },
111
- invites: (channelId)=>{
112
- return `/channels/${channelId}/invites`;
113
- },
114
- message: (channelId, messageId)=>{
115
- return `/channels/${channelId}/messages/${messageId}`;
116
- },
117
- messages: (channelId, options)=>{
118
- let url = `/channels/${channelId}/messages?`;
119
- if (options) {
120
- if (isGetMessagesAfter(options) && options.after) {
121
- url += `after=${options.after}`;
122
- }
123
- if (isGetMessagesBefore(options) && options.before) {
124
- url += `&before=${options.before}`;
125
- }
126
- if (isGetMessagesAround(options) && options.around) {
127
- url += `&around=${options.around}`;
128
- }
129
- if (isGetMessagesLimit(options) && options.limit) {
130
- url += `&limit=${options.limit}`;
131
- }
132
- }
133
- return url;
134
- },
135
- overwrite: (channelId, overwriteId)=>{
136
- return `/channels/${channelId}/permissions/${overwriteId}`;
137
- },
138
- crosspost: (channelId, messageId)=>{
139
- return `/channels/${channelId}/messages/${messageId}/crosspost`;
140
- },
141
- stages: ()=>{
142
- return '/stage-instances';
143
- },
144
- stage: (channelId)=>{
145
- return `/stage-instances/${channelId}`;
146
- },
147
- // Thread Endpoints
148
- threads: {
149
- message: (channelId, messageId)=>{
150
- return `/channels/${channelId}/messages/${messageId}/threads`;
151
- },
152
- all: (channelId)=>{
153
- return `/channels/${channelId}/threads`;
154
- },
155
- active: (guildId)=>{
156
- return `/guilds/${guildId}/threads/active`;
157
- },
158
- members: (channelId)=>{
159
- return `/channels/${channelId}/thread-members`;
160
- },
161
- me: (channelId)=>{
162
- return `/channels/${channelId}/thread-members/@me`;
163
- },
164
- user: (channelId, userId)=>{
165
- return `/channels/${channelId}/thread-members/${userId}`;
166
- },
167
- archived: (channelId)=>{
168
- return `/channels/${channelId}/threads/archived`;
169
- },
170
- public: (channelId, options)=>{
171
- let url = `/channels/${channelId}/threads/archived/public?`;
172
- if (options) {
173
- if (options.before) {
174
- url += `before=${new Date(options.before).toISOString()}`;
175
- }
176
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
177
- if (options.limit) url += `&limit=${options.limit}`;
178
- }
179
- return url;
180
- },
181
- private: (channelId, options)=>{
182
- let url = `/channels/${channelId}/threads/archived/private?`;
183
- if (options) {
184
- if (options.before) {
185
- url += `before=${new Date(options.before).toISOString()}`;
186
- }
187
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
188
- if (options.limit) url += `&limit=${options.limit}`;
189
- }
190
- return url;
191
- },
192
- joined: (channelId, options)=>{
193
- let url = `/channels/${channelId}/users/@me/threads/archived/private?`;
194
- if (options) {
195
- if (options.before) {
196
- url += `before=${new Date(options.before).toISOString()}`;
197
- }
198
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
199
- if (options.limit) url += `&limit=${options.limit}`;
200
- }
201
- return url;
202
- }
203
- },
204
- typing: (channelId)=>{
205
- return `/channels/${channelId}/typing`;
206
- }
207
- },
208
- // Guild Endpoints
209
- guilds: {
210
- all: ()=>{
211
- return '/guilds';
212
- },
213
- auditlogs: (guildId, options)=>{
214
- let url = `/guilds/${guildId}/audit-logs?`;
215
- if (options) {
216
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
217
- if (options.actionType) url += `action_type=${options.actionType}`;
218
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
219
- if (options.before) url += `&before=${options.before}`;
220
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
221
- if (options.limit) url += `&limit=${options.limit}`;
222
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
223
- if (options.userId) url += `&user_id=${options.userId}`;
224
- }
225
- return url;
226
- },
227
- automod: {
228
- rule: (guildId, ruleId)=>{
229
- return `/guilds/${guildId}/auto-moderation/rules/${ruleId}`;
230
- },
231
- rules: (guildId)=>{
232
- return `/guilds/${guildId}/auto-moderation/rules`;
233
- }
234
- },
235
- channels: (guildId)=>{
236
- return `/guilds/${guildId}/channels`;
237
- },
238
- emoji: (guildId, emojiId)=>{
239
- return `/guilds/${guildId}/emojis/${emojiId}`;
240
- },
241
- emojis: (guildId)=>{
242
- return `/guilds/${guildId}/emojis`;
243
- },
244
- events: {
245
- events: (guildId, withUserCount)=>{
246
- let url = `/guilds/${guildId}/scheduled-events?`;
247
- if (withUserCount !== undefined) {
248
- url += `with_user_count=${withUserCount.toString()}`;
249
- }
250
- return url;
251
- },
252
- event: (guildId, eventId, withUserCount)=>{
253
- let url = `/guilds/${guildId}/scheduled-events/${eventId}`;
254
- if (withUserCount !== undefined) {
255
- url += `with_user_count=${withUserCount.toString()}`;
256
- }
257
- return url;
258
- },
259
- users: (guildId, eventId, options)=>{
260
- let url = `/guilds/${guildId}/scheduled-events/${eventId}/users?`;
261
- if (options) {
262
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
263
- if (options.limit !== undefined) url += `limit=${options.limit}`;
264
- if (options.withMember !== undefined) {
265
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
266
- url += `&with_member=${options.withMember.toString()}`;
267
- }
268
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
269
- if (options.after !== undefined) url += `&after=${options.after}`;
270
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
271
- if (options.before !== undefined) url += `&before=${options.before}`;
272
- }
273
- return url;
274
- }
275
- },
276
- guild (guildId, withCounts) {
277
- let url = `/guilds/${guildId}?`;
278
- if (withCounts !== undefined) {
279
- url += `with_counts=${withCounts.toString()}`;
280
- }
281
- return url;
282
- },
283
- integration (guildId, integrationId) {
284
- return `/guilds/${guildId}/integrations/${integrationId}`;
285
- },
286
- integrations: (guildId)=>{
287
- return `/guilds/${guildId}/integrations?include_applications=true`;
288
- },
289
- invite (inviteCode, options) {
290
- let url = `/invites/${inviteCode}?`;
291
- if (options) {
292
- if (options.withCounts !== undefined) {
293
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
294
- url += `with_counts=${options.withCounts.toString()}`;
295
- }
296
- if (options.withExpiration !== undefined) {
297
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
298
- url += `&with_expiration=${options.withExpiration.toString()}`;
299
- }
300
- if (options.scheduledEventId) {
301
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
302
- url += `&guild_scheduled_event_id=${options.scheduledEventId}`;
303
- }
304
- }
305
- return url;
306
- },
307
- invites: (guildId)=>{
308
- return `/guilds/${guildId}/invites`;
309
- },
310
- leave: (guildId)=>{
311
- return `/users/@me/guilds/${guildId}`;
312
- },
313
- members: {
314
- ban: (guildId, userId)=>{
315
- return `/guilds/${guildId}/bans/${userId}`;
316
- },
317
- bans: (guildId, options)=>{
318
- let url = `/guilds/${guildId}/bans?`;
319
- if (options) {
320
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
321
- if (options.limit) url += `limit=${options.limit}`;
322
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
323
- if (options.after) url += `&after=${options.after}`;
324
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
325
- if (options.before) url += `&before=${options.before}`;
326
- }
327
- return url;
328
- },
329
- bot: (guildId)=>{
330
- return `/guilds/${guildId}/members/@me`;
331
- },
332
- member: (guildId, userId)=>{
333
- return `/guilds/${guildId}/members/${userId}`;
334
- },
335
- members: (guildId, options)=>{
336
- let url = `/guilds/${guildId}/members?`;
337
- if (options !== undefined) {
338
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
339
- if (options.limit) url += `limit=${options.limit}`;
340
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
341
- if (options.after) url += `&after=${options.after}`;
342
- }
343
- return url;
344
- },
345
- search: (guildId, query, options)=>{
346
- let url = `/guilds/${guildId}/members/search?query=${encodeURIComponent(query)}`;
347
- if (options) {
348
- if (options.limit !== undefined) url += `&limit=${options.limit}`;
349
- }
350
- return url;
351
- },
352
- prune: (guildId, options)=>{
353
- let url = `/guilds/${guildId}/prune?`;
354
- if (options) {
355
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
356
- if (options.days) url += `days=${options.days}`;
357
- if (Array.isArray(options.includeRoles)) {
358
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
359
- url += `&include_roles=${options.includeRoles.join(',')}`;
360
- } else if (options.includeRoles) {
361
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
362
- url += `&include_roles=${options.includeRoles}`;
363
- }
364
- }
365
- return url;
366
- }
367
- },
368
- mfa: (guildId)=>`/guilds/${guildId}/mfa`,
369
- preview: (guildId)=>{
370
- return `/guilds/${guildId}/preview`;
371
- },
372
- prune: (guildId, options)=>{
373
- let url = `/guilds/${guildId}/prune?`;
374
- if (options) {
375
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
376
- if (options.days) url += `days=${options.days}`;
377
- if (Array.isArray(options.includeRoles)) {
378
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
379
- url += `&include_roles=${options.includeRoles.join(',')}`;
380
- } else if (options.includeRoles) {
381
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
382
- url += `&include_roles=${options.includeRoles}`;
383
- }
384
- }
385
- return url;
386
- },
387
- roles: {
388
- one: (guildId, roleId)=>{
389
- return `/guilds/${guildId}/roles/${roleId}`;
390
- },
391
- all: (guildId)=>{
392
- return `/guilds/${guildId}/roles`;
393
- },
394
- member: (guildId, memberId, roleId)=>{
395
- return `/guilds/${guildId}/members/${memberId}/roles/${roleId}`;
396
- }
397
- },
398
- stickers: (guildId)=>{
399
- return `/guilds/${guildId}/stickers`;
400
- },
401
- sticker: (guildId, stickerId)=>{
402
- return `/guilds/${guildId}/stickers/${stickerId}`;
403
- },
404
- voice: (guildId, userId)=>{
405
- return `/guilds/${guildId}/voice-states/${userId ?? '@me'}`;
406
- },
407
- templates: {
408
- code: (code)=>{
409
- return `/guilds/templates/${code}`;
410
- },
411
- guild: (guildId, code)=>{
412
- return `/guilds/${guildId}/templates/${code}`;
413
- },
414
- all: (guildId)=>{
415
- return `/guilds/${guildId}/templates`;
416
- }
417
- },
418
- vanity: (guildId)=>{
419
- return `/guilds/${guildId}/vanity-url`;
420
- },
421
- regions: (guildId)=>{
422
- return `/guilds/${guildId}/regions`;
423
- },
424
- webhooks: (guildId)=>{
425
- return `/guilds/${guildId}/webhooks`;
426
- },
427
- welcome: (guildId)=>{
428
- return `/guilds/${guildId}/welcome-screen`;
429
- },
430
- widget: (guildId)=>{
431
- return `/guilds/${guildId}/widget`;
432
- },
433
- widgetJson: (guildId)=>{
434
- return `/guilds/${guildId}/widget.json`;
435
- }
436
- },
437
- sticker: (stickerId)=>{
438
- return `/stickers/${stickerId}`;
439
- },
440
- regions: ()=>{
441
- return '/voice/regions';
442
- },
443
- // Interaction Endpoints
444
- interactions: {
445
- commands: {
446
- // Application Endpoints
447
- commands: (applicationId)=>{
448
- return `/applications/${applicationId}/commands`;
449
- },
450
- guilds: {
451
- all (applicationId, guildId) {
452
- return `/applications/${applicationId}/guilds/${guildId}/commands`;
453
- },
454
- one (applicationId, guildId, commandId, withLocalizations) {
455
- let url = `/applications/${applicationId}/guilds/${guildId}/commands/${commandId}?`;
456
- if (withLocalizations !== undefined) {
457
- url += `with_localizations=${withLocalizations.toString()}`;
458
- }
459
- return url;
460
- }
461
- },
462
- permissions: (applicationId, guildId)=>{
463
- return `/applications/${applicationId}/guilds/${guildId}/commands/permissions`;
464
- },
465
- permission: (applicationId, guildId, commandId)=>{
466
- return `/applications/${applicationId}/guilds/${guildId}/commands/${commandId}/permissions`;
467
- },
468
- command: (applicationId, commandId, withLocalizations)=>{
469
- let url = `/applications/${applicationId}/commands/${commandId}?`;
470
- if (withLocalizations !== undefined) {
471
- url += `withLocalizations=${withLocalizations.toString()}`;
472
- }
473
- return url;
474
- }
475
- },
476
- responses: {
477
- // Interaction Endpoints
478
- callback: (interactionId, token)=>{
479
- return `/interactions/${interactionId}/${token}/callback`;
480
- },
481
- original: (interactionId, token)=>{
482
- return `/webhooks/${interactionId}/${token}/messages/@original`;
483
- },
484
- message: (applicationId, token, messageId)=>{
485
- return `/webhooks/${applicationId}/${token}/messages/${messageId}`;
486
- }
487
- }
488
- },
489
- // User endpoints
490
- user (userId) {
491
- return `/users/${userId}`;
492
- },
493
- userBot () {
494
- return '/users/@me';
495
- },
496
- oauth2Application () {
497
- return 'oauth2/applications/@me';
498
- },
499
- gatewayBot () {
500
- return '/gateway/bot';
501
- },
502
- nitroStickerPacks () {
503
- return '/sticker-packs';
504
- }
505
- },
27
+ routes: createRoutes(),
506
28
  checkRateLimits (url) {
507
29
  const ratelimited = rest.rateLimitedPaths.get(url);
508
30
  const global = rest.rateLimitedPaths.get('global');
@@ -543,56 +65,47 @@ export function createRestManager(options) {
543
65
  if (typeof obj === 'bigint') return obj.toString();
544
66
  return obj;
545
67
  },
546
- createRequest (options) {
68
+ createRequestBody (method, options) {
547
69
  const headers = {
548
70
  'user-agent': `DiscordBot (https://github.com/discordeno/discordeno, v${version})`
549
71
  };
550
- if (!options.unauthorized) headers.authorization = `Bot ${rest.token}`;
551
- // SOMETIMES SPECIAL HEADERS (E.G. CUSTOM AUTHORIZATION) NEED TO BE USED
552
- if (options.headers) {
553
- for(const key in options.headers){
554
- headers[key.toLowerCase()] = options.headers[key];
555
- }
556
- }
557
- // GET METHODS SHOULD NOT HAVE A BODY
558
- if (options.method === 'GET') {
559
- options.body = undefined;
560
- }
72
+ if (options?.unauthorized !== false) headers.authorization = `Bot ${rest.token}`;
561
73
  // IF A REASON IS PROVIDED ENCODE IT IN HEADERS
562
- if (options.body?.reason) {
563
- headers['X-Audit-Log-Reason'] = encodeURIComponent(options.body.reason);
564
- options.body.reason = undefined;
74
+ if (options?.reason !== undefined) {
75
+ headers['x-audit-log-reason'] = encodeURIComponent(options?.reason);
565
76
  }
566
- if (options.body) {
567
- const { file } = options.body;
568
- if (file) {
569
- const files = findFiles(file);
570
- const form = new FormData();
571
- // WHEN CREATING A STICKER, DISCORD WANTS FORM DATA ONLY
572
- if (options.url?.endsWith('/stickers') && options.method === 'POST') {
573
- form.append('file', files[0].blob, files[0].name);
574
- form.append('name', options.body.name);
575
- form.append('description', options.body.description);
576
- form.append('tags', options.body.tags);
577
- } else {
578
- for(let i = 0; i < files.length; i++){
579
- form.append(`file${i}`, files[i].blob, files[i].name);
580
- }
581
- if (file) options.body.file = undefined;
582
- form.append('payload_json', JSON.stringify(rest.changeToDiscordFormat(options.body)));
583
- }
584
- options.body.file = form;
585
- } else if (options.body && ![
586
- 'GET',
587
- 'DELETE'
588
- ].includes(options.method)) {
589
- headers['Content-Type'] = 'application/json';
77
+ let body;
78
+ // TODO: check if we need to add specific check for GET method
79
+ // Since GET does not allow bodies
80
+ // Have to check for attachments first, since body then has to be send in a different way.
81
+ if (options?.files !== undefined) {
82
+ const form = new FormData();
83
+ for(let i = 0; i < options.files.length; ++i){
84
+ form.append(`file${i}`, options.files[i].blob, options.files[i].name);
85
+ }
86
+ form.append('payload_json', JSON.stringify({
87
+ ...options.body,
88
+ files: undefined
89
+ }));
90
+ body = form;
91
+ // No need to set the `content-type` header since `fetch` does that automatically for us when we use a `FormData` object.
92
+ } else if (options?.body !== undefined) {
93
+ if (options.body instanceof FormData) {
94
+ body = options.body;
95
+ // No need to set the `content-type` header since `fetch` does that automatically for us when we use a `FormData` object.
96
+ } else {
97
+ body = JSON.stringify(rest.changeToDiscordFormat(options.body));
98
+ headers['content-type'] = `application/json`;
590
99
  }
591
100
  }
101
+ // SOMETIMES SPECIAL HEADERS (E.G. CUSTOM AUTHORIZATION) NEED TO BE USED
102
+ if (options?.headers) {
103
+ Object.assign(headers, options.headers);
104
+ }
592
105
  return {
106
+ body,
593
107
  headers,
594
- body: options.body?.file ?? JSON.stringify(rest.changeToDiscordFormat(options.body)),
595
- method: options.method
108
+ method
596
109
  };
597
110
  },
598
111
  processRateLimitedPaths () {
@@ -687,12 +200,7 @@ export function createRestManager(options) {
687
200
  },
688
201
  async sendRequest (options) {
689
202
  const url = options.url.startsWith('https://') ? options.url : `${rest.baseUrl}/v${rest.version}${options.url}`;
690
- const payload = rest.createRequest({
691
- method: options.method,
692
- url: options.url,
693
- body: options.body,
694
- ...options.options
695
- });
203
+ const payload = rest.createRequestBody(options.method, options.requestBodyOptions);
696
204
  logger.debug(`sending request to ${url}`, 'with payload:', {
697
205
  ...payload,
698
206
  headers: {
@@ -745,26 +253,28 @@ export function createRestManager(options) {
745
253
  body: JSON.stringify(json)
746
254
  });
747
255
  },
748
- // Credits: github.com/abalabahaha/eris lib/rest/RequestHandler.js#L397
749
- // Modified for our use-case
750
256
  simplifyUrl (url, method) {
751
- let route = url.replace(/\/([a-z-]+)\/(?:[0-9]{17,19})/g, function(match, p) {
752
- return [
753
- 'channels',
754
- 'guilds'
755
- ].includes(p) ? match : `/${p}/x`;
756
- }).replace(/\/reactions\/[^/]+/g, '/reactions/x');
757
- // GENERAL /reactions and /reactions/emoji/@me share the buckets
758
- if (route.includes('/reactions')) {
759
- route = route.substring(0, route.indexOf('/reactions') + '/reactions'.length);
257
+ const parts = url.split('/');
258
+ const secondLastPart = parts[parts.length - 2];
259
+ if (secondLastPart === 'channels' || secondLastPart === 'guilds') {
260
+ return url;
760
261
  }
761
- // Delete Message endpoint has its own rate limit
762
- if (method === 'DELETE' && route.endsWith('/messages/x')) {
763
- route = method + route;
262
+ if (secondLastPart === 'reactions' || parts[parts.length - 1] === '@me') {
263
+ parts.splice(-2);
264
+ parts.push('reactions');
265
+ } else {
266
+ parts.splice(-1);
267
+ parts.push('x');
764
268
  }
765
- return route;
269
+ if (parts[parts.length - 3] === 'reactions') {
270
+ parts.splice(-2);
271
+ }
272
+ if (method === 'DELETE' && secondLastPart === 'messages') {
273
+ return `D${parts.join('/')}`;
274
+ }
275
+ return parts.join('/');
766
276
  },
767
- processRequest (request) {
277
+ async processRequest (request) {
768
278
  const route = request.url.substring(request.url.indexOf('api/'));
769
279
  const parts = route.split('/');
770
280
  // Remove the api/
@@ -774,6 +284,10 @@ export function createRestManager(options) {
774
284
  // Set the full url to discord api in case it was recieved in a proxy rest
775
285
  request.url = `${rest.baseUrl}/v${rest.version}/${parts.join('/')}`;
776
286
  const url = rest.simplifyUrl(request.url, request.method);
287
+ if (request.runThroughQueue === false) {
288
+ await rest.sendRequest(request);
289
+ return;
290
+ }
777
291
  const queue = rest.queues.get(url);
778
292
  if (queue !== undefined) {
779
293
  queue.makeRequest(request);
@@ -789,35 +303,9 @@ export function createRestManager(options) {
789
303
  rest.queues.set(url, bucketQueue);
790
304
  }
791
305
  },
792
- async makeRequest (method, url, body, options) {
306
+ async makeRequest (method, url, options) {
793
307
  if (!rest.baseUrl.startsWith('https://discord.com') && url[0] === '/') {
794
- // Special handling for sending blobs across http to proxy
795
- if (body?.file) {
796
- if (!Array.isArray(body.file)) {
797
- body.file = [
798
- body.file
799
- ];
800
- }
801
- // convert blobs to string before sending to proxy
802
- body.file = await Promise.all(body.file.map(async (f)=>{
803
- const url = encode(await f.blob.arrayBuffer());
804
- return {
805
- name: f.name,
806
- blob: `data:${f.blob.type};base64,${url}`
807
- };
808
- }));
809
- }
810
- const headers = {
811
- Authorization: rest.authorization ?? ""
812
- };
813
- if (body) {
814
- headers['Content-Type'] = 'application/json';
815
- }
816
- const result = await fetch(`${rest.baseUrl}${url}`, {
817
- body: body ? JSON.stringify(body) : undefined,
818
- headers,
819
- method
820
- });
308
+ const result = await fetch(`${rest.baseUrl}${url}`, rest.createRequestBody(method, options));
821
309
  if (!result.ok) {
822
310
  const err = await result.json().catch(()=>{});
823
311
  // Legacy Handling to not break old code or when body is missing
@@ -826,38 +314,39 @@ export function createRestManager(options) {
826
314
  }
827
315
  return result.status !== 204 ? await result.json() : undefined;
828
316
  }
829
- return await new Promise((resolve, reject)=>{
317
+ // eslint-disable-next-line no-async-promise-executor
318
+ return await new Promise(async (resolve, reject)=>{
830
319
  const payload = {
831
320
  url,
832
321
  method,
833
- body,
322
+ requestBodyOptions: options,
834
323
  retryCount: 0,
835
- retryRequest: async function(options) {
836
- rest.processRequest(payload);
324
+ retryRequest: async function(payload) {
325
+ await rest.processRequest(payload);
837
326
  },
838
327
  resolve: (data)=>{
839
328
  resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
840
329
  },
841
330
  reject,
842
- options
331
+ runThroughQueue: options?.runThroughQueue
843
332
  };
844
- rest.processRequest(payload);
333
+ await rest.processRequest(payload);
845
334
  });
846
335
  },
847
- async get (url) {
848
- return camelize(await rest.makeRequest('GET', url));
336
+ async get (url, options) {
337
+ return camelize(await rest.makeRequest('GET', url, options));
849
338
  },
850
- async post (url, body) {
851
- return camelize(await rest.makeRequest('POST', url, body));
339
+ async post (url, options) {
340
+ return camelize(await rest.makeRequest('POST', url, options));
852
341
  },
853
- async delete (url, body) {
854
- camelize(await rest.makeRequest('DELETE', url, body));
342
+ async delete (url, options) {
343
+ camelize(await rest.makeRequest('DELETE', url, options));
855
344
  },
856
- async patch (url, body) {
857
- return camelize(await rest.makeRequest('PATCH', url, body));
345
+ async patch (url, options) {
346
+ return camelize(await rest.makeRequest('PATCH', url, options));
858
347
  },
859
- async put (url, body, options) {
860
- return camelize(await rest.makeRequest('PUT', url, body, options));
348
+ async put (url, options) {
349
+ return camelize(await rest.makeRequest('PUT', url, options));
861
350
  },
862
351
  async addReaction (channelId, messageId, reaction) {
863
352
  reaction = processReactionString(reaction);
@@ -882,59 +371,101 @@ export function createRestManager(options) {
882
371
  async addThreadMember (channelId, userId) {
883
372
  await rest.put(rest.routes.channels.threads.user(channelId, userId));
884
373
  },
885
- async createAutomodRule (guildId, options) {
886
- return await rest.post(rest.routes.guilds.automod.rules(guildId), options);
374
+ async createAutomodRule (guildId, body, reason) {
375
+ return await rest.post(rest.routes.guilds.automod.rules(guildId), {
376
+ body,
377
+ reason
378
+ });
887
379
  },
888
- async createChannel (guildId, options) {
889
- return await rest.post(rest.routes.guilds.channels(guildId), options);
380
+ async createChannel (guildId, body, reason) {
381
+ return await rest.post(rest.routes.guilds.channels(guildId), {
382
+ body,
383
+ reason
384
+ });
890
385
  },
891
- async createEmoji (guildId, options) {
892
- return await rest.post(rest.routes.guilds.emojis(guildId), options);
386
+ async createEmoji (guildId, body, reason) {
387
+ return await rest.post(rest.routes.guilds.emojis(guildId), {
388
+ body,
389
+ reason
390
+ });
893
391
  },
894
- async createGlobalApplicationCommand (command) {
895
- return await rest.post(rest.routes.interactions.commands.commands(rest.applicationId), command);
392
+ async createGlobalApplicationCommand (body) {
393
+ return await rest.post(rest.routes.interactions.commands.commands(rest.applicationId), {
394
+ body
395
+ });
896
396
  },
897
- async createGuild (options) {
898
- return await rest.post(rest.routes.guilds.all(), options);
397
+ async createGuild (body) {
398
+ return await rest.post(rest.routes.guilds.all(), {
399
+ body
400
+ });
899
401
  },
900
- async createGuildApplicationCommand (command, guildId) {
901
- return await rest.post(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), command);
402
+ async createGuildApplicationCommand (body, guildId) {
403
+ return await rest.post(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
404
+ body
405
+ });
902
406
  },
903
- async createGuildFromTemplate (templateCode, options) {
904
- if (options.icon) {
905
- options.icon = await urlToBase64(options.icon);
407
+ async createGuildFromTemplate (templateCode, body) {
408
+ if (body.icon) {
409
+ body.icon = await urlToBase64(body.icon);
906
410
  }
907
- return await rest.post(rest.routes.guilds.templates.code(templateCode), options);
411
+ return await rest.post(rest.routes.guilds.templates.code(templateCode), {
412
+ body
413
+ });
908
414
  },
909
- async createGuildSticker (guildId, options) {
910
- return await rest.post(rest.routes.guilds.stickers(guildId), options);
415
+ async createGuildSticker (guildId, options, reason) {
416
+ const form = new FormData();
417
+ form.append('file', options.file.blob, options.file.name);
418
+ form.append('name', options.name);
419
+ form.append('description', options.description);
420
+ form.append('tags', options.tags);
421
+ return await rest.post(rest.routes.guilds.stickers(guildId), {
422
+ body: form,
423
+ reason
424
+ });
911
425
  },
912
- async createGuildTemplate (guildId, options) {
913
- return await rest.post(rest.routes.guilds.templates.all(guildId), options);
426
+ async createGuildTemplate (guildId, body) {
427
+ return await rest.post(rest.routes.guilds.templates.all(guildId), {
428
+ body
429
+ });
914
430
  },
915
- async createForumThread (channelId, options) {
916
- return await rest.post(rest.routes.channels.forum(channelId), options);
431
+ async createForumThread (channelId, body, reason) {
432
+ return await rest.post(rest.routes.channels.forum(channelId), {
433
+ body,
434
+ files: body.files,
435
+ reason
436
+ });
917
437
  },
918
- async createInvite (channelId, options = {}) {
919
- return await rest.post(rest.routes.channels.invites(channelId), options);
438
+ async createInvite (channelId, body = {}, reason) {
439
+ return await rest.post(rest.routes.channels.invites(channelId), {
440
+ body,
441
+ reason
442
+ });
920
443
  },
921
- async createRole (guildId, options, reason) {
444
+ async createRole (guildId, body, reason) {
922
445
  return await rest.post(rest.routes.guilds.roles.all(guildId), {
923
- ...options,
446
+ body,
924
447
  reason
925
448
  });
926
449
  },
927
- async createScheduledEvent (guildId, options) {
928
- return await rest.post(rest.routes.guilds.events.events(guildId), options);
450
+ async createScheduledEvent (guildId, body, reason) {
451
+ return await rest.post(rest.routes.guilds.events.events(guildId), {
452
+ body,
453
+ reason
454
+ });
929
455
  },
930
- async createStageInstance (options) {
931
- return await rest.post(rest.routes.channels.stages(), options);
456
+ async createStageInstance (body, reason) {
457
+ return await rest.post(rest.routes.channels.stages(), {
458
+ body,
459
+ reason
460
+ });
932
461
  },
933
- async createWebhook (channelId, options) {
462
+ async createWebhook (channelId, options, reason) {
934
463
  return await rest.post(rest.routes.channels.webhooks(channelId), {
935
- name: options.name,
936
- avatar: options.avatar ? await urlToBase64(options.avatar) : undefined,
937
- reason: options.reason
464
+ body: {
465
+ name: options.name,
466
+ avatar: options.avatar ? await urlToBase64(options.avatar) : undefined
467
+ },
468
+ reason
938
469
  });
939
470
  },
940
471
  async deleteAutomodRule (guildId, ruleId, reason) {
@@ -948,9 +479,9 @@ export function createRestManager(options) {
948
479
  });
949
480
  },
950
481
  async deleteChannelPermissionOverride (channelId, overwriteId, reason) {
951
- await rest.delete(rest.routes.channels.overwrite(channelId, overwriteId), reason ? {
482
+ await rest.delete(rest.routes.channels.overwrite(channelId, overwriteId), {
952
483
  reason
953
- } : undefined);
484
+ });
954
485
  },
955
486
  async deleteEmoji (guildId, id, reason) {
956
487
  await rest.delete(rest.routes.guilds.emoji(guildId, id), {
@@ -958,7 +489,9 @@ export function createRestManager(options) {
958
489
  });
959
490
  },
960
491
  async deleteFollowupMessage (token, messageId) {
961
- await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId));
492
+ await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
493
+ unauthorized: true
494
+ });
962
495
  },
963
496
  async deleteGlobalApplicationCommand (commandId) {
964
497
  await rest.delete(rest.routes.interactions.commands.command(rest.applicationId, commandId));
@@ -970,20 +503,22 @@ export function createRestManager(options) {
970
503
  await rest.delete(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId));
971
504
  },
972
505
  async deleteGuildSticker (guildId, stickerId, reason) {
973
- await rest.delete(rest.routes.guilds.sticker(guildId, stickerId), reason ? {
506
+ await rest.delete(rest.routes.guilds.sticker(guildId, stickerId), {
974
507
  reason
975
- } : undefined);
508
+ });
976
509
  },
977
510
  async deleteGuildTemplate (guildId, templateCode) {
978
511
  await rest.delete(rest.routes.guilds.templates.guild(guildId, templateCode));
979
512
  },
980
- async deleteIntegration (guildId, integrationId) {
981
- await rest.delete(rest.routes.guilds.integration(guildId, integrationId));
513
+ async deleteIntegration (guildId, integrationId, reason) {
514
+ await rest.delete(rest.routes.guilds.integration(guildId, integrationId), {
515
+ reason
516
+ });
982
517
  },
983
518
  async deleteInvite (inviteCode, reason) {
984
- await rest.delete(rest.routes.guilds.invite(inviteCode), reason ? {
519
+ await rest.delete(rest.routes.guilds.invite(inviteCode), {
985
520
  reason
986
- } : undefined);
521
+ });
987
522
  },
988
523
  async deleteMessage (channelId, messageId, reason) {
989
524
  await rest.delete(rest.routes.channels.message(channelId, messageId), {
@@ -992,12 +527,16 @@ export function createRestManager(options) {
992
527
  },
993
528
  async deleteMessages (channelId, messageIds, reason) {
994
529
  await rest.post(rest.routes.channels.bulk(channelId), {
995
- messages: messageIds.slice(0, 100).map((id)=>id.toString()),
530
+ body: {
531
+ messages: messageIds.slice(0, 100).map((id)=>id.toString())
532
+ },
996
533
  reason
997
534
  });
998
535
  },
999
536
  async deleteOriginalInteractionResponse (token) {
1000
- await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token));
537
+ await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token), {
538
+ unauthorized: true
539
+ });
1001
540
  },
1002
541
  async deleteOwnReaction (channelId, messageId, reaction) {
1003
542
  reaction = processReactionString(reaction);
@@ -1010,16 +549,18 @@ export function createRestManager(options) {
1010
549
  reaction = processReactionString(reaction);
1011
550
  await rest.delete(rest.routes.channels.reactions.emoji(channelId, messageId, reaction));
1012
551
  },
1013
- async deleteRole (guildId, roleId) {
1014
- await rest.delete(rest.routes.guilds.roles.one(guildId, roleId));
552
+ async deleteRole (guildId, roleId, reason) {
553
+ await rest.delete(rest.routes.guilds.roles.one(guildId, roleId), {
554
+ reason
555
+ });
1015
556
  },
1016
557
  async deleteScheduledEvent (guildId, eventId) {
1017
558
  await rest.delete(rest.routes.guilds.events.event(guildId, eventId));
1018
559
  },
1019
560
  async deleteStageInstance (channelId, reason) {
1020
- await rest.delete(rest.routes.channels.stage(channelId), reason ? {
561
+ await rest.delete(rest.routes.channels.stage(channelId), {
1021
562
  reason
1022
- } : undefined);
563
+ });
1023
564
  },
1024
565
  async deleteUserReaction (channelId, messageId, userId, reaction) {
1025
566
  reaction = processReactionString(reaction);
@@ -1036,122 +577,195 @@ export function createRestManager(options) {
1036
577
  async deleteWebhookWithToken (webhookId, token) {
1037
578
  await rest.delete(rest.routes.webhooks.webhook(webhookId, token));
1038
579
  },
1039
- async editApplicationCommandPermissions (guildId, commandId, bearerToken, options) {
580
+ async editApplicationCommandPermissions (guildId, commandId, bearerToken, permissions) {
1040
581
  return await rest.put(rest.routes.interactions.commands.permission(rest.applicationId, guildId, commandId), {
1041
- permissions: options
1042
- }, {
582
+ body: {
583
+ permissions
584
+ },
1043
585
  headers: {
1044
586
  authorization: `Bearer ${bearerToken}`
1045
587
  }
1046
588
  });
1047
589
  },
1048
- async editAutomodRule (guildId, ruleId, options) {
1049
- return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), options);
590
+ async editAutomodRule (guildId, ruleId, body, reason) {
591
+ return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), {
592
+ body,
593
+ reason
594
+ });
1050
595
  },
1051
596
  async editBotProfile (options) {
1052
597
  const avatar = options?.botAvatarURL ? await urlToBase64(options?.botAvatarURL) : options?.botAvatarURL;
1053
598
  return await rest.patch(rest.routes.userBot(), {
1054
- username: options.username?.trim(),
1055
- avatar
599
+ body: {
600
+ username: options.username?.trim(),
601
+ avatar
602
+ }
1056
603
  });
1057
604
  },
1058
- async editChannel (channelId, options) {
1059
- return await rest.patch(rest.routes.channels.channel(channelId), options);
605
+ async editChannel (channelId, body, reason) {
606
+ return await rest.patch(rest.routes.channels.channel(channelId), {
607
+ body,
608
+ reason
609
+ });
1060
610
  },
1061
- async editChannelPermissionOverrides (channelId, options) {
1062
- await rest.put(rest.routes.channels.overwrite(channelId, options.id), options);
611
+ async editChannelPermissionOverrides (channelId, body, reason) {
612
+ await rest.put(rest.routes.channels.overwrite(channelId, body.id), {
613
+ body,
614
+ reason
615
+ });
1063
616
  },
1064
- async editChannelPositions (guildId, channelPositions) {
1065
- await rest.patch(rest.routes.guilds.channels(guildId), channelPositions);
617
+ async editChannelPositions (guildId, body) {
618
+ await rest.patch(rest.routes.guilds.channels(guildId), {
619
+ body
620
+ });
1066
621
  },
1067
- async editEmoji (guildId, id, options) {
1068
- return await rest.patch(rest.routes.guilds.emoji(guildId, id), options);
622
+ async editEmoji (guildId, id, body, reason) {
623
+ return await rest.patch(rest.routes.guilds.emoji(guildId, id), {
624
+ body,
625
+ reason
626
+ });
1069
627
  },
1070
- async editFollowupMessage (token, messageId, options) {
1071
- return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), options);
628
+ async editFollowupMessage (token, messageId, body) {
629
+ return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
630
+ body,
631
+ files: body.files,
632
+ unauthorized: true
633
+ });
1072
634
  },
1073
- async editGlobalApplicationCommand (commandId, options) {
1074
- return await rest.patch(rest.routes.interactions.commands.command(rest.applicationId, commandId), options);
635
+ async editGlobalApplicationCommand (commandId, body) {
636
+ return await rest.patch(rest.routes.interactions.commands.command(rest.applicationId, commandId), {
637
+ body
638
+ });
1075
639
  },
1076
- async editGuild (guildId, options) {
1077
- return await rest.patch(rest.routes.guilds.guild(guildId), options);
640
+ async editGuild (guildId, body, reason) {
641
+ return await rest.patch(rest.routes.guilds.guild(guildId), {
642
+ body,
643
+ reason
644
+ });
1078
645
  },
1079
- async editGuildApplicationCommand (commandId, guildId, options) {
1080
- return await rest.patch(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId), options);
646
+ async editGuildApplicationCommand (commandId, guildId, body) {
647
+ return await rest.patch(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId), {
648
+ body
649
+ });
1081
650
  },
1082
651
  async editGuildMfaLevel (guildId, mfaLevel, reason) {
1083
652
  await rest.post(rest.routes.guilds.mfa(guildId), {
1084
- level: mfaLevel,
653
+ body: {
654
+ level: mfaLevel
655
+ },
1085
656
  reason
1086
657
  });
1087
658
  },
1088
- async editGuildSticker (guildId, stickerId, options) {
1089
- return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), options);
659
+ async editGuildSticker (guildId, stickerId, body, reason) {
660
+ return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), {
661
+ body,
662
+ reason
663
+ });
1090
664
  },
1091
- async editGuildTemplate (guildId, templateCode, options) {
1092
- return await rest.patch(rest.routes.guilds.templates.guild(guildId, templateCode), options);
665
+ async editGuildTemplate (guildId, templateCode, body) {
666
+ return await rest.patch(rest.routes.guilds.templates.guild(guildId, templateCode), {
667
+ body
668
+ });
1093
669
  },
1094
- async editMessage (channelId, messageId, options) {
1095
- return await rest.patch(rest.routes.channels.message(channelId, messageId), options);
670
+ async editMessage (channelId, messageId, body) {
671
+ return await rest.patch(rest.routes.channels.message(channelId, messageId), {
672
+ body
673
+ });
1096
674
  },
1097
- async editOriginalInteractionResponse (token, options) {
1098
- return await rest.patch(rest.routes.interactions.responses.original(rest.applicationId, token), options);
675
+ async editOriginalInteractionResponse (token, body) {
676
+ return await rest.patch(rest.routes.interactions.responses.original(rest.applicationId, token), {
677
+ body,
678
+ files: body.files
679
+ });
1099
680
  },
1100
681
  async editOriginalWebhookMessage (webhookId, token, options) {
1101
682
  return await rest.patch(rest.routes.webhooks.original(webhookId, token, options), {
1102
- type: InteractionResponseTypes.UpdateMessage,
1103
- data: options
683
+ body: {
684
+ type: InteractionResponseTypes.UpdateMessage,
685
+ data: options
686
+ },
687
+ files: options.files,
688
+ unauthorized: true
1104
689
  });
1105
690
  },
1106
691
  async editOwnVoiceState (guildId, options) {
1107
692
  await rest.patch(rest.routes.guilds.voice(guildId), {
1108
- channel_id: options.channelId,
1109
- suppress: options.suppress,
1110
- request_to_speak_timestamp: options.requestToSpeakTimestamp ? new Date(options.requestToSpeakTimestamp).toISOString() : options.requestToSpeakTimestamp
693
+ body: {
694
+ ...options,
695
+ request_to_speak_timestamp: options.requestToSpeakTimestamp ? new Date(options.requestToSpeakTimestamp).toISOString() : options.requestToSpeakTimestamp
696
+ }
1111
697
  });
1112
698
  },
1113
- async editScheduledEvent (guildId, eventId, options) {
1114
- return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), options);
699
+ async editScheduledEvent (guildId, eventId, body, reason) {
700
+ return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), {
701
+ body,
702
+ reason
703
+ });
1115
704
  },
1116
- async editRole (guildId, roleId, options) {
1117
- return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), options);
705
+ async editRole (guildId, roleId, body, reason) {
706
+ return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), {
707
+ body,
708
+ reason
709
+ });
1118
710
  },
1119
- async editRolePositions (guildId, options) {
1120
- return await rest.patch(rest.routes.guilds.roles.all(guildId), options);
711
+ async editRolePositions (guildId, body, reason) {
712
+ return await rest.patch(rest.routes.guilds.roles.all(guildId), {
713
+ body,
714
+ reason
715
+ });
1121
716
  },
1122
- async editStageInstance (channelId, data) {
717
+ async editStageInstance (channelId, topic, reason) {
1123
718
  return await rest.patch(rest.routes.channels.stage(channelId), {
1124
- topic: data.topic
719
+ body: {
720
+ topic
721
+ },
722
+ reason
1125
723
  });
1126
724
  },
1127
725
  async editUserVoiceState (guildId, options) {
1128
726
  await rest.patch(rest.routes.guilds.voice(guildId, options.userId), {
1129
- channel_id: options.channelId,
1130
- suppress: options.suppress,
1131
- user_id: options.userId
727
+ body: options
1132
728
  });
1133
729
  },
1134
- async editWebhook (webhookId, options) {
1135
- return await rest.patch(rest.routes.webhooks.id(webhookId), options);
730
+ async editWebhook (webhookId, body, reason) {
731
+ return await rest.patch(rest.routes.webhooks.id(webhookId), {
732
+ body,
733
+ reason
734
+ });
1136
735
  },
1137
736
  async editWebhookMessage (webhookId, token, messageId, options) {
1138
- return await rest.patch(rest.routes.webhooks.message(webhookId, token, messageId, options), options);
737
+ return await rest.patch(rest.routes.webhooks.message(webhookId, token, messageId, options), {
738
+ body: options,
739
+ files: options.files
740
+ });
1139
741
  },
1140
- async editWebhookWithToken (webhookId, token, options) {
1141
- return await rest.patch(rest.routes.webhooks.webhook(webhookId, token), options);
742
+ async editWebhookWithToken (webhookId, token, body) {
743
+ return await rest.patch(rest.routes.webhooks.webhook(webhookId, token), {
744
+ body
745
+ });
1142
746
  },
1143
- async editWelcomeScreen (guildId, options) {
1144
- return await rest.patch(rest.routes.guilds.welcome(guildId), options);
747
+ async editWelcomeScreen (guildId, body, reason) {
748
+ return await rest.patch(rest.routes.guilds.welcome(guildId), {
749
+ body,
750
+ reason
751
+ });
1145
752
  },
1146
- async editWidgetSettings (guildId, options) {
1147
- return await rest.patch(rest.routes.guilds.widget(guildId), options);
753
+ async editWidgetSettings (guildId, body, reason) {
754
+ return await rest.patch(rest.routes.guilds.widget(guildId), {
755
+ body,
756
+ reason
757
+ });
1148
758
  },
1149
759
  async executeWebhook (webhookId, token, options) {
1150
- return await rest.post(rest.routes.webhooks.webhook(webhookId, token, options), options);
760
+ return await rest.post(rest.routes.webhooks.webhook(webhookId, token, options), {
761
+ body: options
762
+ });
1151
763
  },
1152
764
  async followAnnouncement (sourceChannelId, targetChannelId) {
1153
765
  return await rest.post(rest.routes.channels.follow(sourceChannelId), {
1154
- webhook_channel_id: targetChannelId
766
+ body: {
767
+ webhook_channel_id: targetChannelId
768
+ }
1155
769
  });
1156
770
  },
1157
771
  async getActiveThreads (guildId) {
@@ -1198,7 +812,9 @@ export function createRestManager(options) {
1198
812
  },
1199
813
  async getDmChannel (userId) {
1200
814
  return await rest.post(rest.routes.channels.dm(), {
1201
- recipient_id: userId.toString()
815
+ body: {
816
+ recipient_id: userId
817
+ }
1202
818
  });
1203
819
  },
1204
820
  async getEmoji (guildId, emojiId) {
@@ -1208,7 +824,9 @@ export function createRestManager(options) {
1208
824
  return await rest.get(rest.routes.guilds.emojis(guildId));
1209
825
  },
1210
826
  async getFollowupMessage (token, messageId) {
1211
- return await rest.get(rest.routes.interactions.responses.message(rest.applicationId, token, messageId));
827
+ return await rest.get(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
828
+ unauthorized: true
829
+ });
1212
830
  },
1213
831
  async getGatewayBot () {
1214
832
  return await rest.get(rest.routes.gatewayBot());
@@ -1261,7 +879,9 @@ export function createRestManager(options) {
1261
879
  return await rest.get(rest.routes.nitroStickerPacks());
1262
880
  },
1263
881
  async getOriginalInteractionResponse (token) {
1264
- return await rest.get(rest.routes.interactions.responses.original(rest.applicationId, token));
882
+ return await rest.get(rest.routes.interactions.responses.original(rest.applicationId, token), {
883
+ unauthorized: true
884
+ });
1265
885
  },
1266
886
  async getPinnedMessages (channelId) {
1267
887
  return await rest.get(rest.routes.channels.pins(channelId));
@@ -1362,61 +982,58 @@ export function createRestManager(options) {
1362
982
  await rest.delete(rest.routes.channels.threads.user(channelId, userId));
1363
983
  },
1364
984
  async sendFollowupMessage (token, options) {
1365
- return await new Promise((resolve, reject)=>{
1366
- rest.sendRequest({
1367
- url: rest.routes.webhooks.webhook(rest.applicationId, token),
1368
- method: 'POST',
1369
- body: options,
1370
- retryCount: 0,
1371
- retryRequest: async function(options) {
1372
- // TODO: should change to reprocess queue item
1373
- await rest.sendRequest(options);
1374
- },
1375
- resolve: (data)=>{
1376
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1377
- },
1378
- reject
1379
- });
985
+ return await rest.post(rest.routes.webhooks.webhook(rest.applicationId, token), {
986
+ body: options,
987
+ files: options.files,
988
+ unauthorized: true
1380
989
  });
1381
990
  },
1382
991
  async sendInteractionResponse (interactionId, token, options) {
1383
- await new Promise((resolve, reject)=>{
1384
- rest.sendRequest({
1385
- url: rest.routes.interactions.responses.callback(interactionId, token),
1386
- method: 'POST',
1387
- body: options,
1388
- retryCount: 0,
1389
- retryRequest: async function(options) {
1390
- // TODO: should change to reprocess queue item
1391
- await rest.sendRequest(options);
1392
- },
1393
- resolve: (data)=>{
1394
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1395
- },
1396
- reject
1397
- });
992
+ return await rest.post(rest.routes.interactions.responses.callback(interactionId, token), {
993
+ body: options,
994
+ files: options.data?.files,
995
+ runThroughQueue: false,
996
+ unauthorized: true
1398
997
  });
1399
998
  },
1400
- async sendMessage (channelId, options) {
1401
- return await rest.post(rest.routes.channels.messages(channelId), options);
999
+ async sendMessage (channelId, body) {
1000
+ return await rest.post(rest.routes.channels.messages(channelId), {
1001
+ body,
1002
+ files: body.files
1003
+ });
1402
1004
  },
1403
- async startThreadWithMessage (channelId, messageId, options) {
1404
- return await rest.post(rest.routes.channels.threads.message(channelId, messageId), options);
1005
+ async startThreadWithMessage (channelId, messageId, body, reason) {
1006
+ return await rest.post(rest.routes.channels.threads.message(channelId, messageId), {
1007
+ body,
1008
+ reason
1009
+ });
1405
1010
  },
1406
- async startThreadWithoutMessage (channelId, options) {
1407
- return await rest.post(rest.routes.channels.threads.all(channelId), options);
1011
+ async startThreadWithoutMessage (channelId, body, reason) {
1012
+ return await rest.post(rest.routes.channels.threads.all(channelId), {
1013
+ body,
1014
+ reason
1015
+ });
1408
1016
  },
1409
1017
  async syncGuildTemplate (guildId) {
1410
1018
  return await rest.put(rest.routes.guilds.templates.all(guildId));
1411
1019
  },
1412
- async banMember (guildId, userId, options) {
1413
- await rest.put(rest.routes.guilds.members.ban(guildId, userId), options);
1020
+ async banMember (guildId, userId, body, reason) {
1021
+ await rest.put(rest.routes.guilds.members.ban(guildId, userId), {
1022
+ body,
1023
+ reason
1024
+ });
1414
1025
  },
1415
- async editBotMember (guildId, options) {
1416
- return await rest.patch(rest.routes.guilds.members.bot(guildId), options);
1026
+ async editBotMember (guildId, body, reason) {
1027
+ return await rest.patch(rest.routes.guilds.members.bot(guildId), {
1028
+ body,
1029
+ reason
1030
+ });
1417
1031
  },
1418
- async editMember (guildId, userId, options) {
1419
- return await rest.patch(rest.routes.guilds.members.member(guildId, userId), options);
1032
+ async editMember (guildId, userId, body, reason) {
1033
+ return await rest.patch(rest.routes.guilds.members.member(guildId, userId), {
1034
+ body,
1035
+ reason
1036
+ });
1420
1037
  },
1421
1038
  async getMember (guildId, userId) {
1422
1039
  return await rest.get(rest.routes.guilds.members.member(guildId, userId));
@@ -1430,32 +1047,41 @@ export function createRestManager(options) {
1430
1047
  });
1431
1048
  },
1432
1049
  async pinMessage (channelId, messageId, reason) {
1433
- await rest.put(rest.routes.channels.pin(channelId, messageId), reason ? {
1050
+ await rest.put(rest.routes.channels.pin(channelId, messageId), {
1434
1051
  reason
1435
- } : undefined);
1052
+ });
1436
1053
  },
1437
- async pruneMembers (guildId, options) {
1438
- return await rest.post(rest.routes.guilds.members.prune(guildId), options);
1054
+ async pruneMembers (guildId, body, reason) {
1055
+ return await rest.post(rest.routes.guilds.members.prune(guildId), {
1056
+ body,
1057
+ reason
1058
+ });
1439
1059
  },
1440
1060
  async searchMembers (guildId, query, options) {
1441
1061
  return await rest.get(rest.routes.guilds.members.search(guildId, query, options));
1442
1062
  },
1443
- async unbanMember (guildId, userId) {
1444
- await rest.delete(rest.routes.guilds.members.ban(guildId, userId));
1063
+ async unbanMember (guildId, userId, reason) {
1064
+ await rest.delete(rest.routes.guilds.members.ban(guildId, userId), {
1065
+ reason
1066
+ });
1445
1067
  },
1446
1068
  async unpinMessage (channelId, messageId, reason) {
1447
- await rest.delete(rest.routes.channels.pin(channelId, messageId), reason ? {
1069
+ await rest.delete(rest.routes.channels.pin(channelId, messageId), {
1448
1070
  reason
1449
- } : undefined);
1071
+ });
1450
1072
  },
1451
1073
  async triggerTypingIndicator (channelId) {
1452
1074
  await rest.post(rest.routes.channels.typing(channelId));
1453
1075
  },
1454
- async upsertGlobalApplicationCommands (commands) {
1455
- return await rest.put(rest.routes.interactions.commands.commands(rest.applicationId), commands);
1076
+ async upsertGlobalApplicationCommands (body) {
1077
+ return await rest.put(rest.routes.interactions.commands.commands(rest.applicationId), {
1078
+ body
1079
+ });
1456
1080
  },
1457
- async upsertGuildApplicationCommands (guildId, commands) {
1458
- return await rest.put(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), commands);
1081
+ async upsertGuildApplicationCommands (guildId, body) {
1082
+ return await rest.put(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
1083
+ body
1084
+ });
1459
1085
  }
1460
1086
  };
1461
1087
  return rest;