lunibee 0.1.7 → 0.2.0

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 (78) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +225 -223
  3. package/dist/builders/commands.d.ts +6 -4
  4. package/dist/builders/components.d.ts +23 -0
  5. package/dist/builders/index.d.ts +2 -2
  6. package/dist/builders/index.js +120 -24
  7. package/dist/builders/index.js.map +9 -8
  8. package/dist/collection/index.d.ts +68 -1
  9. package/dist/collection/index.js +335 -1
  10. package/dist/collection/index.js.map +4 -4
  11. package/dist/core/collector.d.ts +13 -0
  12. package/dist/core/events.d.ts +112 -2
  13. package/dist/core/index.d.ts +30 -93
  14. package/dist/core/index.js +3386 -957
  15. package/dist/core/index.js.map +48 -24
  16. package/dist/core/permissions.d.ts +48 -0
  17. package/dist/formatters/index.js.map +1 -1
  18. package/dist/handlers/index.js.map +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.js +3806 -1193
  21. package/dist/index.js.map +64 -43
  22. package/dist/managers/advanced.d.ts +116 -0
  23. package/dist/managers/base.d.ts +13 -2
  24. package/dist/managers/emoji.d.ts +1 -1
  25. package/dist/managers/guild-resources.d.ts +109 -0
  26. package/dist/managers/guild.d.ts +33 -2
  27. package/dist/managers/index.d.ts +44 -6
  28. package/dist/managers/index.js +1186 -150
  29. package/dist/managers/index.js.map +27 -21
  30. package/dist/managers/message.d.ts +10 -1
  31. package/dist/rest/decoder.d.ts +23 -0
  32. package/dist/rest/errors.d.ts +53 -0
  33. package/dist/rest/index.d.ts +21 -17
  34. package/dist/rest/index.js +526 -264
  35. package/dist/rest/index.js.map +14 -8
  36. package/dist/rest/limiter.d.ts +42 -0
  37. package/dist/rest/redis.d.ts +18 -1
  38. package/dist/rest/route.d.ts +31 -0
  39. package/dist/rest/routes.d.ts +22 -0
  40. package/dist/rest/scheduler.d.ts +42 -0
  41. package/dist/rest/store.d.ts +29 -0
  42. package/dist/rest/transport.d.ts +40 -0
  43. package/dist/sharding/bus.d.ts +31 -0
  44. package/dist/sharding/cluster.d.ts +10 -0
  45. package/dist/sharding/index.d.ts +39 -2
  46. package/dist/sharding/index.js +1105 -404
  47. package/dist/sharding/index.js.map +18 -8
  48. package/dist/structures/base.d.ts +10 -0
  49. package/dist/structures/channels.d.ts +56 -0
  50. package/dist/structures/index.d.ts +1 -0
  51. package/dist/structures/index.js +305 -311
  52. package/dist/structures/index.js.map +18 -14
  53. package/dist/structures/interactions.d.ts +38 -52
  54. package/dist/structures/options.d.ts +55 -0
  55. package/dist/structures/resources.d.ts +3 -3
  56. package/dist/types/gateway-events.d.ts +132 -0
  57. package/dist/types/gateway.d.ts +197 -0
  58. package/dist/types/index.d.ts +53 -260
  59. package/dist/types/index.js +44 -2
  60. package/dist/types/index.js.map +5 -4
  61. package/dist/utils/index.js.map +1 -1
  62. package/dist/voice/index.d.ts +32 -1
  63. package/dist/voice/index.js +73 -7
  64. package/dist/voice/index.js.map +3 -3
  65. package/dist/ws/close-codes.d.ts +23 -0
  66. package/dist/ws/decoder.d.ts +58 -0
  67. package/dist/ws/heartbeat.d.ts +87 -0
  68. package/dist/ws/index.d.ts +18 -65
  69. package/dist/ws/index.js +911 -385
  70. package/dist/ws/index.js.map +15 -5
  71. package/dist/ws/opcodes.d.ts +14 -0
  72. package/dist/ws/protocol.d.ts +110 -0
  73. package/dist/ws/reconnect.d.ts +108 -0
  74. package/dist/ws/send-budget.d.ts +17 -0
  75. package/dist/ws/session.d.ts +100 -0
  76. package/dist/ws/state.d.ts +27 -0
  77. package/dist/ws/transport.d.ts +80 -0
  78. package/package.json +21 -20
@@ -1,4 +1,12 @@
1
1
  import { Collection } from "@lunibee/collection";
