@discordeno/rest 19.0.0-next.e2d86ea → 19.0.0-next.e30e5a2

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/README.md +15 -1
  2. package/dist/cjs/index.cjs +25 -0
  3. package/dist/cjs/invalidBucket.cjs +87 -0
  4. package/dist/cjs/manager.cjs +1446 -0
  5. package/dist/cjs/queue.cjs +166 -0
  6. package/dist/cjs/routes.cjs +582 -0
  7. package/dist/cjs/types.cjs +6 -0
  8. package/dist/cjs/typings/routes.cjs +6 -0
  9. package/dist/esm/index.js +8 -0
  10. package/dist/esm/invalidBucket.js +83 -0
  11. package/dist/esm/manager.js +1404 -0
  12. package/dist/esm/queue.js +156 -0
  13. package/dist/esm/routes.js +572 -0
  14. package/dist/esm/types.js +3 -0
  15. package/dist/esm/typings/routes.js +3 -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 -1
  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} +448 -109
  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 +30 -25
  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 -1112
  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,1112 +0,0 @@
1
- /* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable no-const-assign */ import { calculateBits, camelToSnakeCase, camelize, delay, getBotIdFromToken, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
2
- import { createInvalidRequestBucket } from './invalidBucket.js';
3
- import { Queue } from './queue.js';
4
- import { InteractionResponseTypes } from '@discordeno/types';
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).catch(async (error)=>{
222
- logger.error(error);
223
- // Mark request and completed
224
- rest.invalidBucket.handleCompletedRequest(999, false);
225
- options.reject({
226
- ok: false,
227
- status: 999,
228
- error: 'Possible network or request shape issue occurred. If this is rare, its a network glitch. If it occurs a lot something is wrong.'
229
- });
230
- throw error;
231
- });
232
- logger.debug(`request fetched from ${url} with status ${response.status} & ${response.statusText}`);
233
- // Mark request and completed
234
- rest.invalidBucket.handleCompletedRequest(response.status, response.headers.get(RATE_LIMIT_SCOPE_HEADER) === 'shared');
235
- // Set the bucket id if it was available on the headers
236
- const bucketId = rest.processHeaders(rest.simplifyUrl(options.route, options.method), response.headers);
237
- if (bucketId) options.bucketId = bucketId;
238
- if (response.status < 200 || response.status >= 400) {
239
- logger.debug(`Request to ${url} failed.`);
240
- if (response.status !== 429) {
241
- options.reject({
242
- ok: false,
243
- status: response.status,
244
- body: await response.text()
245
- });
246
- return;
247
- }
248
- logger.debug(`Request to ${url} was ratelimited.`);
249
- // Too many attempts, get rid of request from queue.
250
- if (options.retryCount >= rest.maxRetryCount) {
251
- logger.debug(`Request to ${url} exceeded the maximum allowed retries.`, 'with payload:', payload);
252
- // rest.debug(`[REST - RetriesMaxed] ${JSON.stringify(options)}`)
253
- options.reject({
254
- ok: false,
255
- status: response.status,
256
- error: 'The request was rate limited and it maxed out the retries limit.'
257
- });
258
- return;
259
- }
260
- options.retryCount += 1;
261
- const resetAfter = response.headers.get(RATE_LIMIT_RESET_AFTER_HEADER);
262
- if (resetAfter) await delay(Number(resetAfter) * 1000);
263
- // process the response to prevent mem leak
264
- await response.arrayBuffer();
265
- return await options.retryRequest?.(options);
266
- }
267
- // Discord sometimes sends no response with no content.
268
- options.resolve({
269
- ok: true,
270
- status: response.status,
271
- body: response.status === 204 ? undefined : await response.text()
272
- });
273
- },
274
- simplifyUrl (url, method) {
275
- const parts = url.split('/');
276
- const secondLastPart = parts[parts.length - 2];
277
- if (secondLastPart === 'channels' || secondLastPart === 'guilds') {
278
- return url;
279
- }
280
- if (secondLastPart === 'reactions' || parts[parts.length - 1] === '@me') {
281
- parts.splice(-2);
282
- parts.push('reactions');
283
- } else {
284
- parts.splice(-1);
285
- parts.push('x');
286
- }
287
- if (parts[parts.length - 3] === 'reactions') {
288
- parts.splice(-2);
289
- }
290
- if (method === 'DELETE' && secondLastPart === 'messages') {
291
- return `D${parts.join('/')}`;
292
- }
293
- return parts.join('/');
294
- },
295
- async processRequest (request) {
296
- const url = rest.simplifyUrl(request.route, request.method);
297
- if (request.runThroughQueue === false) {
298
- await rest.sendRequest(request);
299
- return;
300
- }
301
- const queue = rest.queues.get(url);
302
- if (queue !== undefined) {
303
- queue.makeRequest(request);
304
- } else {
305
- // CREATES A NEW QUEUE
306
- const bucketQueue = new Queue(rest, {
307
- url,
308
- deleteQueueDelay: rest.deleteQueueDelay
309
- });
310
- // Add request to queue
311
- bucketQueue.makeRequest(request);
312
- // Save queue
313
- rest.queues.set(url, bucketQueue);
314
- }
315
- },
316
- async makeRequest (method, route, options) {
317
- if (rest.isProxied) {
318
- if (rest.authorization !== undefined) {
319
- options ??= {};
320
- options.headers ??= {};
321
- options.headers.authorization = rest.authorization;
322
- }
323
- const result = await fetch(`${rest.baseUrl}/v${rest.version}${route}`, rest.createRequestBody(method, options));
324
- if (!result.ok) {
325
- const err = await result.json().catch(()=>{});
326
- // Legacy Handling to not break old code or when body is missing
327
- if (!err?.body) throw new Error(`Error: ${err.message ?? result.statusText}`);
328
- throw new Error(JSON.stringify(err));
329
- }
330
- return result.status !== 204 ? await result.json() : undefined;
331
- }
332
- // eslint-disable-next-line no-async-promise-executor
333
- return await new Promise(async (resolve, reject)=>{
334
- const payload = {
335
- route,
336
- method,
337
- requestBodyOptions: options,
338
- retryCount: 0,
339
- retryRequest: async function(payload) {
340
- await rest.processRequest(payload);
341
- },
342
- resolve: (data)=>{
343
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
344
- },
345
- reject,
346
- runThroughQueue: options?.runThroughQueue
347
- };
348
- await rest.processRequest(payload);
349
- });
350
- },
351
- async get (url, options) {
352
- return camelize(await rest.makeRequest('GET', url, options));
353
- },
354
- async post (url, options) {
355
- return camelize(await rest.makeRequest('POST', url, options));
356
- },
357
- async delete (url, options) {
358
- camelize(await rest.makeRequest('DELETE', url, options));
359
- },
360
- async patch (url, options) {
361
- return camelize(await rest.makeRequest('PATCH', url, options));
362
- },
363
- async put (url, options) {
364
- return camelize(await rest.makeRequest('PUT', url, options));
365
- },
366
- async addReaction (channelId, messageId, reaction) {
367
- reaction = processReactionString(reaction);
368
- await rest.put(rest.routes.channels.reactions.bot(channelId, messageId, reaction));
369
- },
370
- async addReactions (channelId, messageId, reactions, ordered = false) {
371
- if (!ordered) {
372
- await Promise.all(reactions.map(async (reaction)=>{
373
- await rest.addReaction(channelId, messageId, reaction);
374
- }));
375
- return;
376
- }
377
- for (const reaction of reactions){
378
- await rest.addReaction(channelId, messageId, reaction);
379
- }
380
- },
381
- async addRole (guildId, userId, roleId, reason) {
382
- await rest.put(rest.routes.guilds.roles.member(guildId, userId, roleId), {
383
- reason
384
- });
385
- },
386
- async addThreadMember (channelId, userId) {
387
- await rest.put(rest.routes.channels.threads.user(channelId, userId));
388
- },
389
- async createAutomodRule (guildId, body, reason) {
390
- return await rest.post(rest.routes.guilds.automod.rules(guildId), {
391
- body,
392
- reason
393
- });
394
- },
395
- async createChannel (guildId, body, reason) {
396
- return await rest.post(rest.routes.guilds.channels(guildId), {
397
- body,
398
- reason
399
- });
400
- },
401
- async createEmoji (guildId, body, reason) {
402
- return await rest.post(rest.routes.guilds.emojis(guildId), {
403
- body,
404
- reason
405
- });
406
- },
407
- async createGlobalApplicationCommand (body) {
408
- return await rest.post(rest.routes.interactions.commands.commands(rest.applicationId), {
409
- body
410
- });
411
- },
412
- async createGuild (body) {
413
- return await rest.post(rest.routes.guilds.all(), {
414
- body
415
- });
416
- },
417
- async createGuildApplicationCommand (body, guildId) {
418
- return await rest.post(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
419
- body
420
- });
421
- },
422
- async createGuildFromTemplate (templateCode, body) {
423
- if (body.icon) {
424
- body.icon = await urlToBase64(body.icon);
425
- }
426
- return await rest.post(rest.routes.guilds.templates.code(templateCode), {
427
- body
428
- });
429
- },
430
- async createGuildSticker (guildId, options, reason) {
431
- const form = new FormData();
432
- form.append('file', options.file.blob, options.file.name);
433
- form.append('name', options.name);
434
- form.append('description', options.description);
435
- form.append('tags', options.tags);
436
- return await rest.post(rest.routes.guilds.stickers(guildId), {
437
- body: form,
438
- reason
439
- });
440
- },
441
- async createGuildTemplate (guildId, body) {
442
- return await rest.post(rest.routes.guilds.templates.all(guildId), {
443
- body
444
- });
445
- },
446
- async createForumThread (channelId, body, reason) {
447
- return await rest.post(rest.routes.channels.forum(channelId), {
448
- body,
449
- files: body.files,
450
- reason
451
- });
452
- },
453
- async createInvite (channelId, body = {}, reason) {
454
- return await rest.post(rest.routes.channels.invites(channelId), {
455
- body,
456
- reason
457
- });
458
- },
459
- async createRole (guildId, body, reason) {
460
- return await rest.post(rest.routes.guilds.roles.all(guildId), {
461
- body,
462
- reason
463
- });
464
- },
465
- async createScheduledEvent (guildId, body, reason) {
466
- return await rest.post(rest.routes.guilds.events.events(guildId), {
467
- body,
468
- reason
469
- });
470
- },
471
- async createStageInstance (body, reason) {
472
- return await rest.post(rest.routes.channels.stages(), {
473
- body,
474
- reason
475
- });
476
- },
477
- async createWebhook (channelId, options, reason) {
478
- return await rest.post(rest.routes.channels.webhooks(channelId), {
479
- body: {
480
- name: options.name,
481
- avatar: options.avatar ? await urlToBase64(options.avatar) : undefined
482
- },
483
- reason
484
- });
485
- },
486
- async deleteAutomodRule (guildId, ruleId, reason) {
487
- await rest.delete(rest.routes.guilds.automod.rule(guildId, ruleId), {
488
- reason
489
- });
490
- },
491
- async deleteChannel (channelId, reason) {
492
- await rest.delete(rest.routes.channels.channel(channelId), {
493
- reason
494
- });
495
- },
496
- async deleteChannelPermissionOverride (channelId, overwriteId, reason) {
497
- await rest.delete(rest.routes.channels.overwrite(channelId, overwriteId), {
498
- reason
499
- });
500
- },
501
- async deleteEmoji (guildId, id, reason) {
502
- await rest.delete(rest.routes.guilds.emoji(guildId, id), {
503
- reason
504
- });
505
- },
506
- async deleteFollowupMessage (token, messageId) {
507
- await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
508
- unauthorized: true
509
- });
510
- },
511
- async deleteGlobalApplicationCommand (commandId) {
512
- await rest.delete(rest.routes.interactions.commands.command(rest.applicationId, commandId));
513
- },
514
- async deleteGuild (guildId) {
515
- await rest.delete(rest.routes.guilds.guild(guildId));
516
- },
517
- async deleteGuildApplicationCommand (commandId, guildId) {
518
- await rest.delete(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId));
519
- },
520
- async deleteGuildSticker (guildId, stickerId, reason) {
521
- await rest.delete(rest.routes.guilds.sticker(guildId, stickerId), {
522
- reason
523
- });
524
- },
525
- async deleteGuildTemplate (guildId, templateCode) {
526
- await rest.delete(rest.routes.guilds.templates.guild(guildId, templateCode));
527
- },
528
- async deleteIntegration (guildId, integrationId, reason) {
529
- await rest.delete(rest.routes.guilds.integration(guildId, integrationId), {
530
- reason
531
- });
532
- },
533
- async deleteInvite (inviteCode, reason) {
534
- await rest.delete(rest.routes.guilds.invite(inviteCode), {
535
- reason
536
- });
537
- },
538
- async deleteMessage (channelId, messageId, reason) {
539
- await rest.delete(rest.routes.channels.message(channelId, messageId), {
540
- reason
541
- });
542
- },
543
- async deleteMessages (channelId, messageIds, reason) {
544
- await rest.post(rest.routes.channels.bulk(channelId), {
545
- body: {
546
- messages: messageIds.slice(0, 100).map((id)=>id.toString())
547
- },
548
- reason
549
- });
550
- },
551
- async deleteOriginalInteractionResponse (token) {
552
- await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token), {
553
- unauthorized: true
554
- });
555
- },
556
- async deleteOwnReaction (channelId, messageId, reaction) {
557
- reaction = processReactionString(reaction);
558
- await rest.delete(rest.routes.channels.reactions.bot(channelId, messageId, reaction));
559
- },
560
- async deleteReactionsAll (channelId, messageId) {
561
- await rest.delete(rest.routes.channels.reactions.all(channelId, messageId));
562
- },
563
- async deleteReactionsEmoji (channelId, messageId, reaction) {
564
- reaction = processReactionString(reaction);
565
- await rest.delete(rest.routes.channels.reactions.emoji(channelId, messageId, reaction));
566
- },
567
- async deleteRole (guildId, roleId, reason) {
568
- await rest.delete(rest.routes.guilds.roles.one(guildId, roleId), {
569
- reason
570
- });
571
- },
572
- async deleteScheduledEvent (guildId, eventId) {
573
- await rest.delete(rest.routes.guilds.events.event(guildId, eventId));
574
- },
575
- async deleteStageInstance (channelId, reason) {
576
- await rest.delete(rest.routes.channels.stage(channelId), {
577
- reason
578
- });
579
- },
580
- async deleteUserReaction (channelId, messageId, userId, reaction) {
581
- reaction = processReactionString(reaction);
582
- await rest.delete(rest.routes.channels.reactions.user(channelId, messageId, reaction, userId));
583
- },
584
- async deleteWebhook (webhookId, reason) {
585
- await rest.delete(rest.routes.webhooks.id(webhookId), {
586
- reason
587
- });
588
- },
589
- async deleteWebhookMessage (webhookId, token, messageId, options) {
590
- await rest.delete(rest.routes.webhooks.message(webhookId, token, messageId, options));
591
- },
592
- async deleteWebhookWithToken (webhookId, token) {
593
- await rest.delete(rest.routes.webhooks.webhook(webhookId, token));
594
- },
595
- async editApplicationCommandPermissions (guildId, commandId, bearerToken, permissions) {
596
- return await rest.put(rest.routes.interactions.commands.permission(rest.applicationId, guildId, commandId), {
597
- body: {
598
- permissions
599
- },
600
- headers: {
601
- authorization: `Bearer ${bearerToken}`
602
- }
603
- });
604
- },
605
- async editAutomodRule (guildId, ruleId, body, reason) {
606
- return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), {
607
- body,
608
- reason
609
- });
610
- },
611
- async editBotProfile (options) {
612
- const avatar = options?.botAvatarURL ? await urlToBase64(options?.botAvatarURL) : options?.botAvatarURL;
613
- return await rest.patch(rest.routes.userBot(), {
614
- body: {
615
- username: options.username?.trim(),
616
- avatar
617
- }
618
- });
619
- },
620
- async editChannel (channelId, body, reason) {
621
- return await rest.patch(rest.routes.channels.channel(channelId), {
622
- body,
623
- reason
624
- });
625
- },
626
- async editChannelPermissionOverrides (channelId, body, reason) {
627
- await rest.put(rest.routes.channels.overwrite(channelId, body.id), {
628
- body,
629
- reason
630
- });
631
- },
632
- async editChannelPositions (guildId, body) {
633
- await rest.patch(rest.routes.guilds.channels(guildId), {
634
- body
635
- });
636
- },
637
- async editEmoji (guildId, id, body, reason) {
638
- return await rest.patch(rest.routes.guilds.emoji(guildId, id), {
639
- body,
640
- reason
641
- });
642
- },
643
- async editFollowupMessage (token, messageId, body) {
644
- return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
645
- body,
646
- files: body.files,
647
- unauthorized: true
648
- });
649
- },
650
- async editGlobalApplicationCommand (commandId, body) {
651
- return await rest.patch(rest.routes.interactions.commands.command(rest.applicationId, commandId), {
652
- body
653
- });
654
- },
655
- async editGuild (guildId, body, reason) {
656
- return await rest.patch(rest.routes.guilds.guild(guildId), {
657
- body,
658
- reason
659
- });
660
- },
661
- async editGuildApplicationCommand (commandId, guildId, body) {
662
- return await rest.patch(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId), {
663
- body
664
- });
665
- },
666
- async editGuildMfaLevel (guildId, mfaLevel, reason) {
667
- await rest.post(rest.routes.guilds.mfa(guildId), {
668
- body: {
669
- level: mfaLevel
670
- },
671
- reason
672
- });
673
- },
674
- async editGuildSticker (guildId, stickerId, body, reason) {
675
- return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), {
676
- body,
677
- reason
678
- });
679
- },
680
- async editGuildTemplate (guildId, templateCode, body) {
681
- return await rest.patch(rest.routes.guilds.templates.guild(guildId, templateCode), {
682
- body
683
- });
684
- },
685
- async editMessage (channelId, messageId, body) {
686
- return await rest.patch(rest.routes.channels.message(channelId, messageId), {
687
- body
688
- });
689
- },
690
- async editOriginalInteractionResponse (token, body) {
691
- return await rest.patch(rest.routes.interactions.responses.original(rest.applicationId, token), {
692
- body,
693
- files: body.files
694
- });
695
- },
696
- async editOriginalWebhookMessage (webhookId, token, options) {
697
- return await rest.patch(rest.routes.webhooks.original(webhookId, token, options), {
698
- body: {
699
- type: InteractionResponseTypes.UpdateMessage,
700
- data: options
701
- },
702
- files: options.files,
703
- unauthorized: true
704
- });
705
- },
706
- async editOwnVoiceState (guildId, options) {
707
- await rest.patch(rest.routes.guilds.voice(guildId), {
708
- body: {
709
- ...options,
710
- request_to_speak_timestamp: options.requestToSpeakTimestamp ? new Date(options.requestToSpeakTimestamp).toISOString() : options.requestToSpeakTimestamp
711
- }
712
- });
713
- },
714
- async editScheduledEvent (guildId, eventId, body, reason) {
715
- return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), {
716
- body,
717
- reason
718
- });
719
- },
720
- async editRole (guildId, roleId, body, reason) {
721
- return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), {
722
- body,
723
- reason
724
- });
725
- },
726
- async editRolePositions (guildId, body, reason) {
727
- return await rest.patch(rest.routes.guilds.roles.all(guildId), {
728
- body,
729
- reason
730
- });
731
- },
732
- async editStageInstance (channelId, topic, reason) {
733
- return await rest.patch(rest.routes.channels.stage(channelId), {
734
- body: {
735
- topic
736
- },
737
- reason
738
- });
739
- },
740
- async editUserVoiceState (guildId, options) {
741
- await rest.patch(rest.routes.guilds.voice(guildId, options.userId), {
742
- body: options
743
- });
744
- },
745
- async editWebhook (webhookId, body, reason) {
746
- return await rest.patch(rest.routes.webhooks.id(webhookId), {
747
- body,
748
- reason
749
- });
750
- },
751
- async editWebhookMessage (webhookId, token, messageId, options) {
752
- return await rest.patch(rest.routes.webhooks.message(webhookId, token, messageId, options), {
753
- body: options,
754
- files: options.files
755
- });
756
- },
757
- async editWebhookWithToken (webhookId, token, body) {
758
- return await rest.patch(rest.routes.webhooks.webhook(webhookId, token), {
759
- body
760
- });
761
- },
762
- async editWelcomeScreen (guildId, body, reason) {
763
- return await rest.patch(rest.routes.guilds.welcome(guildId), {
764
- body,
765
- reason
766
- });
767
- },
768
- async editWidgetSettings (guildId, body, reason) {
769
- return await rest.patch(rest.routes.guilds.widget(guildId), {
770
- body,
771
- reason
772
- });
773
- },
774
- async executeWebhook (webhookId, token, options) {
775
- return await rest.post(rest.routes.webhooks.webhook(webhookId, token, options), {
776
- body: options
777
- });
778
- },
779
- async followAnnouncement (sourceChannelId, targetChannelId) {
780
- return await rest.post(rest.routes.channels.follow(sourceChannelId), {
781
- body: {
782
- webhook_channel_id: targetChannelId
783
- }
784
- });
785
- },
786
- async getActiveThreads (guildId) {
787
- return await rest.get(rest.routes.channels.threads.active(guildId));
788
- },
789
- async getApplicationCommandPermission (guildId, commandId) {
790
- return await rest.get(rest.routes.interactions.commands.permission(rest.applicationId, guildId, commandId));
791
- },
792
- async getApplicationCommandPermissions (guildId) {
793
- return await rest.get(rest.routes.interactions.commands.permissions(rest.applicationId, guildId));
794
- },
795
- async getApplicationInfo () {
796
- return await rest.get(rest.routes.oauth2Application());
797
- },
798
- async getAuditLog (guildId, options) {
799
- return await rest.get(rest.routes.guilds.auditlogs(guildId, options));
800
- },
801
- async getAutomodRule (guildId, ruleId) {
802
- return await rest.get(rest.routes.guilds.automod.rule(guildId, ruleId));
803
- },
804
- async getAutomodRules (guildId) {
805
- return await rest.get(rest.routes.guilds.automod.rules(guildId));
806
- },
807
- async getAvailableVoiceRegions () {
808
- return await rest.get(rest.routes.regions());
809
- },
810
- async getBan (guildId, userId) {
811
- return await rest.get(rest.routes.guilds.members.ban(guildId, userId));
812
- },
813
- async getBans (guildId, options) {
814
- return await rest.get(rest.routes.guilds.members.bans(guildId, options));
815
- },
816
- async getChannel (id) {
817
- return await rest.get(rest.routes.channels.channel(id));
818
- },
819
- async getChannelInvites (channelId) {
820
- return await rest.get(rest.routes.channels.invites(channelId));
821
- },
822
- async getChannels (guildId) {
823
- return await rest.get(rest.routes.guilds.channels(guildId));
824
- },
825
- async getChannelWebhooks (channelId) {
826
- return await rest.get(rest.routes.channels.webhooks(channelId));
827
- },
828
- async getDmChannel (userId) {
829
- return await rest.post(rest.routes.channels.dm(), {
830
- body: {
831
- recipient_id: userId
832
- }
833
- });
834
- },
835
- async getEmoji (guildId, emojiId) {
836
- return await rest.get(rest.routes.guilds.emoji(guildId, emojiId));
837
- },
838
- async getEmojis (guildId) {
839
- return await rest.get(rest.routes.guilds.emojis(guildId));
840
- },
841
- async getFollowupMessage (token, messageId) {
842
- return await rest.get(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
843
- unauthorized: true
844
- });
845
- },
846
- async getGatewayBot () {
847
- return await rest.get(rest.routes.gatewayBot());
848
- },
849
- async getGlobalApplicationCommand (commandId) {
850
- return await rest.get(rest.routes.interactions.commands.command(rest.applicationId, commandId));
851
- },
852
- async getGlobalApplicationCommands () {
853
- return await rest.get(rest.routes.interactions.commands.commands(rest.applicationId));
854
- },
855
- async getGuild (guildId, options = {
856
- counts: true
857
- }) {
858
- return await rest.get(rest.routes.guilds.guild(guildId, options.counts));
859
- },
860
- async getGuildApplicationCommand (commandId, guildId) {
861
- return await rest.get(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId));
862
- },
863
- async getGuildApplicationCommands (guildId) {
864
- return await rest.get(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId));
865
- },
866
- async getGuildPreview (guildId) {
867
- return await rest.get(rest.routes.guilds.preview(guildId));
868
- },
869
- async getGuildTemplate (templateCode) {
870
- return await rest.get(rest.routes.guilds.templates.code(templateCode));
871
- },
872
- async getGuildTemplates (guildId) {
873
- return await rest.get(rest.routes.guilds.templates.all(guildId));
874
- },
875
- async getGuildWebhooks (guildId) {
876
- return await rest.get(rest.routes.guilds.webhooks(guildId));
877
- },
878
- async getIntegrations (guildId) {
879
- return await rest.get(rest.routes.guilds.integrations(guildId));
880
- },
881
- async getInvite (inviteCode, options) {
882
- return await rest.get(rest.routes.guilds.invite(inviteCode, options));
883
- },
884
- async getInvites (guildId) {
885
- return await rest.get(rest.routes.guilds.invites(guildId));
886
- },
887
- async getMessage (channelId, messageId) {
888
- return await rest.get(rest.routes.channels.message(channelId, messageId));
889
- },
890
- async getMessages (channelId, options) {
891
- return await rest.get(rest.routes.channels.messages(channelId, options));
892
- },
893
- async getNitroStickerPacks () {
894
- return await rest.get(rest.routes.nitroStickerPacks());
895
- },
896
- async getOriginalInteractionResponse (token) {
897
- return await rest.get(rest.routes.interactions.responses.original(rest.applicationId, token), {
898
- unauthorized: true
899
- });
900
- },
901
- async getPinnedMessages (channelId) {
902
- return await rest.get(rest.routes.channels.pins(channelId));
903
- },
904
- async getPrivateArchivedThreads (channelId, options) {
905
- return await rest.get(rest.routes.channels.threads.private(channelId, options));
906
- },
907
- async getPrivateJoinedArchivedThreads (channelId, options) {
908
- return await rest.get(rest.routes.channels.threads.joined(channelId, options));
909
- },
910
- async getPruneCount (guildId, options) {
911
- return await rest.get(rest.routes.guilds.prune(guildId, options));
912
- },
913
- async getPublicArchivedThreads (channelId, options) {
914
- return await rest.get(rest.routes.channels.threads.public(channelId, options));
915
- },
916
- async getRoles (guildId) {
917
- return await rest.get(rest.routes.guilds.roles.all(guildId));
918
- },
919
- async getScheduledEvent (guildId, eventId, options) {
920
- return await rest.get(rest.routes.guilds.events.event(guildId, eventId, options?.withUserCount));
921
- },
922
- async getScheduledEvents (guildId, options) {
923
- return await rest.get(rest.routes.guilds.events.events(guildId, options?.withUserCount));
924
- },
925
- async getScheduledEventUsers (guildId, eventId, options) {
926
- return await rest.get(rest.routes.guilds.events.users(guildId, eventId, options));
927
- },
928
- async getSessionInfo () {
929
- return await rest.getGatewayBot();
930
- },
931
- async getStageInstance (channelId) {
932
- return await rest.get(rest.routes.channels.stage(channelId));
933
- },
934
- async getSticker (stickerId) {
935
- return await rest.get(rest.routes.sticker(stickerId));
936
- },
937
- async getGuildSticker (guildId, stickerId) {
938
- return await rest.get(rest.routes.guilds.sticker(guildId, stickerId));
939
- },
940
- async getGuildStickers (guildId) {
941
- return await rest.get(rest.routes.guilds.stickers(guildId));
942
- },
943
- async getThreadMember (channelId, userId) {
944
- return await rest.get(rest.routes.channels.threads.user(channelId, userId));
945
- },
946
- async getThreadMembers (channelId) {
947
- return await rest.get(rest.routes.channels.threads.members(channelId));
948
- },
949
- async getReactions (channelId, messageId, reaction, options) {
950
- return await rest.get(rest.routes.channels.reactions.message(channelId, messageId, reaction, options));
951
- },
952
- async getUser (id) {
953
- return await rest.get(rest.routes.user(id));
954
- },
955
- async getVanityUrl (guildId) {
956
- return await rest.get(rest.routes.guilds.vanity(guildId));
957
- },
958
- async getVoiceRegions (guildId) {
959
- return await rest.get(rest.routes.guilds.regions(guildId));
960
- },
961
- async getWebhook (webhookId) {
962
- return await rest.get(rest.routes.webhooks.id(webhookId));
963
- },
964
- async getWebhookMessage (webhookId, token, messageId, options) {
965
- return await rest.get(rest.routes.webhooks.message(webhookId, token, messageId, options));
966
- },
967
- async getWebhookWithToken (webhookId, token) {
968
- return await rest.get(rest.routes.webhooks.webhook(webhookId, token));
969
- },
970
- async getWelcomeScreen (guildId) {
971
- return await rest.get(rest.routes.guilds.welcome(guildId));
972
- },
973
- async getWidget (guildId) {
974
- return await rest.get(rest.routes.guilds.widgetJson(guildId));
975
- },
976
- async getWidgetSettings (guildId) {
977
- return await rest.get(rest.routes.guilds.widget(guildId));
978
- },
979
- async joinThread (channelId) {
980
- await rest.put(rest.routes.channels.threads.me(channelId));
981
- },
982
- async leaveGuild (guildId) {
983
- await rest.delete(rest.routes.guilds.leave(guildId));
984
- },
985
- async leaveThread (channelId) {
986
- await rest.delete(rest.routes.channels.threads.me(channelId));
987
- },
988
- async publishMessage (channelId, messageId) {
989
- return await rest.post(rest.routes.channels.crosspost(channelId, messageId));
990
- },
991
- async removeRole (guildId, userId, roleId, reason) {
992
- await rest.delete(rest.routes.guilds.roles.member(guildId, userId, roleId), {
993
- reason
994
- });
995
- },
996
- async removeThreadMember (channelId, userId) {
997
- await rest.delete(rest.routes.channels.threads.user(channelId, userId));
998
- },
999
- async sendFollowupMessage (token, options) {
1000
- return await rest.post(rest.routes.webhooks.webhook(rest.applicationId, token), {
1001
- body: options,
1002
- files: options.files,
1003
- unauthorized: true
1004
- });
1005
- },
1006
- async sendInteractionResponse (interactionId, token, options) {
1007
- return await rest.post(rest.routes.interactions.responses.callback(interactionId, token), {
1008
- body: options,
1009
- files: options.data?.files,
1010
- runThroughQueue: false,
1011
- unauthorized: true
1012
- });
1013
- },
1014
- async sendMessage (channelId, body) {
1015
- return await rest.post(rest.routes.channels.messages(channelId), {
1016
- body,
1017
- files: body.files
1018
- });
1019
- },
1020
- async startThreadWithMessage (channelId, messageId, body, reason) {
1021
- return await rest.post(rest.routes.channels.threads.message(channelId, messageId), {
1022
- body,
1023
- reason
1024
- });
1025
- },
1026
- async startThreadWithoutMessage (channelId, body, reason) {
1027
- return await rest.post(rest.routes.channels.threads.all(channelId), {
1028
- body,
1029
- reason
1030
- });
1031
- },
1032
- async syncGuildTemplate (guildId) {
1033
- return await rest.put(rest.routes.guilds.templates.all(guildId));
1034
- },
1035
- async banMember (guildId, userId, body, reason) {
1036
- await rest.put(rest.routes.guilds.members.ban(guildId, userId), {
1037
- body,
1038
- reason
1039
- });
1040
- },
1041
- async editBotMember (guildId, body, reason) {
1042
- return await rest.patch(rest.routes.guilds.members.bot(guildId), {
1043
- body,
1044
- reason
1045
- });
1046
- },
1047
- async editMember (guildId, userId, body, reason) {
1048
- return await rest.patch(rest.routes.guilds.members.member(guildId, userId), {
1049
- body,
1050
- reason
1051
- });
1052
- },
1053
- async getMember (guildId, userId) {
1054
- return await rest.get(rest.routes.guilds.members.member(guildId, userId));
1055
- },
1056
- async getMembers (guildId, options) {
1057
- return await rest.get(rest.routes.guilds.members.members(guildId, options));
1058
- },
1059
- async kickMember (guildId, userId, reason) {
1060
- await rest.delete(rest.routes.guilds.members.member(guildId, userId), {
1061
- reason
1062
- });
1063
- },
1064
- async pinMessage (channelId, messageId, reason) {
1065
- await rest.put(rest.routes.channels.pin(channelId, messageId), {
1066
- reason
1067
- });
1068
- },
1069
- async pruneMembers (guildId, body, reason) {
1070
- return await rest.post(rest.routes.guilds.members.prune(guildId), {
1071
- body,
1072
- reason
1073
- });
1074
- },
1075
- async searchMembers (guildId, query, options) {
1076
- return await rest.get(rest.routes.guilds.members.search(guildId, query, options));
1077
- },
1078
- async unbanMember (guildId, userId, reason) {
1079
- await rest.delete(rest.routes.guilds.members.ban(guildId, userId), {
1080
- reason
1081
- });
1082
- },
1083
- async unpinMessage (channelId, messageId, reason) {
1084
- await rest.delete(rest.routes.channels.pin(channelId, messageId), {
1085
- reason
1086
- });
1087
- },
1088
- async triggerTypingIndicator (channelId) {
1089
- await rest.post(rest.routes.channels.typing(channelId));
1090
- },
1091
- async upsertGlobalApplicationCommands (body) {
1092
- return await rest.put(rest.routes.interactions.commands.commands(rest.applicationId), {
1093
- body
1094
- });
1095
- },
1096
- async upsertGuildApplicationCommands (guildId, body) {
1097
- return await rest.put(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
1098
- body
1099
- });
1100
- }
1101
- };
1102
- return rest;
1103
- }
1104
- var HttpResponseCode;
1105
- (function(HttpResponseCode) {
1106
- HttpResponseCode[HttpResponseCode[/** Minimum value of a code in oder to consider that it was successful. */ "Success"] = 200] = "Success";
1107
- HttpResponseCode[HttpResponseCode[/** Request completed successfully, but Discord returned an empty body. */ "NoContent"] = 204] = "NoContent";
1108
- HttpResponseCode[HttpResponseCode[/** Minimum value of a code in order to consider that something went wrong. */ "Error"] = 400] = "Error";
1109
- HttpResponseCode[HttpResponseCode[/** This request got rate limited. */ "TooManyRequests"] = 429] = "TooManyRequests";
1110
- })(HttpResponseCode || (HttpResponseCode = {}));
1111
-
1112
- //# sourceMappingURL=manager.js.map