@discordeno/rest 19.0.0-next.fd518cb → 19.0.0-next.fda3003

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.
Files changed (51) hide show
  1. package/dist/cjs/index.cjs +25 -0
  2. package/dist/cjs/invalidBucket.cjs +87 -0
  3. package/dist/cjs/manager.cjs +1446 -0
  4. package/dist/cjs/queue.cjs +166 -0
  5. package/dist/cjs/routes.cjs +581 -0
  6. package/dist/cjs/types.cjs +6 -0
  7. package/dist/cjs/typings/routes.cjs +6 -0
  8. package/dist/esm/index.js +8 -0
  9. package/dist/esm/invalidBucket.js +83 -0
  10. package/dist/esm/manager.js +1404 -0
  11. package/dist/esm/queue.js +156 -0
  12. package/dist/esm/routes.js +571 -0
  13. package/dist/esm/types.js +3 -0
  14. package/dist/esm/typings/routes.js +3 -0
  15. package/dist/tsconfig.tsbuildinfo +1 -0
  16. package/dist/types/index.d.ts.map +1 -0
  17. package/dist/{invalidBucket.d.ts → types/invalidBucket.d.ts} +5 -0
  18. package/dist/types/invalidBucket.d.ts.map +1 -0
  19. package/dist/types/manager.d.ts.map +1 -0
  20. package/dist/{queue.d.ts → types/queue.d.ts} +12 -0
  21. package/dist/types/queue.d.ts.map +1 -0
  22. package/dist/types/routes.d.ts.map +1 -0
  23. package/dist/{types.d.ts → types/types.d.ts} +446 -110
  24. package/dist/types/types.d.ts.map +1 -0
  25. package/dist/{typings → types/typings}/routes.d.ts +46 -5
  26. package/dist/types/typings/routes.d.ts.map +1 -0
  27. package/package.json +29 -23
  28. package/dist/index.d.ts.map +0 -1
  29. package/dist/index.js +0 -8
  30. package/dist/index.js.map +0 -1
  31. package/dist/invalidBucket.d.ts.map +0 -1
  32. package/dist/invalidBucket.js +0 -82
  33. package/dist/invalidBucket.js.map +0 -1
  34. package/dist/manager.d.ts.map +0 -1
  35. package/dist/manager.js +0 -1102
  36. package/dist/manager.js.map +0 -1
  37. package/dist/queue.d.ts.map +0 -1
  38. package/dist/queue.js +0 -153
  39. package/dist/queue.js.map +0 -1
  40. package/dist/routes.d.ts.map +0 -1
  41. package/dist/routes.js +0 -487
  42. package/dist/routes.js.map +0 -1
  43. package/dist/types.d.ts.map +0 -1
  44. package/dist/types.js +0 -3
  45. package/dist/types.js.map +0 -1
  46. package/dist/typings/routes.d.ts.map +0 -1
  47. package/dist/typings/routes.js +0 -3
  48. package/dist/typings/routes.js.map +0 -1
  49. /package/dist/{index.d.ts → types/index.d.ts} +0 -0
  50. /package/dist/{manager.d.ts → types/manager.d.ts} +0 -0
  51. /package/dist/{routes.d.ts → types/routes.d.ts} +0 -0