2
+ /** Opt-in per-channel message cache. Messages are temporary data, so they
3
+ * use bounded TTL/LRU storage rather than permanent resource storage. */
4
+ export interface MessageCacheOptions {
5
+ /** Messages kept per channel; least-recently-used are evicted. Default 100. */
6
+ maxSize?: number;
7
+ /** Sliding TTL in ms. Omit for no expiry. */
8
+ ttl?: number;
9
+ }
2
10
  import { REST } from "@lunibee/rest";
3
11
  import { Message, type ResourceContext } from "@lunibee/structures";
4
12
  export type MessageCreateOptions = Record<string, unknown> & {
@@ -9,8 +17,9 @@ export type MessageEditOptions = Record<string, unknown> & {
9
17
  };
10
18
  export declare class MessageManager {
11
19
  #private;
20
+ /** Cached messages; always empty unless a message cache is configured. */
12
21
  readonly cache: Collection<string, Message>;
13
- constructor(rest: REST, context: ResourceContext, channelId: string);
22
+ constructor(rest: REST, context: ResourceContext, channelId: string, cache?: MessageCacheOptions);
14
23
  resolve(messageId: string): Promise<Message>;
15
24
  send(options: MessageCreateOptions): Promise<Message>;
16
25
  fetch(messageId: string): Promise<Message>;
@@ -0,0 +1,23 @@
1
+ /** Normalised shape of a Discord error payload. */
2
+ export interface DecodedError {
3
+ message?: string;
4
+ code?: number;
5
+ retry_after?: number;
6
+ global?: boolean;
7
+ }
8
+ /** Turns a raw {@link Response} into a payload and the error metadata retries need. */
9
+ export declare class ResponseDecoder {
10
+ /** Reads a Discord response payload as JSON, text, or nothing for 204. */
11
+ read(response: Response): Promise<unknown>;
12
+ /** Extracts Discord's error metadata from a decoded payload. */
13
+ errorData(payload: unknown): DecodedError;
14
+ /**
15
+ * Resolves Retry-After (seconds) from the payload, falling back to the
16
+ * header and finally to one second.
17
+ *
18
+ * An absent header is explicitly *not* a zero delay: `Number(null)` is 0,
19
+ * which is finite, so reading it directly would answer a 429 that carries
20
+ * no Retry-After by retrying immediately — exactly when backing off matters.
21
+ */
22
+ retryAfter(response: Response, data: DecodedError): number;
23
+ }
@@ -0,0 +1,53 @@
1
+ /** Why a REST request failed, for handling without matching on status codes. */
2
+ export type RESTErrorKind =
3
+ /** Cancelled through an AbortSignal. */
4
+ "aborted"
5
+ /** The attempt exceeded the transport timeout. */
6
+ | "timeout"
7
+ /** No response: DNS, TLS, connection reset. */
8
+ | "network"
9
+ /** 429 that outlived the retry budget. */
10
+ | "rateLimited"
11
+ /** 401: invalid or revoked token. */
12
+ | "authentication"
13
+ /** 403: missing access or permissions. */
14
+ | "permission"
15
+ /** 404: unknown resource. */
16
+ | "notFound"
17
+ /** 400 / 422: the request body or parameters were rejected. */
18
+ | "validation"
19
+ /** 5xx: Discord failed to handle the request. */
20
+ | "server"
21
+ /** Any other 4xx. */
22
+ | "client";
23
+ /**
24
+ * Replaces the token segment of webhook and interaction paths with `:token`,
25
+ * so request paths can appear in errors, hooks and logs without leaking a
26
+ * credential.
27
+ */
28
+ export declare function redactPath(path: string): string;
29
+ /** Error thrown when Discord rejects a REST request. */
30
+ export declare class RESTError extends Error {
31
+ /** Failure category. */ readonly kind: RESTErrorKind;
32
+ /** HTTP status returned by Discord. */ readonly status: number;
33
+ /** Discord API error code, when provided. */ readonly code?: number;
34
+ /** Raw Discord validation/error payload. */ readonly errors?: unknown;
35
+ /** HTTP method used for the failed request. */ readonly method?: string;
36
+ /** API path used for the failed request. */ readonly path?: string;
37
+ /** Creates a REST error with request context. @param message Error message. @param status HTTP status. @param code Discord error code. @param errors Raw error payload. @param options Request context and cause. */
38
+ constructor(message: string, status: number, code?: number, errors?: unknown, options?: {
39
+ method?: string;
40
+ path?: string;
41
+ cause?: unknown;
42
+ kind?: RESTErrorKind;
43
+ });
44
+ /** Whether sending the same request again later could succeed. Validation,
45
+ * permission, authentication and not-found failures never will. */
46
+ get retryable(): boolean;
47
+ }
48
+ /** Creates the cancellation error shared by every stage of the request pipeline. */
49
+ export declare function abortError(path: string, reason: unknown): RESTError;
50
+ /** Waits for a delay, rejecting early if the signal aborts. */
51
+ export declare function sleep(delay: number, signal: AbortSignal | undefined, path: string): Promise<void>;
52
+ /** Awaits a promise, rejecting early if the signal aborts. */
53
+ export declare function abortable(promise: Promise<void>, signal: AbortSignal | undefined, path: string): Promise<void>;
@@ -1,26 +1,13 @@
1
- /** Error thrown when Discord rejects a REST request. */
2
- export declare class RESTError extends Error {
3
- /** HTTP status returned by Discord. */ readonly status: number;
4
- /** Discord API error code, when provided. */ readonly code?: number;
5
- /** Raw Discord validation/error payload. */ readonly errors?: unknown;
6
- /** HTTP method used for the failed request. */ readonly method?: string;
7
- /** API path used for the failed request. */ readonly path?: string;
8
- /** Creates a REST error with request context. @param message Error message. @param status HTTP status. @param code Discord error code. @param errors Raw error payload. @param options Request context and cause. */
9
- constructor(message: string, status: number, code?: number, errors?: unknown, options?: {
10
- method?: string;
11
- path?: string;
12
- cause?: unknown;
13
- });
14
- }
15
1
  import { type RateLimitStore } from "./store.js";
2
+ import { RESTError } from "./errors.js";
3
+ import { type RESTQuery } from "./route.js";
4
+ import { HttpTransport } from "./transport.js";
16
5
  /** A file attachment sent as part of a multipart REST request. */
17
6
  export interface RESTFileAttachment {
18
7
  name: string;
19
8
  data: Blob | Uint8Array | ArrayBuffer;
20
9
  contentType?: string;
21
10
  }
22
- /** Query-string value accepted by {@link RESTRequestOptions.query}. */
23
- export type RESTQuery = URLSearchParams | Record<string, string | number | boolean | null | undefined> | string;
24
11
  /**
25
12
  * Options controlling an individual REST request.
26
13
  *
@@ -112,7 +99,18 @@ export declare class REST {
112
99
  baseURL?: string;
113
100
  hooks?: RESTHooks;
114
101
  store?: RateLimitStore;
102
+ /** Replaces the HTTP stage, e.g. with a recording transport in tests. */
103
+ transport?: HttpTransport;
104
+ /**
105
+ * Lets requests on a bucket whose limit is known run concurrently, up
106
+ * to its `remaining` allowance, instead of one at a time. Requires a
107
+ * store with `reserve`. Requests on one bucket may then complete out
108
+ * of order (e.g. messages to one channel). Defaults to `false`.
109
+ */
110
+ concurrentBuckets?: boolean;
115
111
  });
