@discordeno/utils 19.0.0-next.c32e90e → 19.0.0-next.c460c88

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 (48) hide show
  1. package/README.md +1 -1
  2. package/dist/bucket.d.ts +36 -48
  3. package/dist/bucket.d.ts.map +1 -1
  4. package/dist/bucket.js +72 -72
  5. package/dist/bucket.js.map +1 -1
  6. package/dist/builders/embeds.d.ts +146 -0
  7. package/dist/builders/embeds.d.ts.map +1 -0
  8. package/dist/builders/embeds.js +295 -0
  9. package/dist/builders/embeds.js.map +1 -0
  10. package/dist/builders.d.ts +4 -0
  11. package/dist/builders.d.ts.map +1 -0
  12. package/dist/builders.js +5 -0
  13. package/dist/builders.js.map +1 -0
  14. package/dist/casing.d.ts.map +1 -1
  15. package/dist/casing.js +2 -2
  16. package/dist/casing.js.map +1 -1
  17. package/dist/colors.d.ts.map +1 -1
  18. package/dist/colors.js +1 -5
  19. package/dist/colors.js.map +1 -1
  20. package/dist/images.js +2 -2
  21. package/dist/images.js.map +1 -1
  22. package/dist/index.d.ts +3 -2
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +3 -2
  25. package/dist/index.js.map +1 -1
  26. package/dist/logger.d.ts.map +1 -1
  27. package/dist/logger.js +8 -8
  28. package/dist/logger.js.map +1 -1
  29. package/dist/oauth2.d.ts +58 -0
  30. package/dist/oauth2.d.ts.map +1 -0
  31. package/dist/oauth2.js +16 -0
  32. package/dist/oauth2.js.map +1 -0
  33. package/dist/token.d.ts +1 -1
  34. package/dist/token.d.ts.map +1 -1
  35. package/dist/token.js +8 -3
  36. package/dist/token.js.map +1 -1
  37. package/dist/typeguards.d.ts +2 -1
  38. package/dist/typeguards.d.ts.map +1 -1
  39. package/dist/typeguards.js +3 -0
  40. package/dist/typeguards.js.map +1 -1
  41. package/dist/utils.d.ts.map +1 -1
  42. package/dist/utils.js +2 -1
  43. package/dist/utils.js.map +1 -1
  44. package/package.json +13 -12
  45. package/dist/files.d.ts +0 -4
  46. package/dist/files.d.ts.map +0 -1
  47. package/dist/files.js +0 -41
  48. package/dist/files.js.map +0 -1
package/README.md CHANGED
@@ -149,6 +149,6 @@ and unofficial templates:
149
149
 
150
150
  ## Links
151
151
 