package/dist/manager.js DELETED
@@ -1,1102 +0,0 @@
1
- /* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable no-const-assign */ import { InteractionResponseTypes } from '@discordeno/types';
2
- import { calculateBits, camelize, camelToSnakeCase, delay, getBotIdFromToken, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
3
- import { createInvalidRequestBucket } from './invalidBucket.js';
4
- import { Queue } from './queue.js';
5
- import { createRoutes } from './routes.js';
6
- // TODO: make dynamic based on package.json file
7
- const version = '19.0.0-alpha.1';
8
- export const DISCORD_API_VERSION = 10;
9
- export const DISCORD_API_URL = 'https://discord.com/api';
10
- export const AUDIT_LOG_REASON_HEADER = 'x-audit-log-reason';
11
- export const RATE_LIMIT_REMAINING_HEADER = 'x-ratelimit-remaining';
12
- export const RATE_LIMIT_RESET_AFTER_HEADER = 'x-ratelimit-reset-after';
13
- export const RATE_LIMIT_GLOBAL_HEADER = 'x-ratelimit-global';
14
- export const RATE_LIMIT_BUCKET_HEADER = 'x-ratelimit-bucket';
15
- export const RATE_LIMIT_LIMIT_HEADER = 'x-ratelimit-limit';
16
- export const RATE_LIMIT_SCOPE_HEADER = 'x-ratelimit-scope';
17
- export function createRestManager(options) {
18
- const applicationId = options.applicationId ? BigInt(options.applicationId) : options.token ? getBotIdFromToken(options.token) : undefined;
19
- if (!applicationId) {
20
- 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.');
21
- }
22
- const baseUrl = options.proxy?.baseUrl ?? DISCORD_API_URL;
23
- const rest = {
24
- applicationId,
25
- authorization: options.proxy?.authorization,
26
- baseUrl,
27
- deleteQueueDelay: 60000,
28
- globallyRateLimited: false,
29
- invalidBucket: createInvalidRequestBucket({}),
30
- isProxied: !baseUrl.startsWith(DISCORD_API_URL),
31
- maxRetryCount: Infinity,
32
- processingRateLimitedPaths: false,
33
- queues: new Map(),
34
- rateLimitedPaths: new Map(),
35
- token: options.token,
36
- version: options.version ?? DISCORD_API_VERSION,
37
- routes: createRoutes(),
38
- checkRateLimits (url) {
39
- const ratelimited = rest.rateLimitedPaths.get(url);
40
- const global = rest.rateLimitedPaths.get('global');
41
- const now = Date.now();
42
- if (ratelimited && now < ratelimited.resetTimestamp) {
43
- return ratelimited.resetTimestamp - now;
44
- }
45
- if (global && now < global.resetTimestamp) {
46
- return global.resetTimestamp - now;
47
- }
48
- return false;
49
- },
50
- changeToDiscordFormat (obj) {
51
- if (obj === null) return null;
52
- if (typeof obj === 'object') {
53
- if (Array.isArray(obj)) {
54
- return obj.map((item)=>rest.changeToDiscordFormat(item));
55
- }
56
- const newObj = {};
57
- for (const key of Object.keys(obj)){
58
- // Keys that dont require snake casing
59
- if ([
60
- 'permissions',
61
- 'allow',
62
- 'deny'
63
- ].includes(key) && obj[key] !== undefined) {
64
- newObj[key] = calculateBits(obj[key]);
65
- continue;
66
- }
67
- if (key === 'defaultMemberPermissions' && obj[key] !== undefined) {
68
- newObj.default_member_permissions = calculateBits(obj[key]);
69
- continue;
70
- }
71
- newObj[camelToSnakeCase(key)] = rest.changeToDiscordFormat(obj[key]);
72
- }
73
- return newObj;
74
- }
75
- if (typeof obj === 'bigint') return obj.toString();
76
- return obj;
77
- },
78
- createRequestBody (method, options) {
79
- const headers = {
80
- 'user-agent': `DiscordBot (https://github.com/discordeno/discordeno, v${version})`
81
- };
82
- if (options?.unauthorized !== false) headers.authorization = `Bot ${rest.token}`;
83
- // IF A REASON IS PROVIDED ENCODE IT IN HEADERS
84
- if (options?.reason !== undefined) {
85
- headers[AUDIT_LOG_REASON_HEADER] = encodeURIComponent(options?.reason);
86
- }
87
- let body;
88
- // TODO: check if we need to add specific check for GET method
89
- // Since GET does not allow bodies
90
- // Have to check for attachments first, since body then has to be send in a different way.
91
- if (options?.files !== undefined) {
92
- const form = new FormData();
93
- for(let i = 0; i < options.files.length; ++i){
94
- form.append(`file${i}`, options.files[i].blob, options.files[i].name);
95
- }
96
- form.append('payload_json', JSON.stringify({
97
- ...options.body,
98
- files: undefined
99
- }));
100
- body = form;
101
- // No need to set the `content-type` header since `fetch` does that automatically for us when we use a `FormData` object.
102
- } else if (options?.body !== undefined) {
103
- if (options.body instanceof FormData) {
104
- body = options.body;
105
- // No need to set the `content-type` header since `fetch` does that automatically for us when we use a `FormData` object.
106
- } else {
107
- body = JSON.stringify(rest.changeToDiscordFormat(options.body));
108
- headers['content-type'] = `application/json`;
109
- }
110
- }
111
- // SOMETIMES SPECIAL HEADERS (E.G. CUSTOM AUTHORIZATION) NEED TO BE USED
112
- if (options?.headers) {
113
- Object.assign(headers, options.headers);
114
- }
115
- return {
116
- body,
117
- headers,
118
- method
119
- };
120
- },
121
- processRateLimitedPaths () {
122
- const now = Date.now();
123
- for (const [key, value] of rest.rateLimitedPaths.entries()){
124
- // rest.debug(
125
- // `[REST - processRateLimitedPaths] Running for of loop. ${
126
- // value.resetTimestamp - now
127
- // }`
128
- // )
129
- // If the time has not reached cancel
130
- if (value.resetTimestamp > now) continue;
131
- // Rate limit is over, delete the rate limiter
132
- rest.rateLimitedPaths.delete(key);
133
- // If it was global, also mark the global value as false
134
- if (key === 'global') rest.globallyRateLimited = false;
135
- }
136
- // ALL PATHS ARE CLEARED CAN CANCEL OUT!
137
- if (rest.rateLimitedPaths.size === 0) {
138
- rest.processingRateLimitedPaths = false;
139
- } else {
140
- rest.processingRateLimitedPaths = true;
141
- // RECHECK IN 1 SECOND
142
- setTimeout(()=>{
143
- // rest.debug('[REST - processRateLimitedPaths] Running setTimeout.')
144
- rest.processRateLimitedPaths();
145
- }, 1000);
146
- }
147
- },
148
- /** Processes the rate limit headers and determines if it needs to be rate limited and returns the bucket id if available */ processHeaders (url, headers) {
149
- let rateLimited = false;
150
- // GET ALL NECESSARY HEADERS
151
- const remaining = headers.get(RATE_LIMIT_REMAINING_HEADER);
152
- const retryAfter = headers.get(RATE_LIMIT_RESET_AFTER_HEADER);
153
- const reset = Date.now() + Number(retryAfter) * 1000;
154
- const global = headers.get(RATE_LIMIT_GLOBAL_HEADER);
155
- // undefined override null needed for typings
156
- const bucketId = headers.get(RATE_LIMIT_BUCKET_HEADER) ?? undefined;
157
- const limit = headers.get(RATE_LIMIT_LIMIT_HEADER);
158
- rest.queues.get(url)?.handleCompletedRequest({
159
- remaining: remaining ? Number(remaining) : undefined,
160
- interval: retryAfter ? Number(retryAfter) * 1000 : undefined,
161
- max: limit ? Number(limit) : undefined
162
- });
163
- // IF THERE IS NO REMAINING RATE LIMIT, MARK IT AS RATE LIMITED
164
- if (remaining === '0') {
165
- rateLimited = true;
166
- // SAVE THE URL AS LIMITED, IMPORTANT FOR NEW REQUESTS BY USER WITHOUT BUCKET
167
- rest.rateLimitedPaths.set(url, {
168
- url,
169
- resetTimestamp: reset,
170
- bucketId
171
- });
172
- // SAVE THE BUCKET AS LIMITED SINCE DIFFERENT URLS MAY SHARE A BUCKET
173
- if (bucketId) {
174
- rest.rateLimitedPaths.set(bucketId, {
175
- url,
176
- resetTimestamp: reset,
177
- bucketId
178
- });
179
- }
180
- }
181
- // IF THERE IS NO REMAINING GLOBAL LIMIT, MARK IT RATE LIMITED GLOBALLY
182
- if (global) {
183
- const retryAfter = headers.get('retry-after');
184
- const globalReset = Date.now() + Number(retryAfter) * 1000;
185
- // rest.debug(
186
- // `[REST = Globally Rate Limited] URL: ${url} | Global Rest: ${globalReset}`
187
- // )
188
- rest.globallyRateLimited = true;
189
- rateLimited = true;
190
- setTimeout(()=>{
191
- rest.globallyRateLimited = false;
192
- }, globalReset);
193
- rest.rateLimitedPaths.set('global', {
194
- url: 'global',
195
- resetTimestamp: globalReset,
196
- bucketId
197
- });
198
- if (bucketId) {
199
- rest.rateLimitedPaths.set(bucketId, {
200
- url: 'global',
201
- resetTimestamp: globalReset,
202
- bucketId
203
- });
204
- }
205
- }
206
- if (rateLimited && !rest.processingRateLimitedPaths) {
207
- rest.processRateLimitedPaths();
208
- }
209
- return rateLimited ? bucketId : undefined;
210
- },
211
- async sendRequest (options) {
212
- const url = `${rest.baseUrl}/v${rest.version}${options.route}`;
213
- const payload = rest.createRequestBody(options.method, options.requestBodyOptions);
214
- logger.debug(`sending request to ${url}`, 'with payload:', {
215
- ...payload,
216
- headers: {
217
- ...payload.headers,
218
- authorization: 'Bot tokenhere'
219
- }
220
- });
221
- const response = await fetch(url, payload);
222
- logger.debug(`request fetched from ${url} with status ${response.status} & ${response.statusText}`);
223
- // Set the bucket id if it was available on the headers
224
- const bucketId = rest.processHeaders(rest.simplifyUrl(options.route, options.method), response.headers);
225
- if (bucketId) options.bucketId = bucketId;
226
- if (response.status < 200 || response.status >= 400) {
227
- logger.debug(`Request to ${url} failed.`);
228
- if (response.status !== 429) {
229
- options.reject({
230
- ok: false,
231
- status: response.status,
232
- body: await response.text()
233
- });
234
- return;
235
- }
236
- logger.debug(`Request to ${url} was ratelimited.`);
237
- // Too many attempts, get rid of request from queue.
238
- if (options.retryCount >= rest.maxRetryCount) {
239
- logger.debug(`Request to ${url} exceeded the maximum allowed retries.`, 'with payload:', payload);
240
- // rest.debug(`[REST - RetriesMaxed] ${JSON.stringify(options)}`)
241
- options.reject({
242
- ok: false,
243
- status: response.status,
244
- error: 'The request was rate limited and it maxed out the retries limit.'
245
- });
246
- return;
247
- }
248
- options.retryCount += 1;
249
- // Rate limited, add back to queue
250
- rest.invalidBucket.handleCompletedRequest(response.status, response.headers.get(RATE_LIMIT_SCOPE_HEADER) === 'shared');
251
- const resetAfter = response.headers.get(RATE_LIMIT_RESET_AFTER_HEADER);
252
- if (resetAfter) await delay(Number(resetAfter) * 1000);
253
- // process the response to prevent mem leak
254
- await response.arrayBuffer();
255
- return await options.retryRequest?.(options);
256
- }
257
- // Discord sometimes sends no response with no content.
258
- options.resolve({
259
- ok: true,
260
- status: response.status,
261
- body: response.status === 204 ? undefined : await response.text()
262
- });
263
- },
264
- simplifyUrl (url, method) {
265
- const parts = url.split('/');
266
- const secondLastPart = parts[parts.length - 2];
267
- if (secondLastPart === 'channels' || secondLastPart === 'guilds') {
268
- return url;
269
- }
270
- if (secondLastPart === 'reactions' || parts[parts.length - 1] === '@me') {
271
- parts.splice(-2);
272
- parts.push('reactions');
273
- } else {
274
- parts.splice(-1);
275
- parts.push('x');
276
- }
277
- if (parts[parts.length - 3] === 'reactions') {
278
- parts.splice(-2);
279
- }
280
- if (method === 'DELETE' && secondLastPart === 'messages') {
281
- return `D${parts.join('/')}`;
282
- }
283
- return parts.join('/');
284
- },
285
- async processRequest (request) {
286
- const url = rest.simplifyUrl(request.route, request.method);
287
- if (request.runThroughQueue === false) {
288
- await rest.sendRequest(request);
289
- return;
290
- }
291
- const queue = rest.queues.get(url);
292
- if (queue !== undefined) {
293
- queue.makeRequest(request);
294
- } else {
295
- // CREATES A NEW QUEUE
296
- const bucketQueue = new Queue(rest, {
297
- url,
298
- deleteQueueDelay: rest.deleteQueueDelay
299
- });
300
- // Add request to queue
301
- bucketQueue.makeRequest(request);
302
- // Save queue
303
- rest.queues.set(url, bucketQueue);
304
- }
305
- },
306
- async makeRequest (method, route, options) {
307
- if (rest.isProxied) {
308
- if (rest.authorization !== undefined) {
309
- options ??= {};
310
- options.headers ??= {};
311
- options.headers.authorization = rest.authorization;
312
- }
313
- const result = await fetch(`${rest.baseUrl}/v${rest.version}${route}`, rest.createRequestBody(method, options));
314
- if (!result.ok) {
315
- const err = await result.json().catch(()=>{});
316
- // Legacy Handling to not break old code or when body is missing
317
- if (!err?.body) throw new Error(`Error: ${err.message ?? result.statusText}`);
318
- throw new Error(JSON.stringify(err));
319
- }
320
- return result.status !== 204 ? await result.json() : undefined;
321
- }
322
- // eslint-disable-next-line no-async-promise-executor
323
- return await new Promise(async (resolve, reject)=>{
324
- const payload = {
325
- route,
326
- method,
327
- requestBodyOptions: options,
328
- retryCount: 0,
329
- retryRequest: async function(payload) {
330
- await rest.processRequest(payload);
331
- },
332
- resolve: (data)=>{
333
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
334
- },
335
- reject,
336
- runThroughQueue: options?.runThroughQueue
337
- };
338
- await rest.processRequest(payload);
339
- });
340
- },
341
- async get (url, options) {
342
- return camelize(await rest.makeRequest('GET', url, options));
343
- },
344
- async post (url, options) {
345
- return camelize(await rest.makeRequest('POST', url, options));
346
- },
347
- async delete (url, options) {
348
- camelize(await rest.makeRequest('DELETE', url, options));
349
- },
350
- async patch (url, options) {
351
- return camelize(await rest.makeRequest('PATCH', url, options));
352
- },
353
- async put (url, options) {
354
- return camelize(await rest.makeRequest('PUT', url, options));
355
- },
356
- async addReaction (channelId, messageId, reaction) {
357
- reaction = processReactionString(reaction);
358
- await rest.put(rest.routes.channels.reactions.bot(channelId, messageId, reaction));
359
- },
360
- async addReactions (channelId, messageId, reactions, ordered = false) {
361
- if (!ordered) {
362
- await Promise.all(reactions.map(async (reaction)=>{
363
- await rest.addReaction(channelId, messageId, reaction);
364
- }));
365
- return;
366
- }
367
- for (const reaction of reactions){
368
- await rest.addReaction(channelId, messageId, reaction);
369
- }
370
- },
371
- async addRole (guildId, userId, roleId, reason) {
372
- await rest.put(rest.routes.guilds.roles.member(guildId, userId, roleId), {
373
- reason
374
- });
375
- },
376
- async addThreadMember (channelId, userId) {
377
- await rest.put(rest.routes.channels.threads.user(channelId, userId));
378
- },
379
- async createAutomodRule (guildId, body, reason) {
380
- return await rest.post(rest.routes.guilds.automod.rules(guildId), {
381
- body,
382
- reason
383
- });
384
- },
385
- async createChannel (guildId, body, reason) {
386
- return await rest.post(rest.routes.guilds.channels(guildId), {
387
- body,
388
- reason
389
- });
390
- },
391
- async createEmoji (guildId, body, reason) {
392
- return await rest.post(rest.routes.guilds.emojis(guildId), {
393
- body,
394
- reason
395
- });
396
- },
397
- async createGlobalApplicationCommand (body) {
398
- return await rest.post(rest.routes.interactions.commands.commands(rest.applicationId), {
399
- body
400
- });
401
- },
402
- async createGuild (body) {
403
- return await rest.post(rest.routes.guilds.all(), {
404
- body
405
- });
406
- },
407
- async createGuildApplicationCommand (body, guildId) {
408
- return await rest.post(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
409
- body
410
- });
411
- },
412
- async createGuildFromTemplate (templateCode, body) {
413
- if (body.icon) {
414
- body.icon = await urlToBase64(body.icon);
415
- }
416
- return await rest.post(rest.routes.guilds.templates.code(templateCode), {
417
- body
418
- });
419
- },
420
- async createGuildSticker (guildId, options, reason) {
421
- const form = new FormData();
422
- form.append('file', options.file.blob, options.file.name);
423
- form.append('name', options.name);
424
- form.append('description', options.description);
425
- form.append('tags', options.tags);
426
- return await rest.post(rest.routes.guilds.stickers(guildId), {
427
- body: form,
428
- reason
429
- });
430
- },
431
- async createGuildTemplate (guildId, body) {
432
- return await rest.post(rest.routes.guilds.templates.all(guildId), {
433
- body
434
- });
435
- },
436
- async createForumThread (channelId, body, reason) {
437
- return await rest.post(rest.routes.channels.forum(channelId), {
438
- body,
439
- files: body.files,
440
- reason
441
- });
442
- },
443
- async createInvite (channelId, body = {}, reason) {
444
- return await rest.post(rest.routes.channels.invites(channelId), {
445
- body,
446
- reason
447
- });
448
- },
449
- async createRole (guildId, body, reason) {
450
- return await rest.post(rest.routes.guilds.roles.all(guildId), {
451
- body,
452
- reason
453
- });
454
- },
455
- async createScheduledEvent (guildId, body, reason) {
456
- return await rest.post(rest.routes.guilds.events.events(guildId), {
457
- body,
458
- reason
459
- });
460
- },
461
- async createStageInstance (body, reason) {
462
- return await rest.post(rest.routes.channels.stages(), {
463
- body,
464
- reason
465
- });
466
- },
467
- async createWebhook (channelId, options, reason) {
468
- return await rest.post(rest.routes.channels.webhooks(channelId), {
469
- body: {
470
- name: options.name,
471
- avatar: options.avatar ? await urlToBase64(options.avatar) : undefined
472
- },
473
- reason
474
- });
475
- },
476
- async deleteAutomodRule (guildId, ruleId, reason) {
477
- await rest.delete(rest.routes.guilds.automod.rule(guildId, ruleId), {
478
- reason
479
- });
480
- },
481
- async deleteChannel (channelId, reason) {
482
- await rest.delete(rest.routes.channels.channel(channelId), {
483
- reason
484
- });
485
- },
486
- async deleteChannelPermissionOverride (channelId, overwriteId, reason) {
487
- await rest.delete(rest.routes.channels.overwrite(channelId, overwriteId), {
488
- reason
489
- });
490
- },
491
- async deleteEmoji (guildId, id, reason) {
492
- await rest.delete(rest.routes.guilds.emoji(guildId, id), {
493
- reason
494
- });
495
- },
496
- async deleteFollowupMessage (token, messageId) {
497
- await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
498
- unauthorized: true
499
- });
500
- },
501
- async deleteGlobalApplicationCommand (commandId) {
502
- await rest.delete(rest.routes.interactions.commands.command(rest.applicationId, commandId));
503
- },
504
- async deleteGuild (guildId) {
505
- await rest.delete(rest.routes.guilds.guild(guildId));
506
- },
507
- async deleteGuildApplicationCommand (commandId, guildId) {
508
- await rest.delete(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId));
509
- },
510
- async deleteGuildSticker (guildId, stickerId, reason) {
511
- await rest.delete(rest.routes.guilds.sticker(guildId, stickerId), {
512
- reason
513
- });
514
- },
515
- async deleteGuildTemplate (guildId, templateCode) {
516
- await rest.delete(rest.routes.guilds.templates.guild(guildId, templateCode));
517
- },
518
- async deleteIntegration (guildId, integrationId, reason) {
519
- await rest.delete(rest.routes.guilds.integration(guildId, integrationId), {
520
- reason
521
- });
522
- },
523
- async deleteInvite (inviteCode, reason) {
524
- await rest.delete(rest.routes.guilds.invite(inviteCode), {
525
- reason
526
- });
527
- },
528
- async deleteMessage (channelId, messageId, reason) {
529
- await rest.delete(rest.routes.channels.message(channelId, messageId), {
530
- reason
531
- });
532
- },
533
- async deleteMessages (channelId, messageIds, reason) {
534
- await rest.post(rest.routes.channels.bulk(channelId), {
535
- body: {
536
- messages: messageIds.slice(0, 100).map((id)=>id.toString())
537
- },
538
- reason
539
- });
540
- },
541
- async deleteOriginalInteractionResponse (token) {
542
- await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token), {
543
- unauthorized: true
544
- });
545
- },
546
- async deleteOwnReaction (channelId, messageId, reaction) {
547
- reaction = processReactionString(reaction);
548
- await rest.delete(rest.routes.channels.reactions.bot(channelId, messageId, reaction));
549
- },
550
- async deleteReactionsAll (channelId, messageId) {
551
- await rest.delete(rest.routes.channels.reactions.all(channelId, messageId));
552
- },
553
- async deleteReactionsEmoji (channelId, messageId, reaction) {
554
- reaction = processReactionString(reaction);
555
- await rest.delete(rest.routes.channels.reactions.emoji(channelId, messageId, reaction));
556
- },
557
- async deleteRole (guildId, roleId, reason) {
558
- await rest.delete(rest.routes.guilds.roles.one(guildId, roleId), {
559
- reason
560
- });
561
- },
562
- async deleteScheduledEvent (guildId, eventId) {
563
- await rest.delete(rest.routes.guilds.events.event(guildId, eventId));
564
- },
565
- async deleteStageInstance (channelId, reason) {
566
- await rest.delete(rest.routes.channels.stage(channelId), {
567
- reason
568
- });
569
- },
570
- async deleteUserReaction (channelId, messageId, userId, reaction) {
571
- reaction = processReactionString(reaction);
572
- await rest.delete(rest.routes.channels.reactions.user(channelId, messageId, reaction, userId));
573
- },
574
- async deleteWebhook (webhookId, reason) {
575
- await rest.delete(rest.routes.webhooks.id(webhookId), {
576
- reason
577
- });
578
- },
579
- async deleteWebhookMessage (webhookId, token, messageId, options) {
580
- await rest.delete(rest.routes.webhooks.message(webhookId, token, messageId, options));
581
- },
582
- async deleteWebhookWithToken (webhookId, token) {
583
- await rest.delete(rest.routes.webhooks.webhook(webhookId, token));
584
- },
585
- async editApplicationCommandPermissions (guildId, commandId, bearerToken, permissions) {
586
- return await rest.put(rest.routes.interactions.commands.permission(rest.applicationId, guildId, commandId), {
587
- body: {
588
- permissions
589
- },
590
- headers: {
591
- authorization: `Bearer ${bearerToken}`
592
- }
593
- });
594
- },
595
- async editAutomodRule (guildId, ruleId, body, reason) {
596
- return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), {
597
- body,
598
- reason
599
- });
600
- },
601
- async editBotProfile (options) {
602
- const avatar = options?.botAvatarURL ? await urlToBase64(options?.botAvatarURL) : options?.botAvatarURL;
603
- return await rest.patch(rest.routes.userBot(), {
604
- body: {
605
- username: options.username?.trim(),
606
- avatar
607
- }
608
- });
609
- },
610
- async editChannel (channelId, body, reason) {
611
- return await rest.patch(rest.routes.channels.channel(channelId), {
612
- body,
613
- reason
614
- });
615
- },
616
- async editChannelPermissionOverrides (channelId, body, reason) {
617
- await rest.put(rest.routes.channels.overwrite(channelId, body.id), {
618
- body,
619
- reason
620
- });
621
- },
622
- async editChannelPositions (guildId, body) {
623
- await rest.patch(rest.routes.guilds.channels(guildId), {
624
- body
625
- });
626
- },
627
- async editEmoji (guildId, id, body, reason) {
628
- return await rest.patch(rest.routes.guilds.emoji(guildId, id), {
629
- body,
630
- reason
631
- });
632
- },
633
- async editFollowupMessage (token, messageId, body) {
634
- return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
635
- body,
636
- files: body.files,
637
- unauthorized: true
638
- });
639
- },
640
- async editGlobalApplicationCommand (commandId, body) {
641
- return await rest.patch(rest.routes.interactions.commands.command(rest.applicationId, commandId), {
642
- body
643
- });
644
- },
645
- async editGuild (guildId, body, reason) {
646
- return await rest.patch(rest.routes.guilds.guild(guildId), {
647
- body,
648
- reason
649
- });
650
- },
651
- async editGuildApplicationCommand (commandId, guildId, body) {
652
- return await rest.patch(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId), {
653
- body
654
- });
655
- },
656
- async editGuildMfaLevel (guildId, mfaLevel, reason) {
657
- await rest.post(rest.routes.guilds.mfa(guildId), {
658
- body: {
659
- level: mfaLevel
660
- },
661
- reason
662
- });
663
- },
664
- async editGuildSticker (guildId, stickerId, body, reason) {
665
- return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), {
666
- body,
667
- reason
668
- });
669
- },
670
- async editGuildTemplate (guildId, templateCode, body) {
671
- return await rest.patch(rest.routes.guilds.templates.guild(guildId, templateCode), {
672
- body
673
- });
674
- },
675
- async editMessage (channelId, messageId, body) {
676
- return await rest.patch(rest.routes.channels.message(channelId, messageId), {
677
- body
678
- });
679
- },
680
- async editOriginalInteractionResponse (token, body) {
681
- return await rest.patch(rest.routes.interactions.responses.original(rest.applicationId, token), {
682
- body,
683
- files: body.files
684
- });
685
- },
686
- async editOriginalWebhookMessage (webhookId, token, options) {
687
- return await rest.patch(rest.routes.webhooks.original(webhookId, token, options), {
688
- body: {
689
- type: InteractionResponseTypes.UpdateMessage,
690
- data: options
691
- },
692
- files: options.files,
693
- unauthorized: true
694
- });
695
- },
696
- async editOwnVoiceState (guildId, options) {
697
- await rest.patch(rest.routes.guilds.voice(guildId), {
698
- body: {
699
- ...options,
700
- request_to_speak_timestamp: options.requestToSpeakTimestamp ? new Date(options.requestToSpeakTimestamp).toISOString() : options.requestToSpeakTimestamp
701
- }
702
- });
703
- },
704
- async editScheduledEvent (guildId, eventId, body, reason) {
705
- return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), {
706
- body,
707
- reason
708
- });
709
- },
710
- async editRole (guildId, roleId, body, reason) {
711
- return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), {
712
- body,
713
- reason
714
- });
715
- },
716
- async editRolePositions (guildId, body, reason) {
717
- return await rest.patch(rest.routes.guilds.roles.all(guildId), {
718
- body,
719
- reason
720
- });
721
- },
722
- async editStageInstance (channelId, topic, reason) {
723
- return await rest.patch(rest.routes.channels.stage(channelId), {
724
- body: {
725
- topic
726
- },
727
- reason
728
- });
729
- },
730
- async editUserVoiceState (guildId, options) {
731
- await rest.patch(rest.routes.guilds.voice(guildId, options.userId), {
732
- body: options
733
- });
734
- },
735
- async editWebhook (webhookId, body, reason) {
736
- return await rest.patch(rest.routes.webhooks.id(webhookId), {
737
- body,
738
- reason
739
- });
740
- },
741
- async editWebhookMessage (webhookId, token, messageId, options) {
742
- return await rest.patch(rest.routes.webhooks.message(webhookId, token, messageId, options), {
743
- body: options,
744
- files: options.files
745
- });
746
- },
747
- async editWebhookWithToken (webhookId, token, body) {
748
- return await rest.patch(rest.routes.webhooks.webhook(webhookId, token), {
749
- body
750
- });
751
- },
752
- async editWelcomeScreen (guildId, body, reason) {
753
- return await rest.patch(rest.routes.guilds.welcome(guildId), {
754
- body,
755
- reason
756
- });
757
- },
758
- async editWidgetSettings (guildId, body, reason) {
759
- return await rest.patch(rest.routes.guilds.widget(guildId), {
760
- body,
761
- reason
762
- });
763
- },
764
- async executeWebhook (webhookId, token, options) {
765
- return await rest.post(rest.routes.webhooks.webhook(webhookId, token, options), {
766
- body: options
767
- });
768
- },
769
- async followAnnouncement (sourceChannelId, targetChannelId) {
770
- return await rest.post(rest.routes.channels.follow(sourceChannelId), {
771
- body: {
772
- webhook_channel_id: targetChannelId
773
- }
774
- });
775
- },
776
- async getActiveThreads (guildId) {
777
- return await rest.get(rest.routes.channels.threads.active(guildId));
778
- },
779
- async getApplicationCommandPermission (guildId, commandId) {
780
- return await rest.get(rest.routes.interactions.commands.permission(rest.applicationId, guildId, commandId));
781
- },
782
- async getApplicationCommandPermissions (guildId) {
783
- return await rest.get(rest.routes.interactions.commands.permissions(rest.applicationId, guildId));
784
- },
785
- async getApplicationInfo () {
786
- return await rest.get(rest.routes.oauth2Application());
787
- },
788
- async getAuditLog (guildId, options) {
789
- return await rest.get(rest.routes.guilds.auditlogs(guildId, options));
790
- },
791
- async getAutomodRule (guildId, ruleId) {
792
- return await rest.get(rest.routes.guilds.automod.rule(guildId, ruleId));
793
- },
794
- async getAutomodRules (guildId) {
795
- return await rest.get(rest.routes.guilds.automod.rules(guildId));
796
- },
797
- async getAvailableVoiceRegions () {
798
- return await rest.get(rest.routes.regions());
799
- },
800
- async getBan (guildId, userId) {
801
- return await rest.get(rest.routes.guilds.members.ban(guildId, userId));
802
- },
803
- async getBans (guildId, options) {
804
- return await rest.get(rest.routes.guilds.members.bans(guildId, options));
805
- },
806
- async getChannel (id) {
807
- return await rest.get(rest.routes.channels.channel(id));
808
- },
809
- async getChannelInvites (channelId) {
810
- return await rest.get(rest.routes.channels.invites(channelId));
811
- },
812
- async getChannels (guildId) {
813
- return await rest.get(rest.routes.guilds.channels(guildId));
814
- },
815
- async getChannelWebhooks (channelId) {
816
- return await rest.get(rest.routes.channels.webhooks(channelId));
817
- },
818
- async getDmChannel (userId) {
819
- return await rest.post(rest.routes.channels.dm(), {
820
- body: {
821
- recipient_id: userId
822
- }
823
- });
824
- },
825
- async getEmoji (guildId, emojiId) {
826
- return await rest.get(rest.routes.guilds.emoji(guildId, emojiId));
827
- },
828
- async getEmojis (guildId) {
829
- return await rest.get(rest.routes.guilds.emojis(guildId));
830
- },
831
- async getFollowupMessage (token, messageId) {
832
- return await rest.get(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
833
- unauthorized: true
834
- });
835
- },
836
- async getGatewayBot () {
837
- return await rest.get(rest.routes.gatewayBot());
838
- },
839
- async getGlobalApplicationCommand (commandId) {
840
- return await rest.get(rest.routes.interactions.commands.command(rest.applicationId, commandId));
841
- },
842
- async getGlobalApplicationCommands () {
843
- return await rest.get(rest.routes.interactions.commands.commands(rest.applicationId));
844
- },
845
- async getGuild (guildId, options = {
846
- counts: true
847
- }) {
848
- return await rest.get(rest.routes.guilds.guild(guildId, options.counts));
849
- },
850
- async getGuildApplicationCommand (commandId, guildId) {
851
- return await rest.get(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId));
852
- },
853
- async getGuildApplicationCommands (guildId) {
854
- return await rest.get(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId));
855
- },
856
- async getGuildPreview (guildId) {
857
- return await rest.get(rest.routes.guilds.preview(guildId));
858
- },
859
- async getGuildTemplate (templateCode) {
860
- return await rest.get(rest.routes.guilds.templates.code(templateCode));
861
- },
862
- async getGuildTemplates (guildId) {
863
- return await rest.get(rest.routes.guilds.templates.all(guildId));
864
- },
865
- async getGuildWebhooks (guildId) {
866
- return await rest.get(rest.routes.guilds.webhooks(guildId));
867
- },
868
- async getIntegrations (guildId) {
869
- return await rest.get(rest.routes.guilds.integrations(guildId));
870
- },
871
- async getInvite (inviteCode, options) {
872
- return await rest.get(rest.routes.guilds.invite(inviteCode, options));
873
- },
874
- async getInvites (guildId) {
875
- return await rest.get(rest.routes.guilds.invites(guildId));
876
- },
877
- async getMessage (channelId, messageId) {
878
- return await rest.get(rest.routes.channels.message(channelId, messageId));
879
- },
880
- async getMessages (channelId, options) {
881
- return await rest.get(rest.routes.channels.messages(channelId, options));
882
- },
883
- async getNitroStickerPacks () {
884
- return await rest.get(rest.routes.nitroStickerPacks());
885
- },
886
- async getOriginalInteractionResponse (token) {
887
- return await rest.get(rest.routes.interactions.responses.original(rest.applicationId, token), {
888
- unauthorized: true
889
- });
890
- },
891
- async getPinnedMessages (channelId) {
892
- return await rest.get(rest.routes.channels.pins(channelId));
893
- },
894
- async getPrivateArchivedThreads (channelId, options) {
895
- return await rest.get(rest.routes.channels.threads.private(channelId, options));
896
- },
897
- async getPrivateJoinedArchivedThreads (channelId, options) {
898
- return await rest.get(rest.routes.channels.threads.joined(channelId, options));
899
- },
900
- async getPruneCount (guildId, options) {
901
- return await rest.get(rest.routes.guilds.prune(guildId, options));
902
- },
903
- async getPublicArchivedThreads (channelId, options) {
904
- return await rest.get(rest.routes.channels.threads.public(channelId, options));
905
- },
906
- async getRoles (guildId) {
907
- return await rest.get(rest.routes.guilds.roles.all(guildId));
908
- },
909
- async getScheduledEvent (guildId, eventId, options) {
910
- return await rest.get(rest.routes.guilds.events.event(guildId, eventId, options?.withUserCount));
911
- },
912
- async getScheduledEvents (guildId, options) {
913
- return await rest.get(rest.routes.guilds.events.events(guildId, options?.withUserCount));
914
- },
915
- async getScheduledEventUsers (guildId, eventId, options) {
916
- return await rest.get(rest.routes.guilds.events.users(guildId, eventId, options));
917
- },
918
- async getSessionInfo () {
919
- return await rest.getGatewayBot();
920
- },
921
- async getStageInstance (channelId) {
922
- return await rest.get(rest.routes.channels.stage(channelId));
923
- },
924
- async getSticker (stickerId) {
925
- return await rest.get(rest.routes.sticker(stickerId));
926
- },
927
- async getGuildSticker (guildId, stickerId) {
928
- return await rest.get(rest.routes.guilds.sticker(guildId, stickerId));
929
- },
930
- async getGuildStickers (guildId) {
931
- return await rest.get(rest.routes.guilds.stickers(guildId));
932
- },
933
- async getThreadMember (channelId, userId) {
934
- return await rest.get(rest.routes.channels.threads.user(channelId, userId));
935
- },
936
- async getThreadMembers (channelId) {
937
- return await rest.get(rest.routes.channels.threads.members(channelId));
938
- },
939
- async getReactions (channelId, messageId, reaction, options) {
940
- return await rest.get(rest.routes.channels.reactions.message(channelId, messageId, reaction, options));
941
- },
942
- async getUser (id) {
943
- return await rest.get(rest.routes.user(id));
944
- },
945
- async getVanityUrl (guildId) {
946
- return await rest.get(rest.routes.guilds.vanity(guildId));
947
- },
948
- async getVoiceRegions (guildId) {
949
- return await rest.get(rest.routes.guilds.regions(guildId));
950
- },
951
- async getWebhook (webhookId) {
952
- return await rest.get(rest.routes.webhooks.id(webhookId));
953
- },
954
- async getWebhookMessage (webhookId, token, messageId, options) {
955
- return await rest.get(rest.routes.webhooks.message(webhookId, token, messageId, options));
956
- },
957
- async getWebhookWithToken (webhookId, token) {
958
- return await rest.get(rest.routes.webhooks.webhook(webhookId, token));
959
- },
960
- async getWelcomeScreen (guildId) {
961
- return await rest.get(rest.routes.guilds.welcome(guildId));
962
- },
963
- async getWidget (guildId) {
964
- return await rest.get(rest.routes.guilds.widgetJson(guildId));
965
- },
966
- async getWidgetSettings (guildId) {
967
- return await rest.get(rest.routes.guilds.widget(guildId));
968
- },
969
- async joinThread (channelId) {
970
- await rest.put(rest.routes.channels.threads.me(channelId));
971
- },
972
- async leaveGuild (guildId) {
973
- await rest.delete(rest.routes.guilds.leave(guildId));
974
- },
975
- async leaveThread (channelId) {
976
- await rest.delete(rest.routes.channels.threads.me(channelId));
977
- },
978
- async publishMessage (channelId, messageId) {
979
- return await rest.post(rest.routes.channels.crosspost(channelId, messageId));
980
- },
981
- async removeRole (guildId, userId, roleId, reason) {
982
- await rest.delete(rest.routes.guilds.roles.member(guildId, userId, roleId), {
983
- reason
984
- });
985
- },
986
- async removeThreadMember (channelId, userId) {
987
- await rest.delete(rest.routes.channels.threads.user(channelId, userId));
988
- },
989
- async sendFollowupMessage (token, options) {
990
- return await rest.post(rest.routes.webhooks.webhook(rest.applicationId, token), {
991
- body: options,
992
- files: options.files,
993
- unauthorized: true
994
- });
995
- },
996
- async sendInteractionResponse (interactionId, token, options) {
997
- return await rest.post(rest.routes.interactions.responses.callback(interactionId, token), {
998
- body: options,
999
- files: options.data?.files,
1000
- runThroughQueue: false,
1001
- unauthorized: true
1002
- });
1003
- },
1004
- async sendMessage (channelId, body) {
1005
- return await rest.post(rest.routes.channels.messages(channelId), {
1006
- body,
1007
- files: body.files
1008
- });
1009
- },
1010
- async startThreadWithMessage (channelId, messageId, body, reason) {
1011
- return await rest.post(rest.routes.channels.threads.message(channelId, messageId), {
1012
- body,
1013
- reason
1014
- });
1015
- },
1016
- async startThreadWithoutMessage (channelId, body, reason) {
1017
- return await rest.post(rest.routes.channels.threads.all(channelId), {
1018
- body,
1019
- reason
1020
- });
1021
- },
1022
- async syncGuildTemplate (guildId) {
1023
- return await rest.put(rest.routes.guilds.templates.all(guildId));
1024
- },
1025
- async banMember (guildId, userId, body, reason) {
1026
- await rest.put(rest.routes.guilds.members.ban(guildId, userId), {
1027
- body,
1028
- reason
1029
- });
1030
- },
1031
- async editBotMember (guildId, body, reason) {
1032
- return await rest.patch(rest.routes.guilds.members.bot(guildId), {
1033
- body,
1034
- reason
1035
- });
1036
- },
1037
- async editMember (guildId, userId, body, reason) {
1038
- return await rest.patch(rest.routes.guilds.members.member(guildId, userId), {
1039
- body,
1040
- reason
1041
- });
1042
- },
1043
- async getMember (guildId, userId) {
1044
- return await rest.get(rest.routes.guilds.members.member(guildId, userId));
1045
- },
1046
- async getMembers (guildId, options) {
1047
- return await rest.get(rest.routes.guilds.members.members(guildId, options));
1048
- },
1049
- async kickMember (guildId, userId, reason) {
1050
- await rest.delete(rest.routes.guilds.members.member(guildId, userId), {
1051
- reason
1052
- });
1053
- },
1054
- async pinMessage (channelId, messageId, reason) {
1055
- await rest.put(rest.routes.channels.pin(channelId, messageId), {
1056
- reason
1057
- });
1058
- },
1059
- async pruneMembers (guildId, body, reason) {
1060
- return await rest.post(rest.routes.guilds.members.prune(guildId), {
1061
- body,
1062
- reason
1063
- });
1064
- },
1065
- async searchMembers (guildId, query, options) {
1066
- return await rest.get(rest.routes.guilds.members.search(guildId, query, options));
1067
- },
1068
- async unbanMember (guildId, userId, reason) {
1069
- await rest.delete(rest.routes.guilds.members.ban(guildId, userId), {
1070
- reason
1071
- });
1072
- },
1073
- async unpinMessage (channelId, messageId, reason) {
1074
- await rest.delete(rest.routes.channels.pin(channelId, messageId), {
1075
- reason
1076
- });
1077
- },
1078
- async triggerTypingIndicator (channelId) {
1079
- await rest.post(rest.routes.channels.typing(channelId));
1080
- },
1081
- async upsertGlobalApplicationCommands (body) {
1082
- return await rest.put(rest.routes.interactions.commands.commands(rest.applicationId), {
1083
- body
1084
- });
1085
- },
1086
- async upsertGuildApplicationCommands (guildId, body) {
1087
- return await rest.put(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
1088
- body
1089
- });
1090
- }
1091
- };
1092
- return rest;
1093
- }
1094
- var HttpResponseCode;
1095
- (function(HttpResponseCode) {
1096
- HttpResponseCode[HttpResponseCode[/** Minimum value of a code in oder to consider that it was successful. */ "Success"] = 200] = "Success";
1097
- HttpResponseCode[HttpResponseCode[/** Request completed successfully, but Discord returned an empty body. */ "NoContent"] = 204] = "NoContent";
1098
- HttpResponseCode[HttpResponseCode[/** Minimum value of a code in order to consider that something went wrong. */ "Error"] = 400] = "Error";
1099
- HttpResponseCode[HttpResponseCode[/** This request got rate limited. */ "TooManyRequests"] = 429] = "TooManyRequests";
1100
- })(HttpResponseCode || (HttpResponseCode = {}));
1101
-
1102
- //# sourceMappingURL=manager.js.map