112
+ /** The rate-limit store backing this transport. */
113
+ get store(): RateLimitStore;
116
114
  /** Replaces the current hook set. Pass an empty object to clear all hooks. */
117
115
  setHooks(hooks: RESTHooks): this;
118
116
  /** Sets the authentication token. @param token Discord bot token. @returns Nothing. @throws {TypeError} If token is empty. */
@@ -162,7 +160,13 @@ export declare class REST {
162
160
  patchWithFiles<T>(path: string, payload: unknown, files: RESTFileAttachment[], options?: RESTRequestOptions): Promise<T>;
163
161
  }
164
162
  export { Routes } from "./routes.js";
165
- export { type RateLimitStore, type BucketState, MemoryRateLimitStore, } from "./store.js";
163
+ export { RESTError, abortError, redactPath, type RESTErrorKind, } from "./errors.js";
164
+ export { createRouteKey, normalizeRoutePath, majorParameter, scopeBucket, serializeQuery, type RouteKey, type RESTQuery, } from "./route.js";
165
+ export { RequestScheduler } from "./scheduler.js";
166
+ export { RateLimiter } from "./limiter.js";
167
+ export { HttpTransport, TransportError, type TransportRequest, type FetchLike, } from "./transport.js";
168
+ export { ResponseDecoder, type DecodedError } from "./decoder.js";
169
+ export { type RateLimitStore, type BucketState, type Reservation, MemoryRateLimitStore, } from "./store.js";
166
170
  export { RedisRateLimitStore, type RedisRateLimitStoreOptions, type MinimalRedisClient, } from "./redis.js";
167
171
  export { WebhookClient, type WebhookClientOptions, type WebhookMessageOptions, } from "./webhook.js";
168
172
  /**