@discordeno/rest 19.0.0-next.e3549a8 → 19.0.0-next.e45e5ac

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.
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  /**
3
2
  * A invalid request bucket is used in a similar manner as a leaky bucket but a invalid request bucket can be refilled as needed.
4
3
  * It's purpose is to make sure the bot does not hit the limit to getting a 1 hr ban.
@@ -14,8 +13,8 @@ export interface InvalidRequestBucketOptions {
14
13
  max?: number;
15
14
  /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */
16
15
  interval?: number;
17
- /** timer to reset to 0 */
18
- timeoutId?: NodeJS.Timeout;
16
+ /** When the timeout for the bucket has started at. */
17
+ resetAt?: number;
19
18
  /** how safe to be from max. Defaults to 1 */
20
19
  safety?: number;
21
20
  /** The request statuses that count as an invalid request. */
@@ -25,19 +24,19 @@ export interface InvalidRequestBucketOptions {
25
24
  }
26
25
  export interface InvalidRequestBucket {
27
26
  /** current invalid amount */
28
- current: number;
27
+ invalidRequests: number;
29
28
  /** max invalid requests allowed until ban. Defaults to 10,000 */
30
29
  max: number;
31
30
  /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */
32
31
  interval: number;
33
- /** timer to reset to 0 */
34
- timeoutId: NodeJS.Timeout | undefined;
32
+ /** When the timeout for this bucket has started at. */
33
+ resetAt: number | undefined;
35
34
  /** how safe to be from max. Defaults to 1 */
36
35
  safety: number;
37
36
  /** The request statuses that count as an invalid request. */
38
37
  errorStatuses: number[];
39
38
  /** The amount of requests that were requested from this bucket. */
40
- requested: number;
39
+ activeRequests: number;
41
40
  /** The requests that are currently pending. */
42
41
  waiting: Array<(value: void | PromiseLike<void>) => void>;
43
42
  /** Whether or not the waiting queue is already processing. */
@@ -1 +1 @@
1
- {"version":3,"file":"invalidBucket.d.ts","sourceRoot":"","sources":["../src/invalidBucket.ts"],"names":[],"mappings":";AAEA;;;;;;GAMG;AACH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;IAEnB,+DAA+D;IAC/D,eAAe,EAAE,MAAM,MAAM,CAAA;IAC7B,mDAAmD;IACnD,gBAAgB,EAAE,MAAM,OAAO,CAAA;IAC/B,yCAAyC;IACzC,yBAAyB,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9C,uDAAuD;IACvD,cAAc,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IACnC,iIAAiI;IACjI,sBAAsB,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,KAAK,IAAI,CAAA;CACrE"}
@@ -7,21 +7,25 @@ import { delay, logger } from '@discordeno/utils';
7
7
  * @returns RefillingBucket
8
8
  */ export function createInvalidRequestBucket(options) {
9
9
  const bucket = {
10
- current: options.current ?? 0,
10
+ invalidRequests: options.current ?? 0,
11
11
  max: options.max ?? 10000,
12
- interval: options.interval ?? 600000,
13
- timeoutId: options.timeoutId,
12
+ interval: options.interval ?? 600_000,
13
+ resetAt: options.resetAt,
14
14
  safety: options.safety ?? 1,
15
15
  errorStatuses: options.errorStatuses ?? [
16
16
  401,
17
17
  403,
18
18
  429
19
19
  ],
20
- requested: options.requested ?? 0,
20
+ activeRequests: options.requested ?? 0,
21
21
  processing: false,
22
22
  waiting: [],
23
23
  requestsAllowed: function() {
24
- return bucket.max - bucket.current - bucket.requested - bucket.safety;
24
+ if (bucket.resetAt !== undefined && Date.now() >= bucket.resetAt) {
25
+ bucket.invalidRequests = 0;
26
+ bucket.resetAt = Date.now() + bucket.interval;
27
+ }
28
+ return bucket.max - bucket.invalidRequests - bucket.activeRequests - bucket.safety;
25
29
  },
26
30
  isRequestAllowed: function() {
27
31
  return bucket.requestsAllowed() > 0;
@@ -31,7 +35,7 @@ import { delay, logger } from '@discordeno/utils';
31
35
  return await new Promise(async (resolve)=>{
32
36
  // If whatever amount of requests is left is more than the safety margin, allow the request
33
37
  if (bucket.isRequestAllowed()) {
34
- bucket.requested++;
38
+ bucket.activeRequests += 1;
35
39
  resolve();
36
40
  } else {
37
41
  bucket.waiting.push(resolve);
@@ -41,41 +45,35 @@ import { delay, logger } from '@discordeno/utils';
41
45
  },
42
46
  processWaiting: async function() {
43
47
  // If already processing, that loop will handle all waiting requests.
44
- if (bucket.processing) {
45
- return;
46
- }
48
+ if (bucket.processing) return;
47
49
  // Mark as processing so other loops don't start
48
50
  bucket.processing = true;
49
51
  while(bucket.waiting.length > 0){
50
- logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} remaining.`);
51
- if (bucket.isRequestAllowed()) {
52
- bucket.requested++;
53
- // Resolve the next item in the queue
54
- bucket.waiting.shift()?.();
55
- } else {
56
- await delay(1000);
52
+ logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} pending requests to be made. ${JSON.stringify(bucket)}`);
53
+ if (!bucket.isRequestAllowed() && bucket.resetAt !== undefined) {
54
+ logger.warn(`[InvalidBucket] processing waiting queue is now paused until more requests are available. ${bucket.waiting.length} pending requests. ${JSON.stringify(bucket)}`);
55
+ await delay(bucket.resetAt - Date.now());
57
56
  }
57
+ bucket.activeRequests += 1;
58
+ // Resolve the next item in the queue
59
+ bucket.waiting.shift()?.();
58
60
  }
59
61
  // Mark as false so next pending request can be triggered by new loop.
60
62
  bucket.processing = false;
61
63
  },
62
64
  handleCompletedRequest: function(code, sharedScope) {
63
65
  // Since request is complete, we can remove one from requested.
64
- bucket.requested--;
66
+ bucket.activeRequests -= 1;
65
67
  // Since it is as a valid request, we don't need to do anything
66
68
  if (!bucket.errorStatuses.includes(code)) return;
67
69
  // Shared scope is not considered invalid
68
70
  if (code === 429 && sharedScope) return;
69
71
  // INVALID REQUEST WAS MADE
70
- // Mark a request has been invalid
71
- bucket.current++;
72
- // If a timeout was not started, start a timeout to reset this bucket
73
- if (bucket.timeoutId === undefined) {
74
- bucket.timeoutId = setTimeout(()=>{
75
- bucket.current = 0;
76
- bucket.timeoutId = undefined;
77
- }, bucket.interval);
72
+ if (bucket.resetAt === undefined) {
73
+ bucket.resetAt = Date.now() + bucket.interval;
78
74
  }
75
+ bucket.invalidRequests += 1;
76
+ logger.warn(`[InvalidBucket] an invalid request was made. Increasing invalidRequests count to ${bucket.invalidRequests}`);
79
77
  }
80
78
  };
81
79
  return bucket;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/invalidBucket.ts"],"sourcesContent":["import { delay, logger } from '@discordeno/utils'\n\n/**\n * A invalid request bucket is used in a similar manner as a leaky bucket but a invalid request bucket can be refilled as needed.\n * It's purpose is to make sure the bot does not hit the limit to getting a 1 hr ban.\n *\n * @param options The options used to configure this bucket.\n * @returns RefillingBucket\n */\nexport function createInvalidRequestBucket(options: InvalidRequestBucketOptions): InvalidRequestBucket {\n const bucket: InvalidRequestBucket = {\n current: options.current ?? 0,\n max: options.max ?? 10000,\n interval: options.interval ?? 600000,\n timeoutId: options.timeoutId,\n safety: options.safety ?? 1,\n errorStatuses: options.errorStatuses ?? [401, 403, 429],\n requested: options.requested ?? 0,\n processing: false,\n\n waiting: [],\n\n requestsAllowed: function () {\n return bucket.max - bucket.current - bucket.requested - bucket.safety\n },\n\n isRequestAllowed: function () {\n return bucket.requestsAllowed() > 0\n },\n\n waitUntilRequestAvailable: async function () {\n // eslint-disable-next-line no-async-promise-executor\n return await new Promise(async (resolve) => {\n // If whatever amount of requests is left is more than the safety margin, allow the request\n if (bucket.isRequestAllowed()) {\n bucket.requested++\n resolve()\n } else {\n bucket.waiting.push(resolve)\n await bucket.processWaiting()\n }\n })\n },\n\n processWaiting: async function () {\n // If already processing, that loop will handle all waiting requests.\n if (bucket.processing) {\n return\n }\n\n // Mark as processing so other loops don't start\n bucket.processing = true\n\n while (bucket.waiting.length > 0) {\n logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} remaining.`)\n if (bucket.isRequestAllowed()) {\n bucket.requested++\n // Resolve the next item in the queue\n bucket.waiting.shift()?.()\n } else {\n await delay(1000)\n }\n }\n\n // Mark as false so next pending request can be triggered by new loop.\n bucket.processing = false\n },\n\n handleCompletedRequest: function (code, sharedScope) {\n // Since request is complete, we can remove one from requested.\n bucket.requested--\n // Since it is as a valid request, we don't need to do anything\n if (!bucket.errorStatuses.includes(code)) return\n // Shared scope is not considered invalid\n if (code === 429 && sharedScope) return\n\n // INVALID REQUEST WAS MADE\n\n // Mark a request has been invalid\n bucket.current++\n // If a timeout was not started, start a timeout to reset this bucket\n if (bucket.timeoutId === undefined) {\n bucket.timeoutId = setTimeout(() => {\n bucket.current = 0\n bucket.timeoutId = undefined\n }, bucket.interval)\n }\n },\n }\n\n return bucket\n}\n\nexport interface InvalidRequestBucketOptions {\n /** current invalid amount */\n current?: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max?: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval?: number\n /** timer to reset to 0 */\n timeoutId?: NodeJS.Timeout\n /** how safe to be from max. Defaults to 1 */\n safety?: number\n /** The request statuses that count as an invalid request. */\n errorStatuses?: number[]\n /** The amount of requests that were requested from this bucket. */\n requested?: number\n}\n\nexport interface InvalidRequestBucket {\n /** current invalid amount */\n current: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval: number\n /** timer to reset to 0 */\n timeoutId: NodeJS.Timeout | undefined\n /** how safe to be from max. Defaults to 1 */\n safety: number\n /** The request statuses that count as an invalid request. */\n errorStatuses: number[]\n /** The amount of requests that were requested from this bucket. */\n requested: number\n /** The requests that are currently pending. */\n waiting: Array<(value: void | PromiseLike<void>) => void>\n /** Whether or not the waiting queue is already processing. */\n processing: boolean\n\n /** Gives the number of requests that are currently allowed. */\n requestsAllowed: () => number\n /** Checks if a request is allowed at this time. */\n isRequestAllowed: () => boolean\n /** Waits until a request is available */\n waitUntilRequestAvailable: () => Promise<void>\n /** Begins processing the waiting queue of requests. */\n processWaiting: () => Promise<void>\n /** Handler for whenever a request is validated. This should update the requested values or trigger any other necessary stuff. */\n handleCompletedRequest: (code: number, sharedScope: boolean) => void\n}\n"],"names":["delay","logger","createInvalidRequestBucket","options","bucket","current","max","interval","timeoutId","safety","errorStatuses","requested","processing","waiting","requestsAllowed","isRequestAllowed","waitUntilRequestAvailable","Promise","resolve","push","processWaiting","length","info","shift","handleCompletedRequest","code","sharedScope","includes","undefined","setTimeout"],"mappings":"AAAA,SAASA,KAAK,EAAEC,MAAM,QAAQ,oBAAmB;AAEjD;;;;;;CAMC,GACD,OAAO,SAASC,2BAA2BC,OAAoC,EAAwB;IACrG,MAAMC,SAA+B;QACnCC,SAASF,QAAQE,OAAO,IAAI;QAC5BC,KAAKH,QAAQG,GAAG,IAAI;QACpBC,UAAUJ,QAAQI,QAAQ,IAAI;QAC9BC,WAAWL,QAAQK,SAAS;QAC5BC,QAAQN,QAAQM,MAAM,IAAI;QAC1BC,eAAeP,QAAQO,aAAa,IAAI;YAAC;YAAK;YAAK;SAAI;QACvDC,WAAWR,QAAQQ,SAAS,IAAI;QAChCC,YAAY,KAAK;QAEjBC,SAAS,EAAE;QAEXC,iBAAiB,WAAY;YAC3B,OAAOV,OAAOE,GAAG,GAAGF,OAAOC,OAAO,GAAGD,OAAOO,SAAS,GAAGP,OAAOK,MAAM;QACvE;QAEAM,kBAAkB,WAAY;YAC5B,OAAOX,OAAOU,eAAe,KAAK;QACpC;QAEAE,2BAA2B,iBAAkB;YAC3C,qDAAqD;YACrD,OAAO,MAAM,IAAIC,QAAQ,OAAOC,UAAY;gBAC1C,2FAA2F;gBAC3F,IAAId,OAAOW,gBAAgB,IAAI;oBAC7BX,OAAOO,SAAS;oBAChBO;gBACF,OAAO;oBACLd,OAAOS,OAAO,CAACM,IAAI,CAACD;oBACpB,MAAMd,OAAOgB,cAAc;gBAC7B,CAAC;YACH;QACF;QAEAA,gBAAgB,iBAAkB;YAChC,qEAAqE;YACrE,IAAIhB,OAAOQ,UAAU,EAAE;gBACrB;YACF,CAAC;YAED,gDAAgD;YAChDR,OAAOQ,UAAU,GAAG,IAAI;YAExB,MAAOR,OAAOS,OAAO,CAACQ,MAAM,GAAG,EAAG;gBAChCpB,OAAOqB,IAAI,CAAC,CAAC,6DAA6D,EAAElB,OAAOS,OAAO,CAACQ,MAAM,CAAC,WAAW,CAAC;gBAC9G,IAAIjB,OAAOW,gBAAgB,IAAI;oBAC7BX,OAAOO,SAAS;oBAChB,qCAAqC;oBACrCP,OAAOS,OAAO,CAACU,KAAK;gBACtB,OAAO;oBACL,MAAMvB,MAAM;gBACd,CAAC;YACH;YAEA,sEAAsE;YACtEI,OAAOQ,UAAU,GAAG,KAAK;QAC3B;QAEAY,wBAAwB,SAAUC,IAAI,EAAEC,WAAW,EAAE;YACnD,+DAA+D;YAC/DtB,OAAOO,SAAS;YAChB,+DAA+D;YAC/D,IAAI,CAACP,OAAOM,aAAa,CAACiB,QAAQ,CAACF,OAAO;YAC1C,yCAAyC;YACzC,IAAIA,SAAS,OAAOC,aAAa;YAEjC,2BAA2B;YAE3B,kCAAkC;YAClCtB,OAAOC,OAAO;YACd,qEAAqE;YACrE,IAAID,OAAOI,SAAS,KAAKoB,WAAW;gBAClCxB,OAAOI,SAAS,GAAGqB,WAAW,IAAM;oBAClCzB,OAAOC,OAAO,GAAG;oBACjBD,OAAOI,SAAS,GAAGoB;gBACrB,GAAGxB,OAAOG,QAAQ;YACpB,CAAC;QACH;IACF;IAEA,OAAOH;AACT,CAAC"}
1
+ {"version":3,"sources":["../src/invalidBucket.ts"],"sourcesContent":["import { delay, logger } from '@discordeno/utils'\n\n/**\n * A invalid request bucket is used in a similar manner as a leaky bucket but a invalid request bucket can be refilled as needed.\n * It's purpose is to make sure the bot does not hit the limit to getting a 1 hr ban.\n *\n * @param options The options used to configure this bucket.\n * @returns RefillingBucket\n */\nexport function createInvalidRequestBucket(options: InvalidRequestBucketOptions): InvalidRequestBucket {\n const bucket: InvalidRequestBucket = {\n invalidRequests: options.current ?? 0,\n max: options.max ?? 10000,\n interval: options.interval ?? 600_000, // 10 minutes\n resetAt: options.resetAt,\n safety: options.safety ?? 1,\n errorStatuses: options.errorStatuses ?? [401, 403, 429],\n activeRequests: options.requested ?? 0,\n processing: false,\n\n waiting: [],\n\n requestsAllowed: function () {\n if (bucket.resetAt !== undefined && Date.now() >= bucket.resetAt) {\n bucket.invalidRequests = 0\n bucket.resetAt = Date.now() + bucket.interval\n }\n\n return bucket.max - bucket.invalidRequests - bucket.activeRequests - bucket.safety\n },\n\n isRequestAllowed: function () {\n return bucket.requestsAllowed() > 0\n },\n\n waitUntilRequestAvailable: async function () {\n // eslint-disable-next-line no-async-promise-executor\n return await new Promise(async (resolve) => {\n // If whatever amount of requests is left is more than the safety margin, allow the request\n if (bucket.isRequestAllowed()) {\n bucket.activeRequests += 1\n resolve()\n } else {\n bucket.waiting.push(resolve)\n await bucket.processWaiting()\n }\n })\n },\n\n processWaiting: async function () {\n // If already processing, that loop will handle all waiting requests.\n if (bucket.processing) return\n\n // Mark as processing so other loops don't start\n bucket.processing = true\n\n while (bucket.waiting.length > 0) {\n logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} pending requests to be made. ${JSON.stringify(bucket)}`)\n\n if (!bucket.isRequestAllowed() && bucket.resetAt !== undefined) {\n logger.warn(`[InvalidBucket] processing waiting queue is now paused until more requests are available. ${bucket.waiting.length} pending requests. ${JSON.stringify(bucket)}`)\n await delay(bucket.resetAt - Date.now())\n }\n\n bucket.activeRequests += 1\n // Resolve the next item in the queue\n bucket.waiting.shift()?.()\n }\n\n // Mark as false so next pending request can be triggered by new loop.\n bucket.processing = false\n },\n\n handleCompletedRequest: function (code, sharedScope) {\n // Since request is complete, we can remove one from requested.\n bucket.activeRequests -= 1\n // Since it is as a valid request, we don't need to do anything\n if (!bucket.errorStatuses.includes(code)) return\n // Shared scope is not considered invalid\n if (code === 429 && sharedScope) return\n\n // INVALID REQUEST WAS MADE\n if (bucket.resetAt === undefined) {\n bucket.resetAt = Date.now() + bucket.interval\n }\n\n bucket.invalidRequests += 1\n logger.warn(`[InvalidBucket] an invalid request was made. Increasing invalidRequests count to ${bucket.invalidRequests}`)\n },\n }\n\n return bucket\n}\n\nexport interface InvalidRequestBucketOptions {\n /** current invalid amount */\n current?: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max?: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval?: number\n /** When the timeout for the bucket has started at. */\n resetAt?: number\n /** how safe to be from max. Defaults to 1 */\n safety?: number\n /** The request statuses that count as an invalid request. */\n errorStatuses?: number[]\n /** The amount of requests that were requested from this bucket. */\n requested?: number\n}\n\nexport interface InvalidRequestBucket {\n /** current invalid amount */\n invalidRequests: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval: number\n /** When the timeout for this bucket has started at. */\n resetAt: number | undefined\n /** how safe to be from max. Defaults to 1 */\n safety: number\n /** The request statuses that count as an invalid request. */\n errorStatuses: number[]\n /** The amount of requests that were requested from this bucket. */\n activeRequests: number\n /** The requests that are currently pending. */\n waiting: Array<(value: void | PromiseLike<void>) => void>\n /** Whether or not the waiting queue is already processing. */\n processing: boolean\n\n /** Gives the number of requests that are currently allowed. */\n requestsAllowed: () => number\n /** Checks if a request is allowed at this time. */\n isRequestAllowed: () => boolean\n /** Waits until a request is available */\n waitUntilRequestAvailable: () => Promise<void>\n /** Begins processing the waiting queue of requests. */\n processWaiting: () => Promise<void>\n /** Handler for whenever a request is validated. This should update the requested values or trigger any other necessary stuff. */\n handleCompletedRequest: (code: number, sharedScope: boolean) => void\n}\n"],"names":["delay","logger","createInvalidRequestBucket","options","bucket","invalidRequests","current","max","interval","resetAt","safety","errorStatuses","activeRequests","requested","processing","waiting","requestsAllowed","undefined","Date","now","isRequestAllowed","waitUntilRequestAvailable","Promise","resolve","push","processWaiting","length","info","JSON","stringify","warn","shift","handleCompletedRequest","code","sharedScope","includes"],"mappings":"AAAA,SAASA,KAAK,EAAEC,M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,CAAA;AACrC,eAAO,MAAM,eAAe,4BAA4B,CAAA;AAExD,eAAO,MAAM,uBAAuB,uBAAuB,CAAA;AAC3D,eAAO,MAAM,2BAA2B,0BAA0B,CAAA;AAClE,eAAO,MAAM,6BAA6B,4BAA4B,CAAA;AACtE,eAAO,MAAM,wBAAwB,uBAAuB,CAAA;AAC5D,eAAO,MAAM,wBAAwB,uBAAuB,CAAA;AAC5D,eAAO,MAAM,uBAAuB,sBAAsB,CAAA;AAC1D,eAAO,MAAM,uBAAuB,sBAAsB,CAAA;AAE1D,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CAknChF"}
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAkDA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;AAKrH,eAAO,MAAM,mBAAmB,KAAK,CAAA;AACrC,eAAO,MAAM,eAAe,4BAA4B,CAAA;AAExD,eAAO,MAAM,uBAAuB,uBAAuB,CAAA;AAC3D,eAAO,MAAM,2BAA2B,0BAA0B,CAAA;AAClE,eAAO,MAAM,6BAA6B,4BAA4B,CAAA;AACtE,eAAO,MAAM,wBAAwB,uBAAuB,CAAA;AAC5D,eAAO,MAAM,wBAAwB,uBAAuB,CAAA;AAC5D,eAAO,MAAM,uBAAuB,sBAAsB,CAAA;AAC1D,eAAO,MAAM,uBAAuB,sBAAsB,CAAA;AAE1D,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CAkoChF"}
package/dist/manager.js CHANGED
@@ -1,8 +1,7 @@
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 fetch from 'node-fetch';
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';
4
2
  import { createInvalidRequestBucket } from './invalidBucket.js';
5
3
  import { Queue } from './queue.js';
4
+ import { InteractionResponseTypes } from '@discordeno/types';
6
5
  import { createRoutes } from './routes.js';
7
6
  // TODO: make dynamic based on package.json file
8
7
  const version = '19.0.0-alpha.1';
@@ -61,11 +60,11 @@ export function createRestManager(options) {
61
60
  'permissions',
62
61
  'allow',
63
62
  'deny'
64
- ].includes(key)) {
63
+ ].includes(key) && obj[key] !== undefined) {
65
64
  newObj[key] = calculateBits(obj[key]);
66
65
  continue;
67
66
  }
68
- if (key === 'defaultMemberPermissions') {
67
+ if (key === 'defaultMemberPermissions' && obj[key] !== undefined) {
69
68
  newObj.default_member_permissions = calculateBits(obj[key]);
70
69
  continue;
71
70
  }
@@ -219,8 +218,20 @@ export function createRestManager(options) {
219
218
  authorization: 'Bot tokenhere'
220
219
  }
221
220
  });
222
- const response = await fetch(url, payload);
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
+ });
223
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');
224
235
  // Set the bucket id if it was available on the headers
225
236
  const bucketId = rest.processHeaders(rest.simplifyUrl(options.route, options.method), response.headers);
226
237
  if (bucketId) options.bucketId = bucketId;
@@ -247,8 +258,6 @@ export function createRestManager(options) {
247
258
  return;
248
259
  }
249
260
  options.retryCount += 1;
250
- // Rate limited, add back to queue
251
- rest.invalidBucket.handleCompletedRequest(response.status, response.headers.get(RATE_LIMIT_SCOPE_HEADER) === 'shared');
252
261
  const resetAfter = response.headers.get(RATE_LIMIT_RESET_AFTER_HEADER);
253
262
  if (resetAfter) await delay(Number(resetAfter) * 1000);
254
263
  // process the response to prevent mem leak
@@ -306,6 +315,11 @@ export function createRestManager(options) {
306
315
  },
307
316
  async makeRequest (method, route, options) {
308
317
  if (rest.isProxied) {
318
+ if (rest.authorization !== undefined) {
319
+ options ??= {};
320
+ options.headers ??= {};
321
+ options.headers.authorization = rest.authorization;
322
+ }
309
323
  const result = await fetch(`${rest.baseUrl}/v${rest.version}${route}`, rest.createRequestBody(method, options));
310
324
  if (!result.ok) {
311
325
  const err = await result.json().catch(()=>{});