152
- - [Website](https://discordeno.mod.land)
152
+ - [Website](https://discordeno.js.org/)
153
153
  - [Documentation](https://doc.deno.land/https/deno.land/x/discordeno/mod.ts)
154
154
  - [Discord](https://discord.com/invite/5vBgXk3UcZ)
package/dist/bucket.d.ts CHANGED
@@ -1,55 +1,43 @@
1
- import type { PickPartial } from '@discordeno/types';
2
- /** A Leaky Bucket.
3
- * Useful for rate limiting purposes.
4
- * This uses `performance.now()` instead of `Date.now()` for higher accuracy.
5
- *
6
- * NOTE: This bucket is lazy, means it only updates when a related method is called.
7
- */
8
- export interface LeakyBucket {
9
- /** How many tokens this bucket can hold. */
1
+ /// <reference types="node" />
2
+ export declare class LeakyBucket implements LeakyBucketOptions {
10
3
  max: number;
11
- /** Amount of tokens gained per interval.
12
- * If bigger than `max` it will be pressed to `max`.
13
- */
14
- refillAmount: number;
15
- /** Interval at which the bucket gains tokens. */
16
4
  refillInterval: number;
17
- /** Acquire tokens from the bucket.
18
- * Resolves when the tokens are acquired and available.
19
- * @param {boolean} [highPriority=false] Whether this acquire is should be done asap.
20
- */
21
- acquire: (amount: number, highPriority?: boolean) => Promise<void>;
22
- /** Returns the number of milliseconds until the next refill. */
23
- nextRefill: () => number;
24
- /** Current tokens in the bucket. */
25
- tokens: () => number;
26
- /** @private Internal track of when the last refill of tokens was.
27
- * DO NOT TOUCH THIS! Unless you know what you are doing ofc :P
28
- */
29
- lastRefill: number;
30
- /** @private Internal state of whether currently it is allowed to acquire tokens.
31
- * DO NOT TOUCH THIS! Unless you know what you are doing ofc :P
5
+ refillAmount: number;
6
+ /** The amount of requests that have been used up already. */
7
+ used: number;
8
+ /** The queue of requests to acquire an available request. Mapped by <shardId, resolve()> */
9
+ queue: Array<(value: void | PromiseLike<void>) => void>;
10
+ /** Whether or not the queue is already processing. */
11
+ processing: boolean;
12
+ /** The timeout id for the timer to reduce the used amount by the refill amount. */
13
+ timeoutId?: NodeJS.Timeout;
14
+ /** The timestamp in milliseconds when the next refill is scheduled. */
15
+ refillsAt?: number;
16
+ constructor(options?: LeakyBucketOptions);
17
+ /** The amount of requests that still remain. */
18
+ get remaining(): number;
19
+ /** Refills the bucket as needed. */
20
+ refillBucket(): void;
21
+ /** Begin processing the queue. */
22
+ processQueue(): Promise<void>;
23
+ /** Pauses the execution until the request is available to be made. */
24
+ acquire(highPriority?: boolean): Promise<void>;
25
+ }
26
+ export interface LeakyBucketOptions {
27
+ /**
28
+ * Max requests allowed at once.
29
+ * @default 1
32
30
  */
33
- allowAcquire: boolean;
34
- /** @private Internal number of currently available tokens.
35
- * DO NOT TOUCH THIS! Unless you know what you are doing ofc :P
31
+ max?: number;
32
+ /**
33
+ * Interval in milliseconds between refills.
34
+ * @default 5000
36
35
  */
37
- tokensState: number;
38
- /** @private Internal array of promises necessary to guarantee no race conditions.
39
- * DO NOT TOUCH THIS! Unless you know what you are doing ofc :P
36
+ refillInterval?: number;
37
+ /**
38
+ * Amount of requests to refill at each interval.
39
+ * @default 1
40
40
  */
41
- waiting: Array<(_?: unknown) => void>;
41
+ refillAmount?: number;
42
42
  }
43
- export declare function createLeakyBucket({ max, refillInterval, refillAmount, tokens, waiting, ...rest }: Omit<PickPartial<LeakyBucket, 'max' | 'refillInterval' | 'refillAmount'>, 'tokens'> & {
44
- /** Current tokens in the bucket.
45
- * @default max
46
- */
47
- tokens?: number;
48
- }): LeakyBucket;
49
- /** Update the tokens of that bucket.
50
- * @returns {number} The amount of current available tokens.
51
- */
52
- export declare function updateTokens(bucket: LeakyBucket): number;
53
- export declare function nextRefill(bucket: LeakyBucket): number;
54
- export declare function acquire(bucket: LeakyBucket, amount: number, highPriority?: boolean): Promise<void>;
55
43
  //# sourceMappingURL=bucket.d.ts.map
@@ -1 +1 @@
1
- {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AAM,EAAE,YAAY,UAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CA+CtG"}
1
+ {"version":3,"file":"bucket.d.ts","sourceRoot":"","sources":["../src/bucket.ts"],"names":[],"mappings":";AAGA,qBAAa,WAAY,YAAW,kBAAkB;IACpD,GAAG,EAAE,MAAM,CAAA;IACX,cAAc,EAAE,MAAM,CAAA;IACtB,YAAY,EAAE,MAAM,CAAA;IAEpB,6DAA6D;IAC7D,IAAI,EAAE,MAAM,CAAI;IAChB,4FAA4F;IAC5F,KAAK,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,CAAK;IAC5D,sDAAsD;IACtD,UAAU,EAAE,OAAO,CAAQ;IAC3B,mFAAmF;IACnF,SAAS,CAAC,EAAE,MAAM,CAAC,OAAO,CAAA;IAC1B,uEAAuE;IACvE,SAAS,CAAC,EAAE,MAAM,CAAA;gBAEN,OAAO,CAAC,EAAE,kBAAkB;IAMxC,gDAAgD;IAChD,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED,oCAAoC;IACpC,YAAY,IAAI,IAAI;IAkBpB,kCAAkC;IAC5B,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAmDnC,sEAAsE;IAChE,OAAO,CAAC,YAAY,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAWrD;AAED,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB"}
package/dist/bucket.js CHANGED
@@ -1,80 +1,80 @@
1
+ import logger from './logger.js';
1
2
  import { delay } from './utils.js';
2
- export function createLeakyBucket({ max , refillInterval , refillAmount , tokens , waiting , ...rest }) {
3
- return {
4
- max,
5
- refillInterval,
6
- refillAmount: refillAmount > max ? max : refillAmount,
7
- lastRefill: performance.now(),
8
- allowAcquire: true,
9
- nextRefill: function() {
10
- return nextRefill(this);
11
- },
12
- tokens: function() {
13
- return updateTokens(this);
14
- },
15
- acquire: async function(amount, highPriority) {
16
- return await acquire(this, amount, highPriority);
17
- },
18
- tokensState: tokens ?? max,
19
- waiting: waiting ?? [],
20
- ...rest
21
- };
22
- }
23
- /** Update the tokens of that bucket.
24
- * @returns {number} The amount of current available tokens.
25
- */ export function updateTokens(bucket) {
26
- const timePassed = performance.now() - bucket.lastRefill;
27
- const missedRefills = Math.floor(timePassed / bucket.refillInterval);
28
- // The refill shall not exceed the max amount of tokens.
29
- bucket.tokensState = Math.min(bucket.tokensState + bucket.refillAmount * missedRefills, bucket.max);
30
- bucket.lastRefill += bucket.refillInterval * missedRefills;
31
- return bucket.tokensState;
32
- }
33
- export function nextRefill(bucket) {
34
- // Since this bucket is lazy update the tokens before calculating the next refill.
35
- updateTokens(bucket);
36
- return performance.now() - bucket.lastRefill + bucket.refillInterval;
37
- }
38
- export async function acquire(bucket, amount, highPriority = false) {
39
- // To prevent the race condition of 2 acquires happening at once,
40
- // check whether its currently allowed to acquire.
41
- if (!bucket.allowAcquire) {
42
- // create, push, and wait until the current running acquiring is finished.
43
- await new Promise((resolve)=>{
44
- if (highPriority) {
45
- bucket.waiting.unshift(resolve);
46
- } else {
47
- bucket.waiting.push(resolve);
3
+ export class LeakyBucket {
4
+ constructor(options){
5
+ /** The amount of requests that have been used up already. */ this.used = 0;
6
+ /** The queue of requests to acquire an available request. Mapped by <shardId, resolve()> */ this.queue = [];
7
+ /** Whether or not the queue is already processing. */ this.processing = false;
8
+ this.max = options?.max ?? 1;
9
+ this.refillAmount = options?.refillAmount ? options.refillAmount > this.max ? this.max : options.refillAmount : 1;
10
+ this.refillInterval = options?.refillInterval ?? 5000;
11
+ }
12
+ /** The amount of requests that still remain. */ get remaining() {
13
+ return this.max < this.used ? 0 : this.max - this.used;
14
+ }
15
+ /** Refills the bucket as needed. */ refillBucket() {
16
+ logger.debug(`[LeakyBucket] Timeout for leaky bucket requests executed. Refilling bucket.`);
17
+ // Lower the used amount by the refill amount
18
+ this.used = this.refillAmount > this.used ? 0 : this.used - this.refillAmount;
19
+ // Reset the refillsAt timestamp since it just got refilled
20
+ this.refillsAt = undefined;
21
+ // Reset the timeoutId
22
+ clearTimeout(this.timeoutId);
23
+ this.timeoutId = undefined;
24
+ if (this.used > 0) {
25
+ this.timeoutId = setTimeout(()=>{
26
+ this.refillBucket();
27
+ }, this.refillInterval);
28
+ this.refillsAt = Date.now() + this.refillInterval;
29
+ }
30
+ }
31
+ /** Begin processing the queue. */ async processQueue() {
32
+ logger.debug('[LeakyBucket] Processing queue');
33
+ // There is already a queue that is processing
34
+ if (this.processing) return logger.debug('[LeakyBucket] Queue is already processing.');
35
+ this.processing = true;
36
+ // Begin going through the queue.
37
+ while(this.queue.length){
38
+ if (this.remaining) {
39
+ logger.debug(`[LeakyBucket] Processing queue. Remaining: ${this.remaining} Length: ${this.queue.length}`);
40
+ // Resolves the promise allowing the paused execution of this request to resolve and continue.
41
+ this.queue.shift()?.();
42
+ // A request can be made
43
+ this.used++;
44
+ // Create a new timeout for this request if none exists.
45
+ if (!this.timeoutId) {
46
+ logger.debug(`[LeakyBucket] Creating new timeout for leaky bucket requests.`);
47
+ this.timeoutId = setTimeout(()=>{
48
+ this.refillBucket();
49
+ }, this.refillInterval);
50
+ // Set the time for when this refill will occur.
51
+ this.refillsAt = Date.now() + this.refillInterval;
52
+ }
53
+ } else if (this.refillsAt) {
54
+ const now = Date.now();
55
+ // If there is time left until next refill, just delay execution.
56
+ if (this.refillsAt > now) {
57
+ logger.debug(`[LeakyBucket] Delaying execution of leaky bucket requests for ${this.refillsAt - now}ms`);
58
+ await delay(this.refillsAt - now);
59
+ logger.debug(`[LeakyBucket] Resuming execution`);
60
+ } else {
61
+ logger.debug(`[LeakyBucket] Delaying execution of leaky bucket requests for 1000ms`);
62
+ await delay(1000);
63
+ }
48
64
  }
49
- });
50
- // Somehow another acquire has started,
51
- // so need to wait again.
52
- if (!bucket.allowAcquire) {
53
- return await acquire(bucket, amount);
54
65
  }
66
+ // Loop has ended mark false so it can restart later when needed
67
+ this.processing = false;
55
68
  }
56
- bucket.allowAcquire = false;
57
- // Since the bucket is lazy update the tokens now,
58
- // and also get the current amount of available tokens
59
- const currentTokens = updateTokens(bucket);
60
- // It's possible that more than available tokens have been acquired,
61
- // so calculate the amount of milliseconds to wait until this acquire is good to go.
62
- if (currentTokens < amount) {
63
- const tokensNeeded = amount - currentTokens;
64
- const refillsNeeded = Math.ceil(tokensNeeded / bucket.refillAmount);
65
- const waitTime = bucket.refillInterval * refillsNeeded;
66
- await delay(waitTime);
67
- // Update the tokens again to ensure nothing has been missed.
68
- updateTokens(bucket);
69
+ /** Pauses the execution until the request is available to be made. */ async acquire(highPriority) {
70
+ return await new Promise((resolve)=>{
71
+ // High priority requests get added to the start of the queue
72
+ if (highPriority) this.queue.unshift(resolve);
73
+ else this.queue.push(resolve);
74
+ // Each request should trigger the queue to be processed.
75
+ void this.processQueue();
76
+ });
69
77
  }
70
- // In order to not subtract too much from the tokens,
71
- // calculate what is actually needed to subtract.
72
- const toSubtract = (amount % bucket.refillAmount) ?? amount;
73
- bucket.tokensState -= toSubtract;
74
- // Allow the next acquire to happen.
75
- bucket.allowAcquire = true;
76
- // If there is an acquire waiting, let it continue.
77
- bucket.waiting.shift()?.();
78
78
  }
79
79
 
80
80
  //# sourceMappingURL=bucket.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/bucket.ts"],"sourcesContent":["import type { PickPartial } from '@discordeno/types'\nimport { delay } from './utils.js'\n\n/** A Leaky Bucket.\n * Useful for rate limiting purposes.\n * This uses `performance.now()` instead of `Date.now()` for higher accuracy.\n *\n * NOTE: This bucket is lazy, means it only updates when a related method is called.\n */\nexport interface LeakyBucket {\n /** How many tokens this bucket can hold. */\n max: number\n /** Amount of tokens gained per interval.\n * If bigger than `max` it will be pressed to `max`.\n */\n refillAmount: number\n /** Interval at which the bucket gains tokens. */\n refillInterval: number\n\n /** Acquire tokens from the bucket.\n * Resolves when the tokens are acquired and available.\n * @param {boolean} [highPriority=false] Whether this acquire is should be done asap.\n */\n acquire: (amount: number, highPriority?: boolean) => Promise<void>\n\n /** Returns the number of milliseconds until the next refill. */\n nextRefill: () => number\n\n /** Current tokens in the bucket. */\n tokens: () => number\n\n /** @private Internal track of when the last refill of tokens was.\n * DO NOT TOUCH THIS! Unless you know what you are doing ofc :P\n */\n lastRefill: number\n\n /** @private Internal state of whether currently it is allowed to acquire tokens.\n * DO NOT TOUCH THIS! Unless you know what you are doing ofc :P\n */\n allowAcquire: boolean\n\n /** @private Internal number of currently available tokens.\n * DO NOT TOUCH THIS! Unless you know what you are doing ofc :P\n */\n tokensState: number\n\n /** @private Internal array of promises necessary to guarantee no race conditions.\n * DO NOT TOUCH THIS! Unless you know what you are doing ofc :P\n */\n waiting: Array<(_?: unknown) => void>\n}\n\nexport function createLeakyBucket({\n max,\n refillInterval,\n refillAmount,\n tokens,\n waiting,\n ...rest\n}: Omit<PickPartial<LeakyBucket, 'max' | 'refillInterval' | 'refillAmount'>, 'tokens'> & {\n /** Current tokens in the bucket.\n * @default max\n */\n tokens?: number\n}): LeakyBucket {\n return {\n max,\n refillInterval,\n refillAmount: refillAmount > max ? max : refillAmount,\n lastRefill: performance.now(),\n allowAcquire: true,\n\n nextRefill: function () {\n return nextRefill(this)\n },\n\n tokens: function () {\n return updateTokens(this)\n },\n\n acquire: async function (amount, highPriority) {\n return await acquire(this, amount, highPriority)\n },\n\n tokensState: tokens ?? max,\n waiting: waiting ?? [],\n\n ...rest,\n }\n}\n\n/** Update the tokens of that bucket.\n * @returns {number} The amount of current available tokens.\n */\nexport function updateTokens(bucket: LeakyBucket): number {\n const timePassed = performance.now() - bucket.lastRefill\n const missedRefills = Math.floor(timePassed / bucket.refillInterval)\n\n // The refill shall not exceed the max amount of tokens.\n bucket.tokensState = Math.min(bucket.tokensState + bucket.refillAmount * missedRefills, bucket.max)\n bucket.lastRefill += bucket.refillInterval * missedRefills\n\n return bucket.tokensState\n}\n\nexport function nextRefill(bucket: LeakyBucket): number {\n // Since this bucket is lazy update the tokens before calculating the next refill.\n updateTokens(bucket)\n\n return performance.now() - bucket.lastRefill + bucket.refillInterval\n}\n\nexport async function acquire(bucket: LeakyBucket, amount: number, highPriority = false): Promise<void> {\n // To prevent the race condition of 2 acquires happening at once,\n // check whether its currently allowed to acquire.\n if (!bucket.allowAcquire) {\n // create, push, and wait until the current running acquiring is finished.\n await new Promise((resolve) => {\n if (highPriority) {\n bucket.waiting.unshift(resolve)\n } else {\n bucket.waiting.push(resolve)\n }\n })\n\n // Somehow another acquire has started,\n // so need to wait again.\n if (!bucket.allowAcquire) {\n return await acquire(bucket, amount)\n }\n }\n\n bucket.allowAcquire = false\n // Since the bucket is lazy update the tokens now,\n // and also get the current amount of available tokens\n const currentTokens = updateTokens(bucket)\n\n // It's possible that more than available tokens have been acquired,\n // so calculate the amount of milliseconds to wait until this acquire is good to go.\n if (currentTokens < amount) {\n const tokensNeeded = amount - currentTokens\n const refillsNeeded = Math.ceil(tokensNeeded / bucket.refillAmount)\n\n const waitTime = bucket.refillInterval * refillsNeeded\n await delay(waitTime)\n\n // Update the tokens again to ensure nothing has been missed.\n updateTokens(bucket)\n }\n\n // In order to not subtract too much from the tokens,\n // calculate what is actually needed to subtract.\n const toSubtract = amount % bucket.refillAmount ?? amount\n bucket.tokensState -= toSubtract\n\n // Allow the next acquire to happen.\n bucket.allowAcquire = true\n // If there is an acquire waiting, let it continue.\n bucket.waiting.shift()?.()\n}\n"],"names":["delay","createLeakyBucket","max","refillInterval","refillAmount","tokens","waiting","rest","lastRefill","performance","now","allowAcquire","nextRefill","updateTokens","acquire","amount","highPriority","tokensState","bucket","timePassed","missedRefills","Math","floor","min","Promise","resolve","unshift","push","currentTokens","tokensNeeded","refillsNeeded","ceil","waitTime","toSubtract","shift"],"mappings":"AACA,SAASA,KAAK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Ia,QAAQ,CAACC,UAAY;YAC7B,IAAIT,cAAc;gBAChBE,OAAOZ,OAAO,CAACoB,OAAO,CAACD;YACzB,OAAO;gBACLP,OAAOZ,OAAO,CAACqB,IAAI,CAACF;YACtB,CAAC;QACH;QAEA,uCAAuC;QACvC,yBAAyB;QACzB,IAAI,CAACP,OAAOP,YAAY,EAAE;YACxB,OAAO,MAAMG,QAAQI,QAAQH;QAC/B,CAAC;IACH,CAAC;IAEDG,OAAOP,YAAY,GAAG,KAAK;IAC3B,kDAAkD;IAClD,sDAAsD;IACtD,MAAMiB,gBAAgBf,aAAaK;IAEnC,oEAAoE;IACpE,oFAAoF;IACpF,IAAIU,gBAAgBb,QAAQ;QAC1B,MAAMc,eAAed,SAASa;QAC9B,MAAME,gBAAgBT,KAAKU,IAAI,CAACF,eAAeX,OAAOd,YAAY;QAElE,MAAM4B,WAAWd,OAAOf,cAAc,GAAG2B;QACzC,MAAM9B,MAAMgC;QAEZ,6DAA6D;QAC7DnB,aAAaK;IACf,CAAC;IAED,qDAAqD;IACrD,iDAAiD;IACjD,MAAMe,aAAalB,CAAAA,SAASG,OAAOd,YAAY,AAAD,KAAKW;IACnDG,OAAOD,WAAW,IAAIgB;IAEtB,oCAAoC;IACpCf,OAAOP,YAAY,GAAG,IAAI;IAC1B,mDAAmD;IACnDO,OAAOZ,OAAO,CAAC4B,KAAK;AACtB,CAAC"}
1
+ {"version":3,"sources":["../src/bucket.ts"],"sourcesContent":["import logger from './logger.js'\nimport { delay } from './utils.js'\n\nexport class LeakyBucket implements LeakyBucketOptions {\n max: number\n refillInterval: number\n refillAmount: number\n\n /** The amount of requests that have been used up already. */\n used: number = 0\n /** The queue of requests to acquire an available request. Mapped by <shardId, resolve()> */\n queue: Array<(value: void | PromiseLike<void>) => void> = []\n /** Whether or not the queue is already processing. */\n processing: boolean = false\n /** The timeout id for the timer to reduce the used amount by the refill amount. */\n timeoutId?: NodeJS.Timeout\n /** The timestamp in milliseconds when the next refill is scheduled. */\n refillsAt?: number\n\n constructor(options?: LeakyBucketOptions) {\n this.max = options?.max ?? 1\n this.refillAmount = options?.refillAmount ? (options.refillAmount > this.max ? this.max : options.refillAmount) : 1\n this.refillInterval = options?.refillInterval ?? 5000\n }\n\n /** The amount of requests that still remain. */\n get remaining(): number {\n return this.max < this.used ? 0 : this.max - this.used\n }\n\n /** Refills the bucket as needed. */\n refillBucket(): void {\n logger.debug(`[LeakyBucket] Timeout for leaky bucket requests executed. Refilling bucket.`)\n // Lower the used amount by the refill amount\n this.used = this.refillAmount > this.used ? 0 : this.used - this.refillAmount\n // Reset the refillsAt timestamp since it just got refilled\n this.refillsAt = undefined\n // Reset the timeoutId\n clearTimeout(this.timeoutId)\n this.timeoutId = undefined\n\n if (this.used > 0) {\n this.timeoutId = setTimeout(() => {\n this.refillBucket()\n }, this.refillInterval)\n this.refillsAt = Date.now() + this.refillInterval\n }\n }\n\n /** Begin processing the queue. */\n async processQueue(): Promise<void> {\n logger.debug('[LeakyBucket] Processing queue')\n\n // There is already a queue that is processing\n if (this.processing) return logger.debug('[LeakyBucket] Queue is already processing.')\n\n this.processing = true\n\n // Begin going through the queue.\n while (this.queue.length) {\n if (this.remaining) {\n logger.debug(`[LeakyBucket] Processing queue. Remaining: ${this.remaining} Length: ${this.queue.length}`)\n // Resolves the promise allowing the paused execution of this request to resolve and continue.\n this.queue.shift()?.()\n // A request can be made\n this.used++\n\n // Create a new timeout for this request if none exists.\n if (!this.timeoutId) {\n logger.debug(`[LeakyBucket] Creating new timeout for leaky bucket requests.`)\n\n this.timeoutId = setTimeout(() => {\n this.refillBucket()\n }, this.refillInterval)\n // Set the time for when this refill will occur.\n this.refillsAt = Date.now() + this.refillInterval\n }\n }\n\n // Check if a refill is scheduled, since we have used up all available requests\n else if (this.refillsAt) {\n const now = Date.now()\n // If there is time left until next refill, just delay execution.\n if (this.refillsAt > now) {\n logger.debug(`[LeakyBucket] Delaying execution of leaky bucket requests for ${this.refillsAt - now}ms`)\n await delay(this.refillsAt - now)\n logger.debug(`[LeakyBucket] Resuming execution`)\n }\n\n // If the refillsAt has passed but the timeout didn't yet execute delay the execution\n else {\n logger.debug(`[LeakyBucket] Delaying execution of leaky bucket requests for 1000ms`)\n await delay(1000)\n }\n }\n }\n\n // Loop has ended mark false so it can restart later when needed\n this.processing = false\n }\n\n /** Pauses the execution until the request is available to be made. */\n async acquire(highPriority?: boolean): Promise<void> {\n return await new Promise((resolve) => {\n // High priority requests get added to the start of the queue\n if (highPriority) this.queue.unshift(resolve)\n // All other requests get pushed to the end.\n else this.queue.push(resolve)\n\n // Each request should trigger the queue to be processed.\n void this.processQueue()\n })\n }\n}\n\nexport interface LeakyBucketOptions {\n /**\n * Max requests allowed at once.\n * @default 1\n */\n max?: number\n /**\n * Interval in milliseconds between refills.\n * @default 5000\n */\n refillInterval?: number\n /**\n * Amount of requests to refill at each interval.\n * @default 1\n */\n refillAmount?: number\n}\n"],"names":["logger","delay","LeakyBucket","constructor","options","used","queue","processing","max","refillAmount","refillInterval","remaining","refillBucket","debug","refillsAt","undefined","clearTimeout","timeoutId","setTimeout","Date","now","processQueue","length","shift","acquire","highPriority","Promise","resolve","unshift","push"],"mappings":"AAAA,OAAOA,YAAY,cAAa;AAChC,SAASC,KAAK,QAAQ,aAAY;AAElC,OAAO,MAAMC;IAgBXC,YAAYC,OAA4B,CAAE;QAX1C,2DAA2D,QAC3DC,OAAe;QACf,0FAA0F,QAC1FC,QAA0D,EAAE;QAC5D,oDAAoD,QACpDC,aAAsB,KAAK;QAOzB,IAAI,CAACC,GAAG,GAAGJ,SAASI,OAAO;QAC3B,IAAI,CAACC,YAAY,GAAGL,SAASK,eAAgBL,QAAQK,YAAY,GAAG,IAAI,CAACD,GAAG,GAAG,IAAI,CAACA,GAAG,GAAGJ,QAAQK,YAAY,GAAI,CAAC;QACnH,IAAI,CAACC,cAAc,GAAGN,SAASM,kBAAkB;IACnD;IAEA,8CAA8C,GAC9C,IAAIC,YAAoB;QACtB,OAAO,IAAI,CAACH,G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BjB,OAAOa,KAAK,CAAC,CAAC,6DAA6D,CAAC;oBAE5E,IAAI,CAACI,SAAS,GAAGC,WAAW,IAAM;wBAChC,IAAI,CAACN,YAAY;oBACnB,GAAG,IAAI,CAACF,cAAc;oBACtB,gDAAgD;oBAChD,IAAI,CAACI,SAAS,GAAGK,KAAKC,GAAG,KAAK,IAAI,CAACV,cAAc;gBACnD,CAAC;YACH,OAGK,IAAI,IAAI,CAACI,SAAS,EAAE;gBACvB,MAAMM,MAAMD,KAAKC,GAAG;gBACpB,iEAAiE;gBACjE,IAAI,IAAI,CAACN,SAAS,GAAGM,KAAK;oBACxBpB,OAAOa,KAAK,CAAC,CAAC,8DAA8D,EAAE,IAAI,CAACC,SAAS,GAAGM,IAAI,EAAE,CAAC;oBACtG,MAAMnB,MAAM,IAAI,CAACa,SAAS,GAAGM;oBAC7BpB,OAAOa,KAAK,CAAC,CAAC,gCAAgC,CAAC;gBACjD,OAGK;oBACHb,OAAOa,KAAK,CAAC,CAAC,oEAAoE,CAAC;oBACnF,MAAMZ,MAAM;gBACd,CAAC;YACH,CAAC;QACH;QAEA,gEAAgE;QAChE,IAAI,CAACM,UAAU,GAAG,KAAK;IACzB;IAEA,oEAAoE,GACpE,MAAMiB,QAAQC,YAAsB,EAAiB;QACnD,OAAO,MAAM,IAAIC,QAAQ,CAACC,UAAY;YACpC,6DAA6D;YAC7D,IAAIF,cAAc,IAAI,CAACnB,KAAK,CAACsB,OAAO,CAACD;iBAEhC,IAAI,CAACrB,KAAK,CAACuB,IAAI,CAACF;YAErB,yDAAyD;YACzD,KAAK,IAAI,CAACN,YAAY;QACxB;IACF;AACF,CAAC"}
@@ -0,0 +1,146 @@
1
+ import type { DiscordEmbed, DiscordEmbedAuthor, DiscordEmbedField, DiscordEmbedFooter, DiscordEmbedImage, DiscordEmbedThumbnail, DiscordEmbedVideo } from '@discordeno/types';
2
+ /**
3
+ * A builder to help create Discord embeds.
4
+ *
5
+ * @export
6
+ * @class EmbedsBuilder
7
+ * @typedef {EmbedsBuilder}
8
+ * @extends {Array<DiscordEmbed>}
9
+ * @example
10
+ * const embeds = new EmbedBuilder()
11
+ * .setTitle('My Embed')
12
+ * .setDescription('This is my new embed')
13
+ * .newEmbed()
14
+ * .setTitle('My Second Embed')
15
+ */
16
+ export declare class EmbedsBuilder extends Array<DiscordEmbed> {
17
+ #private;
18
+ /**
19
+ * Adds a new field to the embed fields array.
20
+ *
21
+ * @param {string} name - Field name
22
+ * @param {string} value - Field value
23
+ * @param {?boolean} [inline=false] - Field should be inline or not.
24
+ * @returns {EmbedsBuilder}
25
+ */
26
+ addField(name: string, value: string, inline?: boolean): EmbedsBuilder;
27
+ /**
28
+ * Creates a blank embed.
29
+ *
30
+ * @returns {EmbedsBuilder}
31
+ */
32
+ newEmbed(): EmbedsBuilder;
33
+ /**
34
+ * Set the current embed author.
35
+ *
36
+ * @param {string} name - Name of the author
37
+ * @param {?Omit<DiscordEmbedAuthor, 'name'>} [options] - Extra author options
38
+ * @returns {EmbedsBuilder}
39
+ */
40
+ setAuthor(name: string, options?: Omit<DiscordEmbedAuthor, 'name'>): EmbedsBuilder;
41
+ /**
42
+ * Set the color on the side of the current embed.
43
+ *
44
+ * @param {(number | string)} color - The color, in base16 or hex color code
45
+ * @returns {EmbedsBuilder}
46
+ */
47
+ setColor(color: number | string): EmbedsBuilder;
48
+ /**
49
+ * Set the current embed to a different index.
50
+ *
51
+ * WARNING: Only use this method if you know what you're doing. Make sure to set it back to the latest when you're done.
52
+ *
53
+ * @param {?number} [index] - The index of the embed in the EmbedsBuilder array
54
+ * @returns {EmbedsBuilder}
55
+ */
56
+ setCurrentEmbed(index?: number): EmbedsBuilder;
57
+ /**
58
+ * Set the description of the current embed.
59
+ *
60
+ * @param {string} description - Description
61
+ * @returns {EmbedsBuilder}
62
+ */
63
+ setDescription(description: string): EmbedsBuilder;
64
+ /**
65
+ * Overwrite all fields on the current embed.
66
+ *
67
+ * @param {DiscordEmbedField[]} fields
68
+ * @returns {EmbedsBuilder}
69
+ */
70
+ setFields(fields: DiscordEmbedField[]): EmbedsBuilder;
71
+ /**
72
+ * Set the footer in the current embed.
73
+ *
74
+ * @param {string} text - The text to display in the footer
75
+ * @param {?Omit<DiscordEmbedFooter, 'text'>} [options]
76
+ * @returns {EmbedsBuilder}
77
+ */
78
+ setFooter(text: string, options?: Omit<DiscordEmbedFooter, 'text'>): EmbedsBuilder;
79
+ /**
80
+ * Set the image in the current embed.
81
+ *
82
+ * @param {string} url - URL of the image
83
+ * @param {?Omit<DiscordEmbedImage, 'url'>} [options]
84
+ * @returns {EmbedsBuilder}
85
+ */
86
+ setImage(url: string, options?: Omit<DiscordEmbedImage, 'url'>): EmbedsBuilder;
87
+ /**
88
+ * Set the provider of the current embed.
89
+ *
90
+ * @param {string} name
91
+ * @param {?string} [url]
92
+ * @returns {EmbedsBuilder}
93
+ */
94
+ setProvider(name: string, url?: string): EmbedsBuilder;
95
+ /**
96
+ * Set the color of the current embed to a random value.
97
+ *
98
+ * @returns {EmbedsBuilder}
99
+ */
100
+ setRandomColor(): EmbedsBuilder;
101
+ /**
102
+ * Set the title of the current embed.
103
+ *
104
+ * @param {string} title
105
+ * @param {?string} [url]
106
+ * @returns {EmbedsBuilder}
107
+ */
108
+ setTitle(title: string, url?: string): EmbedsBuilder;
109
+ /**
110
+ * Set the timestamp of the current embed.
111
+ *
112
+ * @param {?(string | number | Date)} [timestamp]
113
+ * @returns {EmbedsBuilder}
114
+ */
115
+ setTimestamp(timestamp?: string | number | Date): EmbedsBuilder;
116
+ /**
117
+ * Set the thumbnail of the current embed.
118
+ *
119
+ * @param {string} url - URL of the image
120
+ * @param {?Omit<DiscordEmbedThumbnail, 'url'>} [options]
121
+ * @returns {EmbedsBuilder}
122
+ */
123
+ setThumbnail(url: string, options?: Omit<DiscordEmbedThumbnail, 'url'>): EmbedsBuilder;
124
+ /**
125
+ * Set the URL of the current embed title.
126
+ *
127
+ * @param {string} url
128
+ * @returns {EmbedsBuilder}
129
+ */
130
+ setUrl(url: string): EmbedsBuilder;
131
+ /**
132
+ * Set the video of the current embed.
133
+ *
134
+ * @param {string} url
135
+ * @param {?Omit<DiscordEmbedVideo, 'url'>} [options]
136
+ * @returns {EmbedsBuilder}
137
+ */
138
+ setVideo(url: string, options?: Omit<DiscordEmbedVideo, 'url'>): EmbedsBuilder;
139
+ /**
140
+ * Validate all embeds available against current known Discord limits to help prevent bad requests.
141
+ *
142
+ * @returns {EmbedsBuilder}
143
+ */
144
+ validate(): EmbedsBuilder;
145
+ }
146
+ //# sourceMappingURL=embeds.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"embeds.d.ts","sourceRoot":"","sources":["../../src/builders/embeds.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,qBAAqB,EACrB,iBAAiB,EAClB,MAAM,mBAAmB,CAAA;AAE1B;;;;;;;;;;;;;GAaG;AACH,qBAAa,aAAc,SAAQ,KAAK,CAAC,YAAY,CAAC;;IAGpD;;;;;;;OAOG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,aAAa;IActE;;;;OAIG;IACH,QAAQ,IAAI,aAAa;IAWzB;;;;;;OAMG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,kBAAkB,EAAE,MAAM,CAAC,GAAG,aAAa;IAUlF;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,aAAa;IAW/C;;;;;;;OAOG;IACH,eAAe,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,aAAa;IAgB9C;;;;;OAKG;IACH,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,aAAa;IAMlD;;;;;OAKG;IACH,SAAS,CAAC,MAAM,EAAE,iBAAiB,EAAE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