@discordeno/rest 19.0.0-next.d757773 → 19.0.0-next.da74ea7

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, 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,490 +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.after) url += `&after=${options.after}`;
222
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
223
- if (options.limit) url += `&limit=${options.limit}`;
224
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
225
- if (options.userId) url += `&user_id=${options.userId}`;
226
- }
227
- return url;
228
- },
229
- automod: {
230
- rule: (guildId, ruleId)=>{
231
- return `/guilds/${guildId}/auto-moderation/rules/${ruleId}`;
232
- },
233
- rules: (guildId)=>{
234
- return `/guilds/${guildId}/auto-moderation/rules`;
235
- }
236
- },
237
- channels: (guildId)=>{
238
- return `/guilds/${guildId}/channels`;
239
- },
240
- emoji: (guildId, emojiId)=>{
241
- return `/guilds/${guildId}/emojis/${emojiId}`;
242
- },
243
- emojis: (guildId)=>{
244
- return `/guilds/${guildId}/emojis`;
245
- },
246
- events: {
247
- events: (guildId, withUserCount)=>{
248
- let url = `/guilds/${guildId}/scheduled-events?`;
249
- if (withUserCount !== undefined) {
250
- url += `with_user_count=${withUserCount.toString()}`;
251
- }
252
- return url;
253
- },
254
- event: (guildId, eventId, withUserCount)=>{
255
- let url = `/guilds/${guildId}/scheduled-events/${eventId}`;
256
- if (withUserCount !== undefined) {
257
- url += `with_user_count=${withUserCount.toString()}`;
258
- }
259
- return url;
260
- },
261
- users: (guildId, eventId, options)=>{
262
- let url = `/guilds/${guildId}/scheduled-events/${eventId}/users?`;
263
- if (options) {
264
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
265
- if (options.limit !== undefined) url += `limit=${options.limit}`;
266
- if (options.withMember !== undefined) {
267
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
268
- url += `&with_member=${options.withMember.toString()}`;
269
- }
270
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
271
- if (options.after !== undefined) url += `&after=${options.after}`;
272
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
273
- if (options.before !== undefined) url += `&before=${options.before}`;
274
- }
275
- return url;
276
- }
277
- },
278
- guild (guildId, withCounts) {
279
- let url = `/guilds/${guildId}?`;
280
- if (withCounts !== undefined) {
281
- url += `with_counts=${withCounts.toString()}`;
282
- }
283
- return url;
284
- },
285
- integration (guildId, integrationId) {
286
- return `/guilds/${guildId}/integrations/${integrationId}`;
287
- },
288
- integrations: (guildId)=>{
289
- return `/guilds/${guildId}/integrations?include_applications=true`;
290
- },
291
- invite (inviteCode, options) {
292
- let url = `/invites/${inviteCode}?`;
293
- if (options) {
294
- if (options.withCounts !== undefined) {
295
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
296
- url += `with_counts=${options.withCounts.toString()}`;
297
- }
298
- if (options.withExpiration !== undefined) {
299
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
300
- url += `&with_expiration=${options.withExpiration.toString()}`;
301
- }
302
- if (options.scheduledEventId) {
303
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
304
- url += `&guild_scheduled_event_id=${options.scheduledEventId}`;
305
- }
306
- }
307
- return url;
308
- },
309
- invites: (guildId)=>{
310
- return `/guilds/${guildId}/invites`;
311
- },
312
- leave: (guildId)=>{
313
- return `/users/@me/guilds/${guildId}`;
314
- },
315
- members: {
316
- ban: (guildId, userId)=>{
317
- return `/guilds/${guildId}/bans/${userId}`;
318
- },
319
- bans: (guildId, options)=>{
320
- let url = `/guilds/${guildId}/bans?`;
321
- if (options) {
322
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
323
- if (options.limit) url += `limit=${options.limit}`;
324
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
325
- if (options.after) url += `&after=${options.after}`;
326
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
327
- if (options.before) url += `&before=${options.before}`;
328
- }
329
- return url;
330
- },
331
- bot: (guildId)=>{
332
- return `/guilds/${guildId}/members/@me`;
333
- },
334
- member: (guildId, userId)=>{
335
- return `/guilds/${guildId}/members/${userId}`;
336
- },
337
- members: (guildId, options)=>{
338
- let url = `/guilds/${guildId}/members?`;
339
- if (options !== undefined) {
340
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
341
- if (options.limit) url += `limit=${options.limit}`;
342
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
343
- if (options.after) url += `&after=${options.after}`;
344
- }
345
- return url;
346
- },
347
- search: (guildId, query, options)=>{
348
- let url = `/guilds/${guildId}/members/search?query=${encodeURIComponent(query)}`;
349
- if (options) {
350
- if (options.limit !== undefined) url += `&limit=${options.limit}`;
351
- }
352
- return url;
353
- },
354
- prune: (guildId, options)=>{
355
- let url = `/guilds/${guildId}/prune?`;
356
- if (options) {
357
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
358
- if (options.days) url += `days=${options.days}`;
359
- if (Array.isArray(options.includeRoles)) {
360
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
361
- url += `&include_roles=${options.includeRoles.join(',')}`;
362
- } else if (options.includeRoles) {
363
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
364
- url += `&include_roles=${options.includeRoles}`;
365
- }
366
- }
367
- return url;
368
- }
369
- },
370
- mfa: (guildId)=>`/guilds/${guildId}/mfa`,
371
- preview: (guildId)=>{
372
- return `/guilds/${guildId}/preview`;
373
- },
374
- prune: (guildId, options)=>{
375
- let url = `/guilds/${guildId}/prune?`;
376
- if (options) {
377
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
378
- if (options.days) url += `days=${options.days}`;
379
- if (Array.isArray(options.includeRoles)) {
380
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
381
- url += `&include_roles=${options.includeRoles.join(',')}`;
382
- } else if (options.includeRoles) {
383
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
384
- url += `&include_roles=${options.includeRoles}`;
385
- }
386
- }
387
- return url;
388
- },
389
- roles: {
390
- one: (guildId, roleId)=>{
391
- return `/guilds/${guildId}/roles/${roleId}`;
392
- },
393
- all: (guildId)=>{
394
- return `/guilds/${guildId}/roles`;
395
- },
396
- member: (guildId, memberId, roleId)=>{
397
- return `/guilds/${guildId}/members/${memberId}/roles/${roleId}`;
398
- }
399
- },
400
- stickers: (guildId)=>{
401
- return `/guilds/${guildId}/stickers`;
402
- },
403
- sticker: (guildId, stickerId)=>{
404
- return `/guilds/${guildId}/stickers/${stickerId}`;
405
- },
406
- voice: (guildId, userId)=>{
407
- return `/guilds/${guildId}/voice-states/${userId ?? '@me'}`;
408
- },
409
- templates: {
410
- code: (code)=>{
411
- return `/guilds/templates/${code}`;
412
- },
413
- guild: (guildId, code)=>{
414
- return `/guilds/${guildId}/templates/${code}`;
415
- },
416
- all: (guildId)=>{
417
- return `/guilds/${guildId}/templates`;
418
- }
419
- },
420
- vanity: (guildId)=>{
421
- return `/guilds/${guildId}/vanity-url`;
422
- },
423
- regions: (guildId)=>{
424
- return `/guilds/${guildId}/regions`;
425
- },
426
- webhooks: (guildId)=>{
427
- return `/guilds/${guildId}/webhooks`;
428
- },
429
- welcome: (guildId)=>{
430
- return `/guilds/${guildId}/welcome-screen`;
431
- },
432
- widget: (guildId)=>{
433
- return `/guilds/${guildId}/widget`;
434
- },
435
- widgetJson: (guildId)=>{
436
- return `/guilds/${guildId}/widget.json`;
437
- }
438
- },
439
- sticker: (stickerId)=>{
440
- return `/stickers/${stickerId}`;
441
- },
442
- regions: ()=>{
443
- return '/voice/regions';
444
- },
445
- // Interaction Endpoints
446
- interactions: {
447
- commands: {
448
- // Application Endpoints
449
- commands: (applicationId)=>{
450
- return `/applications/${applicationId}/commands`;
451
- },
452
- guilds: {
453
- all (applicationId, guildId) {
454
- return `/applications/${applicationId}/guilds/${guildId}/commands`;
455
- },
456
- one (applicationId, guildId, commandId, withLocalizations) {
457
- let url = `/applications/${applicationId}/guilds/${guildId}/commands/${commandId}?`;
458
- if (withLocalizations !== undefined) {
459
- url += `with_localizations=${withLocalizations.toString()}`;
460
- }
461
- return url;
462
- }
463
- },
464
- permissions: (applicationId, guildId)=>{
465
- return `/applications/${applicationId}/guilds/${guildId}/commands/permissions`;
466
- },
467
- permission: (applicationId, guildId, commandId)=>{
468
- return `/applications/${applicationId}/guilds/${guildId}/commands/${commandId}/permissions`;
469
- },
470
- command: (applicationId, commandId, withLocalizations)=>{
471
- let url = `/applications/${applicationId}/commands/${commandId}?`;
472
- if (withLocalizations !== undefined) {
473
- url += `withLocalizations=${withLocalizations.toString()}`;
474
- }
475
- return url;
476
- }
477
- },
478
- responses: {
479
- // Interaction Endpoints
480
- callback: (interactionId, token)=>{
481
- return `/interactions/${interactionId}/${token}/callback`;
482
- },
483
- original: (interactionId, token)=>{
484
- return `/webhooks/${interactionId}/${token}/messages/@original`;
485
- },
486
- message: (applicationId, token, messageId)=>{
487
- return `/webhooks/${applicationId}/${token}/messages/${messageId}`;
488
- }
489
- }
490
- },
491
- // User endpoints
492
- user (userId) {
493
- return `/users/${userId}`;
494
- },
495
- userBot () {
496
- return '/users/@me';
497
- },
498
- oauth2Application () {
499
- return 'oauth2/applications/@me';
500
- },
501
- gatewayBot () {
502
- return '/gateway/bot';
503
- },
504
- nitroStickerPacks () {
505
- return '/sticker-packs';
506
- }
507
- },
27
+ routes: createRoutes(),
508
28
  checkRateLimits (url) {
509
29
  const ratelimited = rest.rateLimitedPaths.get(url);
510
30
  const global = rest.rateLimitedPaths.get('global');
@@ -545,46 +65,47 @@ export function createRestManager(options) {
545
65
  if (typeof obj === 'bigint') return obj.toString();
546
66
  return obj;
547
67
  },
548
- createRequest (options) {
68
+ createRequestBody (method, options) {
549
69
  const headers = {
550
70
  'user-agent': `DiscordBot (https://github.com/discordeno/discordeno, v${version})`
551
71
  };
552
- if (!options.unauthorized) headers.authorization = `Bot ${rest.token}`;
72
+ if (options?.unauthorized !== false) headers.authorization = `Bot ${rest.token}`;
553
73
  // IF A REASON IS PROVIDED ENCODE IT IN HEADERS
554
- if (options.reason !== undefined) {
555
- headers['x-audit-log-reason'] = encodeURIComponent(options.reason);
74
+ if (options?.reason !== undefined) {
75
+ headers['x-audit-log-reason'] = encodeURIComponent(options?.reason);
556
76
  }
557
77
  let body;
558
78
  // TODO: check if we need to add specific check for GET method
559
79
  // Since GET does not allow bodies
560
80
  // Have to check for attachments first, since body then has to be send in a different way.
561
- if (options.attachments !== undefined) {
81
+ if (options?.files !== undefined) {
562
82
  const form = new FormData();
563
- for(let i = 0; i < options.attachments.length; ++i){
564
- form.append(`file${i}`, options.attachments[i].blob, options.attachments[i].name);
83
+ for(let i = 0; i < options.files.length; ++i){
84
+ form.append(`file${i}`, options.files[i].blob, options.files[i].name);
565
85
  }
566
- form.append('payload_json', JSON.stringify(options.body));
86
+ form.append('payload_json', JSON.stringify({
87
+ ...options.body,
88
+ files: undefined
89
+ }));
567
90
  body = form;
568
- // TODO: boundary?
569
- // `multipart/form-data; boundary=${form.getBoundary()}`
570
- headers['content-type'] = `multipart/form-data`;
571
- } else if (options.body !== undefined) {
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) {
572
93
  if (options.body instanceof FormData) {
573
94
  body = options.body;
574
- headers['content-type'] = `multipart/form-data`;
95
+ // No need to set the `content-type` header since `fetch` does that automatically for us when we use a `FormData` object.
575
96
  } else {
576
- body = JSON.stringify(options.body);
97
+ body = JSON.stringify(rest.changeToDiscordFormat(options.body));
577
98
  headers['content-type'] = `application/json`;
578
99
  }
579
100
  }
580
101
  // SOMETIMES SPECIAL HEADERS (E.G. CUSTOM AUTHORIZATION) NEED TO BE USED
581
- if (options.headers) {
102
+ if (options?.headers) {
582
103
  Object.assign(headers, options.headers);
583
104
  }
584
105
  return {
585
106
  body,
586
107
  headers,
587
- method: options.method
108
+ method
588
109
  };
589
110
  },
590
111
  processRateLimitedPaths () {
@@ -679,12 +200,7 @@ export function createRestManager(options) {
679
200
  },
680
201
  async sendRequest (options) {
681
202
  const url = options.url.startsWith('https://') ? options.url : `${rest.baseUrl}/v${rest.version}${options.url}`;
682
- const payload = rest.createRequest({
683
- method: options.method,
684
- url: options.url,
685
- body: options.options?.body,
686
- ...options.options
687
- });
203
+ const payload = rest.createRequestBody(options.method, options.requestBodyOptions);
688
204
  logger.debug(`sending request to ${url}`, 'with payload:', {
689
205
  ...payload,
690
206
  headers: {
@@ -758,7 +274,7 @@ export function createRestManager(options) {
758
274
  }
759
275
  return parts.join('/');
760
276
  },
761
- processRequest (request) {
277
+ async processRequest (request) {
762
278
  const route = request.url.substring(request.url.indexOf('api/'));
763
279
  const parts = route.split('/');
764
280
  // Remove the api/
@@ -768,6 +284,10 @@ export function createRestManager(options) {
768
284
  // Set the full url to discord api in case it was recieved in a proxy rest
769
285
  request.url = `${rest.baseUrl}/v${rest.version}/${parts.join('/')}`;
770
286
  const url = rest.simplifyUrl(request.url, request.method);
287
+ if (request.runThroughQueue === false) {
288
+ await rest.sendRequest(request);
289
+ return;
290
+ }
771
291
  const queue = rest.queues.get(url);
772
292
  if (queue !== undefined) {
773
293
  queue.makeRequest(request);
@@ -785,34 +305,7 @@ export function createRestManager(options) {
785
305
  },
786
306
  async makeRequest (method, url, options) {
787
307
  if (!rest.baseUrl.startsWith('https://discord.com') && url[0] === '/') {
788
- // Special handling for sending blobs across http to proxy
789
- // TODO: fix this hacky handling
790
- if (!(options?.body instanceof FormData) && !Array.isArray(options?.body) && options?.body?.file) {
791
- if (!Array.isArray(options.body.file)) {
792
- options.body.file = [
793
- options.body.file
794
- ];
795
- }
796
- // convert blobs to string before sending to proxy
797
- options.body.file = await Promise.all(options.body.file.map(async (f)=>{
798
- const url = encode(await f.blob.arrayBuffer());
799
- return {
800
- name: f.name,
801
- blob: `data:${f.blob.type};base64,${url}`
802
- };
803
- }));
804
- }
805
- const headers = {
806
- Authorization: rest.authorization ?? ''
807
- };
808
- if (options?.body) {
809
- headers['Content-Type'] = 'application/json';
810
- }
811
- const result = await fetch(`${rest.baseUrl}${url}`, {
812
- body: options?.body ? JSON.stringify(options.body) : undefined,
813
- headers,
814
- method
815
- });
308
+ const result = await fetch(`${rest.baseUrl}${url}`, rest.createRequestBody(method, options));
816
309
  if (!result.ok) {
817
310
  const err = await result.json().catch(()=>{});
818
311
  // Legacy Handling to not break old code or when body is missing
@@ -821,47 +314,39 @@ export function createRestManager(options) {
821
314
  }
822
315
  return result.status !== 204 ? await result.json() : undefined;
823
316
  }
824
- return await new Promise((resolve, reject)=>{
317
+ // eslint-disable-next-line no-async-promise-executor
318
+ return await new Promise(async (resolve, reject)=>{
825
319
  const payload = {
826
320
  url,
827
321
  method,
828
- options,
322
+ requestBodyOptions: options,
829
323
  retryCount: 0,
830
324
  retryRequest: async function(payload) {
831
- rest.processRequest(payload);
325
+ await rest.processRequest(payload);
832
326
  },
833
327
  resolve: (data)=>{
834
328
  resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
835
329
  },
836
- reject
330
+ reject,
331
+ runThroughQueue: options?.runThroughQueue
837
332
  };
838
- rest.processRequest(payload);
333
+ await rest.processRequest(payload);
839
334
  });
840
335
  },
841
- async get (url, body) {
842
- return camelize(await rest.makeRequest('GET', url, {
843
- body
844
- }));
336
+ async get (url, options) {
337
+ return camelize(await rest.makeRequest('GET', url, options));
845
338
  },
846
- async post (url, body) {
847
- return camelize(await rest.makeRequest('POST', url, {
848
- body
849
- }));
339
+ async post (url, options) {
340
+ return camelize(await rest.makeRequest('POST', url, options));
850
341
  },
851
- async delete (url, body) {
852
- camelize(await rest.makeRequest('DELETE', url, {
853
- body
854
- }));
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, {
858
- body
859
- }));
345
+ async patch (url, options) {
346
+ return camelize(await rest.makeRequest('PATCH', url, options));
860
347
  },
861
- async put (url, body) {
862
- return camelize(await rest.makeRequest('PUT', url, {
863
- body
864
- }));
348
+ async put (url, options) {
349
+ return camelize(await rest.makeRequest('PUT', url, options));
865
350
  },
866
351
  async addReaction (channelId, messageId, reaction) {
867
352
  reaction = processReactionString(reaction);
@@ -886,19 +371,22 @@ export function createRestManager(options) {
886
371
  async addThreadMember (channelId, userId) {
887
372
  await rest.put(rest.routes.channels.threads.user(channelId, userId));
888
373
  },
889
- async createAutomodRule (guildId, body) {
374
+ async createAutomodRule (guildId, body, reason) {
890
375
  return await rest.post(rest.routes.guilds.automod.rules(guildId), {
891
- body
376
+ body,
377
+ reason
892
378
  });
893
379
  },
894
- async createChannel (guildId, body) {
380
+ async createChannel (guildId, body, reason) {
895
381
  return await rest.post(rest.routes.guilds.channels(guildId), {
896
- body
382
+ body,
383
+ reason
897
384
  });
898
385
  },
899
- async createEmoji (guildId, body) {
386
+ async createEmoji (guildId, body, reason) {
900
387
  return await rest.post(rest.routes.guilds.emojis(guildId), {
901
- body
388
+ body,
389
+ reason
902
390
  });
903
391
  },
904
392
  async createGlobalApplicationCommand (body) {
@@ -924,14 +412,15 @@ export function createRestManager(options) {
924
412
  body
925
413
  });
926
414
  },
927
- async createGuildSticker (guildId, options) {
415
+ async createGuildSticker (guildId, options, reason) {
928
416
  const form = new FormData();
929
417
  form.append('file', options.file.blob, options.file.name);
930
418
  form.append('name', options.name);
931
419
  form.append('description', options.description);
932
420
  form.append('tags', options.tags);
933
421
  return await rest.post(rest.routes.guilds.stickers(guildId), {
934
- body: form
422
+ body: form,
423
+ reason
935
424
  });
936
425
  },
937
426
  async createGuildTemplate (guildId, body) {
@@ -939,14 +428,17 @@ export function createRestManager(options) {
939
428
  body
940
429
  });
941
430
  },
942
- async createForumThread (channelId, body) {
431
+ async createForumThread (channelId, body, reason) {
943
432
  return await rest.post(rest.routes.channels.forum(channelId), {
944
- body
433
+ body,
434
+ files: body.files,
435
+ reason
945
436
  });
946
437
  },
947
- async createInvite (channelId, body = {}) {
438
+ async createInvite (channelId, body = {}, reason) {
948
439
  return await rest.post(rest.routes.channels.invites(channelId), {
949
- body
440
+ body,
441
+ reason
950
442
  });
951
443
  },
952
444
  async createRole (guildId, body, reason) {
@@ -955,14 +447,16 @@ export function createRestManager(options) {
955
447
  reason
956
448
  });
957
449
  },
958
- async createScheduledEvent (guildId, body) {
450
+ async createScheduledEvent (guildId, body, reason) {
959
451
  return await rest.post(rest.routes.guilds.events.events(guildId), {
960
- body
452
+ body,
453
+ reason
961
454
  });
962
455
  },
963
- async createStageInstance (body) {
456
+ async createStageInstance (body, reason) {
964
457
  return await rest.post(rest.routes.channels.stages(), {
965
- body
458
+ body,
459
+ reason
966
460
  });
967
461
  },
968
462
  async createWebhook (channelId, options, reason) {
@@ -995,7 +489,9 @@ export function createRestManager(options) {
995
489
  });
996
490
  },
997
491
  async deleteFollowupMessage (token, messageId) {
998
- 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
+ });
999
495
  },
1000
496
  async deleteGlobalApplicationCommand (commandId) {
1001
497
  await rest.delete(rest.routes.interactions.commands.command(rest.applicationId, commandId));
@@ -1014,8 +510,10 @@ export function createRestManager(options) {
1014
510
  async deleteGuildTemplate (guildId, templateCode) {
1015
511
  await rest.delete(rest.routes.guilds.templates.guild(guildId, templateCode));
1016
512
  },
1017
- async deleteIntegration (guildId, integrationId) {
1018
- 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
+ });
1019
517
  },
1020
518
  async deleteInvite (inviteCode, reason) {
1021
519
  await rest.delete(rest.routes.guilds.invite(inviteCode), {
@@ -1036,7 +534,9 @@ export function createRestManager(options) {
1036
534
  });
1037
535
  },
1038
536
  async deleteOriginalInteractionResponse (token) {
1039
- 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
+ });
1040
540
  },
1041
541
  async deleteOwnReaction (channelId, messageId, reaction) {
1042
542
  reaction = processReactionString(reaction);
@@ -1049,8 +549,10 @@ export function createRestManager(options) {
1049
549
  reaction = processReactionString(reaction);
1050
550
  await rest.delete(rest.routes.channels.reactions.emoji(channelId, messageId, reaction));
1051
551
  },
1052
- async deleteRole (guildId, roleId) {
1053
- 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
+ });
1054
556
  },
1055
557
  async deleteScheduledEvent (guildId, eventId) {
1056
558
  await rest.delete(rest.routes.guilds.events.event(guildId, eventId));
@@ -1085,9 +587,10 @@ export function createRestManager(options) {
1085
587
  }
1086
588
  });
1087
589
  },
1088
- async editAutomodRule (guildId, ruleId, body) {
590
+ async editAutomodRule (guildId, ruleId, body, reason) {
1089
591
  return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), {
1090
- body
592
+ body,
593
+ reason
1091
594
  });
1092
595
  },
1093
596
  async editBotProfile (options) {
@@ -1099,14 +602,16 @@ export function createRestManager(options) {
1099
602
  }
1100
603
  });
1101
604
  },
1102
- async editChannel (channelId, body) {
605
+ async editChannel (channelId, body, reason) {
1103
606
  return await rest.patch(rest.routes.channels.channel(channelId), {
1104
- body
607
+ body,
608
+ reason
1105
609
  });
1106
610
  },
1107
- async editChannelPermissionOverrides (channelId, body) {
611
+ async editChannelPermissionOverrides (channelId, body, reason) {
1108
612
  await rest.put(rest.routes.channels.overwrite(channelId, body.id), {
1109
- body
613
+ body,
614
+ reason
1110
615
  });
1111
616
  },
1112
617
  async editChannelPositions (guildId, body) {
@@ -1114,14 +619,17 @@ export function createRestManager(options) {
1114
619
  body
1115
620
  });
1116
621
  },
1117
- async editEmoji (guildId, id, body) {
622
+ async editEmoji (guildId, id, body, reason) {
1118
623
  return await rest.patch(rest.routes.guilds.emoji(guildId, id), {
1119
- body
624
+ body,
625
+ reason
1120
626
  });
1121
627
  },
1122
628
  async editFollowupMessage (token, messageId, body) {
1123
629
  return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
1124
- body
630
+ body,
631
+ files: body.files,
632
+ unauthorized: true
1125
633
  });
1126
634
  },
1127
635
  async editGlobalApplicationCommand (commandId, body) {
@@ -1129,9 +637,10 @@ export function createRestManager(options) {
1129
637
  body
1130
638
  });
1131
639
  },
1132
- async editGuild (guildId, body) {
640
+ async editGuild (guildId, body, reason) {
1133
641
  return await rest.patch(rest.routes.guilds.guild(guildId), {
1134
- body
642
+ body,
643
+ reason
1135
644
  });
1136
645
  },
1137
646
  async editGuildApplicationCommand (commandId, guildId, body) {
@@ -1147,9 +656,10 @@ export function createRestManager(options) {
1147
656
  reason
1148
657
  });
1149
658
  },
1150
- async editGuildSticker (guildId, stickerId, body) {
659
+ async editGuildSticker (guildId, stickerId, body, reason) {
1151
660
  return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), {
1152
- body
661
+ body,
662
+ reason
1153
663
  });
1154
664
  },
1155
665
  async editGuildTemplate (guildId, templateCode, body) {
@@ -1164,7 +674,8 @@ export function createRestManager(options) {
1164
674
  },
1165
675
  async editOriginalInteractionResponse (token, body) {
1166
676
  return await rest.patch(rest.routes.interactions.responses.original(rest.applicationId, token), {
1167
- body
677
+ body,
678
+ files: body.files
1168
679
  });
1169
680
  },
1170
681
  async editOriginalWebhookMessage (webhookId, token, options) {
@@ -1172,7 +683,9 @@ export function createRestManager(options) {
1172
683
  body: {
1173
684
  type: InteractionResponseTypes.UpdateMessage,
1174
685
  data: options
1175
- }
686
+ },
687
+ files: options.files,
688
+ unauthorized: true
1176
689
  });
1177
690
  },
1178
691
  async editOwnVoiceState (guildId, options) {
@@ -1183,19 +696,22 @@ export function createRestManager(options) {
1183
696
  }
1184
697
  });
1185
698
  },
1186
- async editScheduledEvent (guildId, eventId, body) {
699
+ async editScheduledEvent (guildId, eventId, body, reason) {
1187
700
  return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), {
1188
- body
701
+ body,
702
+ reason
1189
703
  });
1190
704
  },
1191
- async editRole (guildId, roleId, body) {
705
+ async editRole (guildId, roleId, body, reason) {
1192
706
  return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), {
1193
- body
707
+ body,
708
+ reason
1194
709
  });
1195
710
  },
1196
- async editRolePositions (guildId, body) {
711
+ async editRolePositions (guildId, body, reason) {
1197
712
  return await rest.patch(rest.routes.guilds.roles.all(guildId), {
1198
- body
713
+ body,
714
+ reason
1199
715
  });
1200
716
  },
1201
717
  async editStageInstance (channelId, topic, reason) {
@@ -1211,14 +727,16 @@ export function createRestManager(options) {
1211
727
  body: options
1212
728
  });
1213
729
  },
1214
- async editWebhook (webhookId, body) {
730
+ async editWebhook (webhookId, body, reason) {
1215
731
  return await rest.patch(rest.routes.webhooks.id(webhookId), {
1216
- body
732
+ body,
733
+ reason
1217
734
  });
1218
735
  },
1219
736
  async editWebhookMessage (webhookId, token, messageId, options) {
1220
737
  return await rest.patch(rest.routes.webhooks.message(webhookId, token, messageId, options), {
1221
- body: options
738
+ body: options,
739
+ files: options.files
1222
740
  });
1223
741
  },
1224
742
  async editWebhookWithToken (webhookId, token, body) {
@@ -1226,14 +744,16 @@ export function createRestManager(options) {
1226
744
  body
1227
745
  });
1228
746
  },
1229
- async editWelcomeScreen (guildId, body) {
747
+ async editWelcomeScreen (guildId, body, reason) {
1230
748
  return await rest.patch(rest.routes.guilds.welcome(guildId), {
1231
- body
749
+ body,
750
+ reason
1232
751
  });
1233
752
  },
1234
- async editWidgetSettings (guildId, body) {
753
+ async editWidgetSettings (guildId, body, reason) {
1235
754
  return await rest.patch(rest.routes.guilds.widget(guildId), {
1236
- body
755
+ body,
756
+ reason
1237
757
  });
1238
758
  },
1239
759
  async executeWebhook (webhookId, token, options) {
@@ -1304,7 +824,9 @@ export function createRestManager(options) {
1304
824
  return await rest.get(rest.routes.guilds.emojis(guildId));
1305
825
  },
1306
826
  async getFollowupMessage (token, messageId) {
1307
- 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
+ });
1308
830
  },
1309
831
  async getGatewayBot () {
1310
832
  return await rest.get(rest.routes.gatewayBot());
@@ -1357,7 +879,9 @@ export function createRestManager(options) {
1357
879
  return await rest.get(rest.routes.nitroStickerPacks());
1358
880
  },
1359
881
  async getOriginalInteractionResponse (token) {
1360
- 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
+ });
1361
885
  },
1362
886
  async getPinnedMessages (channelId) {
1363
887
  return await rest.get(rest.routes.channels.pins(channelId));
@@ -1457,79 +981,58 @@ export function createRestManager(options) {
1457
981
  async removeThreadMember (channelId, userId) {
1458
982
  await rest.delete(rest.routes.channels.threads.user(channelId, userId));
1459
983
  },
1460
- // TODO: why that
1461
984
  async sendFollowupMessage (token, options) {
1462
- return await new Promise((resolve, reject)=>{
1463
- rest.sendRequest({
1464
- url: rest.routes.webhooks.webhook(rest.applicationId, token),
1465
- method: 'POST',
1466
- options: {
1467
- body: options
1468
- },
1469
- retryCount: 0,
1470
- retryRequest: async function(options) {
1471
- // TODO: should change to reprocess queue item
1472
- await rest.sendRequest(options);
1473
- },
1474
- resolve: (data)=>{
1475
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1476
- },
1477
- reject
1478
- });
985
+ return await rest.post(rest.routes.webhooks.webhook(rest.applicationId, token), {
986
+ body: options,
987
+ files: options.files,
988
+ unauthorized: true
1479
989
  });
1480
990
  },
1481
- // TODO: why that
1482
991
  async sendInteractionResponse (interactionId, token, options) {
1483
- await new Promise((resolve, reject)=>{
1484
- rest.sendRequest({
1485
- url: rest.routes.interactions.responses.callback(interactionId, token),
1486
- method: 'POST',
1487
- options: {
1488
- body: options
1489
- },
1490
- retryCount: 0,
1491
- retryRequest: async function(options) {
1492
- // TODO: should change to reprocess queue item
1493
- await rest.sendRequest(options);
1494
- },
1495
- resolve: (data)=>{
1496
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1497
- },
1498
- reject
1499
- });
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
1500
997
  });
1501
998
  },
1502
999
  async sendMessage (channelId, body) {
1503
1000
  return await rest.post(rest.routes.channels.messages(channelId), {
1504
- body
1001
+ body,
1002
+ files: body.files
1505
1003
  });
1506
1004
  },
1507
- async startThreadWithMessage (channelId, messageId, body) {
1005
+ async startThreadWithMessage (channelId, messageId, body, reason) {
1508
1006
  return await rest.post(rest.routes.channels.threads.message(channelId, messageId), {
1509
- body
1007
+ body,
1008
+ reason
1510
1009
  });
1511
1010
  },
1512
- async startThreadWithoutMessage (channelId, body) {
1011
+ async startThreadWithoutMessage (channelId, body, reason) {
1513
1012
  return await rest.post(rest.routes.channels.threads.all(channelId), {
1514
- body
1013
+ body,
1014
+ reason
1515
1015
  });
1516
1016
  },
1517
1017
  async syncGuildTemplate (guildId) {
1518
1018
  return await rest.put(rest.routes.guilds.templates.all(guildId));
1519
1019
  },
1520
- async banMember (guildId, userId, body) {
1020
+ async banMember (guildId, userId, body, reason) {
1521
1021
  await rest.put(rest.routes.guilds.members.ban(guildId, userId), {
1522
- body
1022
+ body,
1023
+ reason
1523
1024
  });
1524
1025
  },
1525
- async editBotMember (guildId, body) {
1026
+ async editBotMember (guildId, body, reason) {
1526
1027
  return await rest.patch(rest.routes.guilds.members.bot(guildId), {
1527
- body
1028
+ body,
1029
+ reason
1528
1030
  });
1529
1031
  },
1530
- async editMember (guildId, userId, body) {
1032
+ async editMember (guildId, userId, body, reason) {
1531
1033
  return await rest.patch(rest.routes.guilds.members.member(guildId, userId), {
1532
- body
1034
+ body,
1035
+ reason
1533
1036
  });
1534
1037
  },
1535
1038
  async getMember (guildId, userId) {
@@ -1548,16 +1051,19 @@ export function createRestManager(options) {
1548
1051
  reason
1549
1052
  });
1550
1053
  },
1551
- async pruneMembers (guildId, body) {
1054
+ async pruneMembers (guildId, body, reason) {
1552
1055
  return await rest.post(rest.routes.guilds.members.prune(guildId), {
1553
- body
1056
+ body,
1057
+ reason
1554
1058
  });
1555
1059
  },
1556
1060
  async searchMembers (guildId, query, options) {
1557
1061
  return await rest.get(rest.routes.guilds.members.search(guildId, query, options));
1558
1062
  },
1559
- async unbanMember (guildId, userId) {
1560
- 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
+ });
1561
1067
  },
1562
1068
  async unpinMessage (channelId, messageId, reason) {
1563
1069
  await rest.delete(rest.routes.channels.pin(channelId, messageId), {