@wolfstar/plugin-gateway 0.9.0-next-20261002163719 → 0.9.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.
- package/README.md +61 -1
- package/dist/esm/index.d.ts +306 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +324 -4
- package/dist/esm/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -65,6 +65,7 @@ On top of the `Client` options:
|
|
|
65
65
|
| `intents` | — | The gateway intents. |
|
|
66
66
|
| `cacheConstructor` | `CollectionCache` | Builds `manager.cache`, the cache of structure instances each manager reads through, see [Cache](#cache). |
|
|
67
67
|
| `cacheOptions` | `undefined` | Per-entity options of those caches, e.g. `{ messages: { maxSize: 1_000 } }`, see [Bounding memory](#bounding-memory). |
|
|
68
|
+
| `sweepers` | `undefined` | Periodically evicts entries from those caches, e.g. `DefaultSweeperSettings`, see [Sweepers](#sweepers). |
|
|
68
69
|
| `cache` | `undefined` | A `@wolfstar/plugin-cache` cache whose raw stores back the managers instead, see [Cache](#cache). |
|
|
69
70
|
| `shardCount` | `null` | Total shards across every process, `null` for Discord's recommendation. |
|
|
70
71
|
| `shardIds` | `null` | The shards this client runs, as an array or a `{ start, end }` range. `null` for all. |
|
|
@@ -381,7 +382,7 @@ Only `manager.cache.get` (and `fetch`, `resolve`, which read it) returns the cac
|
|
|
381
382
|
|
|
382
383
|
### Bounding memory
|
|
383
384
|
|
|
384
|
-
With the default cache, nothing is evicted unless a dispatch removes it. There are
|
|
385
|
+
With the default cache, nothing is evicted unless a dispatch removes it. There are four ways to bound it:
|
|
385
386
|
|
|
386
387
|
- **`cacheOptions`** sets a `maxSize` per entity: once reached, the oldest entry is evicted for each new one, and
|
|
387
388
|
`0` holds nothing. It is passed to the `cacheConstructor`, the default `CollectionCache` included.
|
|
@@ -419,9 +420,68 @@ With the default cache, nothing is evicted unless a dispatch removes it. There a
|
|
|
419
420
|
new GatewayClient({ intents, cache: null });
|
|
420
421
|
```
|
|
421
422
|
|
|
423
|
+
- **[`sweepers`](#sweepers)** evict entries on a timer, e.g. messages nobody touched for a while.
|
|
424
|
+
|
|
425
|
+
`cacheOptions` also takes a `keepOverLimit(value, key, cache)`, like discord.js's `LimitedCollection`: once `maxSize`
|
|
426
|
+
is reached, the oldest entry it answers `false` for is evicted, and the cache grows past `maxSize` if it keeps them
|
|
427
|
+
all. `cacheWithLimits` builds `cacheOptions` from plain numbers:
|
|
428
|
+
|
|
429
|
+
```ts
|
|
430
|
+
import { cacheWithLimits } from "@wolfstar/plugin-gateway";
|
|
431
|
+
|
|
432
|
+
new GatewayClient({
|
|
433
|
+
intents,
|
|
434
|
+
// 200 messages, no presences, and up to 1000 members plus the ones in a voice channel.
|
|
435
|
+
cacheOptions: cacheWithLimits({
|
|
436
|
+
messages: 200,
|
|
437
|
+
presences: 0,
|
|
438
|
+
members: { maxSize: 1_000, keepOverLimit: (member) => member.voice?.channelId != null },
|
|
439
|
+
}),
|
|
440
|
+
});
|
|
441
|
+
```
|
|
442
|
+
|
|
422
443
|
`cacheOptions` only applies to caches built by a constructor: like `cacheConstructor`, combining it with `cache` or
|
|
423
444
|
`makeCache` throws (those stores have their own bounds), and it is ignored with `cache: null`.
|
|
424
445
|
|
|
446
|
+
### Sweepers
|
|
447
|
+
|
|
448
|
+
Like discord.js's, `sweepers` evict the entries of a cache every `interval` seconds. Each entity takes either a
|
|
449
|
+
`lifetime` (in seconds) or a `filter` factory that returns the predicate of that sweep, or `null` to skip it. Only
|
|
450
|
+
`invites`, `messages` and `threads` take a `lifetime`: a message is swept when it was last edited or created longer
|
|
451
|
+
ago than that, a thread when it has been archived for longer, an invite when it has expired for longer.
|
|
452
|
+
|
|
453
|
+
```ts
|
|
454
|
+
import { GatewayClient, Sweepers } from "@wolfstar/plugin-gateway";
|
|
455
|
+
|
|
456
|
+
const client = new GatewayClient({
|
|
457
|
+
intents,
|
|
458
|
+
sweepers: {
|
|
459
|
+
messages: { interval: 3_600, lifetime: 1_800 },
|
|
460
|
+
users: { interval: 3_600, filter: () => (user) => user.bot },
|
|
461
|
+
// Evict what Sweepers.filterByLifetime selects, counted from a timestamp of your choosing.
|
|
462
|
+
members: {
|
|
463
|
+
interval: 3_600,
|
|
464
|
+
filter: Sweepers.filterByLifetime({
|
|
465
|
+
lifetime: 7_200,
|
|
466
|
+
getComparisonTimestamp: (member) => member.joinedTimestamp,
|
|
467
|
+
excludeFromSweep: (member) => member.voice?.channelId != null,
|
|
468
|
+
}),
|
|
469
|
+
},
|
|
470
|
+
},
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
// Or on demand, which returns how many entries were evicted.
|
|
474
|
+
client.sweepers.sweepMessages(600);
|
|
475
|
+
client.sweepers.sweepUsers(() => (user) => user.bot);
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
`DefaultSweeperSettings` sweeps messages untouched for 30 minutes and threads archived for four hours, every hour.
|
|
479
|
+
Unlike discord.js's, it is not empty. Every sweep emits `cacheSweep(entity, swept)`, and a failing filter emits
|
|
480
|
+
`cacheError` with `operation: "sweep"` instead of throwing out of the timer. `client.destroy()` stops the timers.
|
|
481
|
+
|
|
482
|
+
Sweepers walk the caches of structure instances, so combining them with `cache` or `makeCache` throws: set the `ttl`
|
|
483
|
+
of their `policies` instead.
|
|
484
|
+
|
|
425
485
|
### Custom caches
|
|
426
486
|
|
|
427
487
|
`cacheConstructor` takes a class implementing `Cache`, instantiated once per entity with
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -824,6 +824,14 @@ interface CacheEntityOptions {
|
|
|
824
824
|
* @default Infinity
|
|
825
825
|
*/
|
|
826
826
|
maxSize?: number;
|
|
827
|
+
/**
|
|
828
|
+
* Decides which entries `maxSize` never evicts, like discord.js's `LimitedCollection#keepOverLimit`: called with
|
|
829
|
+
* the oldest entries in turn, the first one it answers `false` for is evicted. When it answers `true` for all of
|
|
830
|
+
* them, the cache grows past `maxSize`.
|
|
831
|
+
*
|
|
832
|
+
* @default undefined
|
|
833
|
+
*/
|
|
834
|
+
keepOverLimit?: (value: any, key: string, cache: Map<string, any>) => boolean;
|
|
827
835
|
}
|
|
828
836
|
/**
|
|
829
837
|
* What a {@link CacheConstructor} receives as its third argument: how the client keys and refreshes the entity, and
|
|
@@ -10133,6 +10141,224 @@ export declare const Partials: {
|
|
|
10133
10141
|
*/
|
|
10134
10142
|
export type Partials = (typeof Partials)[keyof typeof Partials];
|
|
10135
10143
|
//#endregion
|
|
10144
|
+
//#region src/util/Sweepers.d.ts
|
|
10145
|
+
/**
|
|
10146
|
+
* The structures held by the caches {@link Sweepers} can sweep, by entity.
|
|
10147
|
+
*/
|
|
10148
|
+
export interface SweepableEntities {
|
|
10149
|
+
autoModerationRules: AutoModerationRule;
|
|
10150
|
+
bans: GuildBan;
|
|
10151
|
+
emojis: GuildEmoji;
|
|
10152
|
+
invites: GuildInvite;
|
|
10153
|
+
members: GuildMember;
|
|
10154
|
+
messages: Message;
|
|
10155
|
+
presences: Presence;
|
|
10156
|
+
stageInstances: StageInstance;
|
|
10157
|
+
stickers: Sticker;
|
|
10158
|
+
threadMembers: ThreadMember;
|
|
10159
|
+
threads: AnyThreadChannel;
|
|
10160
|
+
users: User;
|
|
10161
|
+
voiceStates: VoiceState;
|
|
10162
|
+
}
|
|
10163
|
+
/**
|
|
10164
|
+
* The name of an entity whose cache {@link Sweepers} can sweep.
|
|
10165
|
+
*/
|
|
10166
|
+
export type SweepableEntityName = keyof SweepableEntities;
|
|
10167
|
+
/**
|
|
10168
|
+
* Every {@link SweepableEntityName}.
|
|
10169
|
+
*/
|
|
10170
|
+
export declare const SweepableEntityNames: readonly ["autoModerationRules", "bans", "emojis", "invites", "members", "messages", "presences", "stageInstances", "stickers", "threadMembers", "threads", "users", "voiceStates"];
|
|
10171
|
+
/**
|
|
10172
|
+
* The entities whose sweeper can be set with a `lifetime` instead of a `filter`.
|
|
10173
|
+
*/
|
|
10174
|
+
export type LifetimeSweepableEntityName = "invites" | "messages" | "threads";
|
|
10175
|
+
/**
|
|
10176
|
+
* Decides whether a sweep evicts an entry: `true` evicts it.
|
|
10177
|
+
*/
|
|
10178
|
+
export type SweepFilter<Value> = (value: Value, key: string, collection: Collection<string, Value>) => boolean;
|
|
10179
|
+
/**
|
|
10180
|
+
* Builds the {@link SweepFilter} of one sweep, called once per sweep so that it can read the clock. Answering `null`
|
|
10181
|
+
* skips the sweep.
|
|
10182
|
+
*/
|
|
10183
|
+
export type SweepFilterFactory<Value> = () => SweepFilter<Value> | null;
|
|
10184
|
+
/**
|
|
10185
|
+
* Sweeps an entity with a filter, every `interval` seconds.
|
|
10186
|
+
*/
|
|
10187
|
+
export interface SweeperFilterOptions<Value> {
|
|
10188
|
+
/**
|
|
10189
|
+
* The seconds between two sweeps. A non-positive or infinite interval does not schedule any.
|
|
10190
|
+
*/
|
|
10191
|
+
interval: number;
|
|
10192
|
+
/**
|
|
10193
|
+
* Builds the filter of each sweep, see {@link SweepFilterFactory}.
|
|
10194
|
+
*/
|
|
10195
|
+
filter: SweepFilterFactory<Value>;
|
|
10196
|
+
lifetime?: never;
|
|
10197
|
+
}
|
|
10198
|
+
/**
|
|
10199
|
+
* Sweeps an entity every `interval` seconds, evicting the entries older than `lifetime` seconds.
|
|
10200
|
+
*/
|
|
10201
|
+
export interface SweeperLifetimeOptions {
|
|
10202
|
+
/**
|
|
10203
|
+
* The seconds between two sweeps. A non-positive or infinite interval does not schedule any.
|
|
10204
|
+
*/
|
|
10205
|
+
interval: number;
|
|
10206
|
+
/**
|
|
10207
|
+
* The seconds an entry lives: messages since they were created or last edited, threads since they were archived (an
|
|
10208
|
+
* active thread is never swept), invites since they expired (an invite that never expires is never swept). A
|
|
10209
|
+
* non-positive lifetime skips the sweep.
|
|
10210
|
+
*/
|
|
10211
|
+
lifetime: number;
|
|
10212
|
+
filter?: never;
|
|
10213
|
+
}
|
|
10214
|
+
/**
|
|
10215
|
+
* The sweepers of the client, by entity, see {@link GatewayClientOptions.sweepers}.
|
|
10216
|
+
*/
|
|
10217
|
+
export type SweeperOptions = { [Name in SweepableEntityName]?: Name extends LifetimeSweepableEntityName ? SweeperFilterOptions<SweepableEntities[Name]> | SweeperLifetimeOptions : SweeperFilterOptions<SweepableEntities[Name]>; };
|
|
10218
|
+
/**
|
|
10219
|
+
* The options of {@link Sweepers.filterByLifetime}.
|
|
10220
|
+
*/
|
|
10221
|
+
export interface FilterByLifetimeOptions<Value> {
|
|
10222
|
+
/**
|
|
10223
|
+
* The seconds an entry lives, a non-positive value skips the sweep.
|
|
10224
|
+
*
|
|
10225
|
+
* @default 14400
|
|
10226
|
+
*/
|
|
10227
|
+
lifetime?: number;
|
|
10228
|
+
/**
|
|
10229
|
+
* Gets the timestamp the lifetime is counted from, an entry without one is never swept.
|
|
10230
|
+
*
|
|
10231
|
+
* @default (value) => value.createdTimestamp
|
|
10232
|
+
*/
|
|
10233
|
+
getComparisonTimestamp?: (value: Value, key: string, collection: Collection<string, Value>) => number | null | undefined;
|
|
10234
|
+
/**
|
|
10235
|
+
* Whether an entry is never swept.
|
|
10236
|
+
*
|
|
10237
|
+
* @default () => false
|
|
10238
|
+
*/
|
|
10239
|
+
excludeFromSweep?: SweepFilter<Value>;
|
|
10240
|
+
}
|
|
10241
|
+
/**
|
|
10242
|
+
* Evicts entries from the caches of instances, on a schedule or on demand, like discord.js's `Sweepers`.
|
|
10243
|
+
*
|
|
10244
|
+
* @remarks
|
|
10245
|
+
* It works with the caches the client builds from {@link GatewayClientOptions.cacheConstructor}, the default
|
|
10246
|
+
* `CollectionCache` included: they are `Map`s. The stores of `@wolfstar/plugin-cache` hold raw data, and expire it
|
|
10247
|
+
* with the `ttl` of their policies instead, so sweeping them throws.
|
|
10248
|
+
*
|
|
10249
|
+
* A sweep only evicts entries from the cache: it emits no event but `cacheSweep`, and the structures the application
|
|
10250
|
+
* keeps stay valid.
|
|
10251
|
+
*
|
|
10252
|
+
* @example
|
|
10253
|
+
* ```typescript
|
|
10254
|
+
* const client = new GatewayClient({
|
|
10255
|
+
* intents,
|
|
10256
|
+
* sweepers: {
|
|
10257
|
+
* // Every hour, forget the messages not touched for half an hour.
|
|
10258
|
+
* messages: { interval: 3_600, lifetime: 1_800 },
|
|
10259
|
+
* // Every hour, forget the bots.
|
|
10260
|
+
* users: { interval: 3_600, filter: () => (user) => user.bot },
|
|
10261
|
+
* },
|
|
10262
|
+
* });
|
|
10263
|
+
*
|
|
10264
|
+
* // On demand.
|
|
10265
|
+
* client.sweepers.sweepMessages(600);
|
|
10266
|
+
* ```
|
|
10267
|
+
*/
|
|
10268
|
+
export declare class Sweepers {
|
|
10269
|
+
#private;
|
|
10270
|
+
/**
|
|
10271
|
+
* The options the sweepers were scheduled from.
|
|
10272
|
+
*/
|
|
10273
|
+
readonly options: SweeperOptions;
|
|
10274
|
+
constructor(client: GatewayClient, options?: SweeperOptions);
|
|
10275
|
+
/**
|
|
10276
|
+
* Evicts the entries of an entity's cache the filter answers `true` for.
|
|
10277
|
+
*
|
|
10278
|
+
* @param entity The name of the entity.
|
|
10279
|
+
* @param filter Builds the filter of the sweep.
|
|
10280
|
+
* @returns The amount of entries evicted, `0` when the filter skips the sweep or the entity is not cached.
|
|
10281
|
+
*/
|
|
10282
|
+
sweep<Name extends SweepableEntityName>(entity: Name, filter: SweepFilterFactory<SweepableEntities[Name]>): number;
|
|
10283
|
+
/**
|
|
10284
|
+
* Evicts the auto moderation rules the filter answers `true` for.
|
|
10285
|
+
*/
|
|
10286
|
+
sweepAutoModerationRules(filter: SweepFilterFactory<AutoModerationRule>): number;
|
|
10287
|
+
/**
|
|
10288
|
+
* Evicts the bans the filter answers `true` for.
|
|
10289
|
+
*/
|
|
10290
|
+
sweepBans(filter: SweepFilterFactory<GuildBan>): number;
|
|
10291
|
+
/**
|
|
10292
|
+
* Evicts the emojis the filter answers `true` for.
|
|
10293
|
+
*/
|
|
10294
|
+
sweepEmojis(filter: SweepFilterFactory<GuildEmoji>): number;
|
|
10295
|
+
/**
|
|
10296
|
+
* Evicts the invites that expired more than `lifetime` seconds ago.
|
|
10297
|
+
*
|
|
10298
|
+
* @param lifetime The seconds an invite lives past its expiration.
|
|
10299
|
+
*/
|
|
10300
|
+
sweepInvites(lifetime?: number): number;
|
|
10301
|
+
/**
|
|
10302
|
+
* Evicts the members the filter answers `true` for.
|
|
10303
|
+
*/
|
|
10304
|
+
sweepMembers(filter: SweepFilterFactory<GuildMember>): number;
|
|
10305
|
+
/**
|
|
10306
|
+
* Evicts the messages created or last edited more than `lifetime` seconds ago.
|
|
10307
|
+
*
|
|
10308
|
+
* @param lifetime The seconds a message lives.
|
|
10309
|
+
*/
|
|
10310
|
+
sweepMessages(lifetime?: number): number;
|
|
10311
|
+
/**
|
|
10312
|
+
* Evicts the presences the filter answers `true` for.
|
|
10313
|
+
*/
|
|
10314
|
+
sweepPresences(filter: SweepFilterFactory<Presence>): number;
|
|
10315
|
+
/**
|
|
10316
|
+
* Evicts the stage instances the filter answers `true` for.
|
|
10317
|
+
*/
|
|
10318
|
+
sweepStageInstances(filter: SweepFilterFactory<StageInstance>): number;
|
|
10319
|
+
/**
|
|
10320
|
+
* Evicts the stickers the filter answers `true` for.
|
|
10321
|
+
*/
|
|
10322
|
+
sweepStickers(filter: SweepFilterFactory<Sticker>): number;
|
|
10323
|
+
/**
|
|
10324
|
+
* Evicts the thread members the filter answers `true` for.
|
|
10325
|
+
*/
|
|
10326
|
+
sweepThreadMembers(filter: SweepFilterFactory<ThreadMember>): number;
|
|
10327
|
+
/**
|
|
10328
|
+
* Evicts the threads archived more than `lifetime` seconds ago, see {@link Sweepers.outdatedThreadSweepFilter}.
|
|
10329
|
+
*
|
|
10330
|
+
* @param lifetime The seconds a thread lives past its archival.
|
|
10331
|
+
*/
|
|
10332
|
+
sweepThreads(lifetime?: number): number;
|
|
10333
|
+
/**
|
|
10334
|
+
* Evicts the users the filter answers `true` for.
|
|
10335
|
+
*/
|
|
10336
|
+
sweepUsers(filter: SweepFilterFactory<User>): number;
|
|
10337
|
+
/**
|
|
10338
|
+
* Evicts the voice states the filter answers `true` for.
|
|
10339
|
+
*/
|
|
10340
|
+
sweepVoiceStates(filter: SweepFilterFactory<VoiceState>): number;
|
|
10341
|
+
/**
|
|
10342
|
+
* Stops the scheduled sweeps. On-demand sweeps keep working.
|
|
10343
|
+
*/
|
|
10344
|
+
destroy(): void;
|
|
10345
|
+
/**
|
|
10346
|
+
* Builds a filter factory evicting the entries older than a lifetime, for {@link Sweepers.sweep} and the `filter`
|
|
10347
|
+
* of a sweeper.
|
|
10348
|
+
*
|
|
10349
|
+
* @param options The lifetime, and what it is counted from.
|
|
10350
|
+
*/
|
|
10351
|
+
static filterByLifetime<Value>(options?: FilterByLifetimeOptions<Value>): SweepFilterFactory<Value>;
|
|
10352
|
+
/**
|
|
10353
|
+
* Builds a filter factory evicting the archived threads that have been so for longer than a lifetime. Active
|
|
10354
|
+
* threads are never evicted.
|
|
10355
|
+
*
|
|
10356
|
+
* @param lifetime The seconds a thread lives past its archival.
|
|
10357
|
+
* @default 14400
|
|
10358
|
+
*/
|
|
10359
|
+
static outdatedThreadSweepFilter(lifetime?: number): SweepFilterFactory<AnyThreadChannel>;
|
|
10360
|
+
}
|
|
10361
|
+
//#endregion
|
|
10136
10362
|
//#region src/GatewayClient.d.ts
|
|
10137
10363
|
export interface GatewayClientOptions extends ClientOptions, GatewayClientMessageDefaults {
|
|
10138
10364
|
/**
|
|
@@ -10199,6 +10425,30 @@ export interface GatewayClientOptions extends ClientOptions, GatewayClientMessag
|
|
|
10199
10425
|
* @default undefined
|
|
10200
10426
|
*/
|
|
10201
10427
|
cacheOptions?: Partial<Record<CacheEntityName, CacheEntityOptions>>;
|
|
10428
|
+
/**
|
|
10429
|
+
* Evicts entries from the caches of instances on a schedule, like discord.js's `sweepers` option: each entity has
|
|
10430
|
+
* its own sweeper, evicting what a `filter` selects or what outlived a `lifetime`, every `interval` seconds. See
|
|
10431
|
+
* {@link Sweepers}, whose methods sweep on demand, and {@link DefaultSweeperSettings}.
|
|
10432
|
+
*
|
|
10433
|
+
* @remarks
|
|
10434
|
+
* The timers do not keep the process alive, and stop on {@link GatewayClient.destroy}. A sweep that throws (e.g. a
|
|
10435
|
+
* filter) is reported through `cacheError`, and the next one runs as scheduled.
|
|
10436
|
+
*
|
|
10437
|
+
* It only applies to caches built by a constructor, the default included: like `cacheConstructor`, combining it with
|
|
10438
|
+
* {@link GatewayClientOptions.cache} or {@link GatewayClientOptions.makeCache} throws, their stores expire entries
|
|
10439
|
+
* with the `ttl` of {@link GatewayClientOptions.policies}. With `cache: null` it is ignored.
|
|
10440
|
+
*
|
|
10441
|
+
* @example
|
|
10442
|
+
* ```typescript
|
|
10443
|
+
* sweepers: {
|
|
10444
|
+
* messages: { interval: 3_600, lifetime: 1_800 },
|
|
10445
|
+
* users: { interval: 3_600, filter: () => (user) => user.bot },
|
|
10446
|
+
* }
|
|
10447
|
+
* ```
|
|
10448
|
+
*
|
|
10449
|
+
* @default undefined
|
|
10450
|
+
*/
|
|
10451
|
+
sweepers?: SweeperOptions;
|
|
10202
10452
|
/**
|
|
10203
10453
|
* A `@wolfstar/plugin-cache` cache (in memory or Redis) whose raw stores back the managers, instead of the
|
|
10204
10454
|
* in-memory caches of instances built by {@link GatewayClientOptions.cacheConstructor}: structures are then built
|
|
@@ -10417,6 +10667,11 @@ export declare class GatewayClient extends Client {
|
|
|
10417
10667
|
readonly templates: GuildTemplateManager;
|
|
10418
10668
|
readonly voiceStates: VoiceStateManager;
|
|
10419
10669
|
readonly presences: PresenceManager;
|
|
10670
|
+
/**
|
|
10671
|
+
* Evicts entries from the caches of instances, on the schedule of {@link GatewayClientOptions.sweepers} and on
|
|
10672
|
+
* demand, like discord.js's `Client#sweepers`.
|
|
10673
|
+
*/
|
|
10674
|
+
readonly sweepers: Sweepers;
|
|
10420
10675
|
/**
|
|
10421
10676
|
* What happens to a dispatch whose cache read or write fails, see {@link GatewayClientOptions.cacheFailure}.
|
|
10422
10677
|
*/
|
|
@@ -10479,6 +10734,13 @@ export declare class GatewayClient extends Client {
|
|
|
10479
10734
|
* @param name The name of the entity.
|
|
10480
10735
|
*/
|
|
10481
10736
|
CacheConstructor<Value extends StructureMixin<object>>(creator: StructureCreator<Value>, name: CacheEntityName): Cache<Value>;
|
|
10737
|
+
/**
|
|
10738
|
+
* Gets the cache of an entity if it was built already, without building it.
|
|
10739
|
+
*
|
|
10740
|
+
* @param name The name of the entity.
|
|
10741
|
+
* @internal
|
|
10742
|
+
*/
|
|
10743
|
+
cacheOf(name: CacheEntityName): Cache<any> | undefined;
|
|
10482
10744
|
/**
|
|
10483
10745
|
* Runs a raw store operation, reporting its failure through `cacheError`, see
|
|
10484
10746
|
* {@link GatewayClientOptions.cacheErrors}. Synchronous when the operation is.
|
|
@@ -10779,7 +11041,7 @@ export interface CacheErrorContext {
|
|
|
10779
11041
|
/**
|
|
10780
11042
|
* The operation that failed.
|
|
10781
11043
|
*/
|
|
10782
|
-
operation: "get" | "set" | "upsert" | "delete" | "has" | "clear" | "getSize" | "keys" | "entries";
|
|
11044
|
+
operation: "get" | "set" | "upsert" | "delete" | "has" | "clear" | "getSize" | "keys" | "entries" | "sweep";
|
|
10783
11045
|
}
|
|
10784
11046
|
/**
|
|
10785
11047
|
* What a reaction event says besides the reaction and the user.
|
|
@@ -10851,6 +11113,10 @@ export interface GatewayEventMap {
|
|
|
10851
11113
|
* `cacheErrors: "miss"`, the manager then carries on as if the entry was not cached.
|
|
10852
11114
|
*/
|
|
10853
11115
|
cacheError: [error: unknown, context: CacheErrorContext];
|
|
11116
|
+
/**
|
|
11117
|
+
* Emitted when a sweep of an entity's cache ran, with the amount of entries it evicted, see `Sweepers`.
|
|
11118
|
+
*/
|
|
11119
|
+
cacheSweep: [entity: SweepableEntityName, swept: number];
|
|
10854
11120
|
guildCreate: [guild: Guild];
|
|
10855
11121
|
guildUpdate: [oldGuild: Guild | null, newGuild: Guild];
|
|
10856
11122
|
/**
|
|
@@ -11041,6 +11307,7 @@ export declare enum GatewayEvents {
|
|
|
11041
11307
|
ShardClose = "shardClose",
|
|
11042
11308
|
ShardError = "shardError",
|
|
11043
11309
|
CacheError = "cacheError",
|
|
11310
|
+
CacheSweep = "cacheSweep",
|
|
11044
11311
|
GuildCreate = "guildCreate",
|
|
11045
11312
|
GuildUpdate = "guildUpdate",
|
|
11046
11313
|
GuildDelete = "guildDelete",
|
|
@@ -11173,7 +11440,9 @@ export declare namespace EventGatewayListener {
|
|
|
11173
11440
|
export declare const GatewayErrorMessages: {
|
|
11174
11441
|
readonly ClientSessionStoreConflict: "sessionStore replaces gateway.retrieveSessionInfo and gateway.updateSessionInfo, pass one or the other";
|
|
11175
11442
|
readonly ClientCacheConflict: "cacheConstructor and cacheOptions cannot be combined with cache or makeCache, pass one or the other";
|
|
11443
|
+
readonly ClientSweepersConflict: "sweepers cannot be combined with cache or makeCache, whose stores hold raw data a sweep cannot filter: use their policies' ttl instead";
|
|
11176
11444
|
readonly ClientNotConstructed: "No GatewayClient has been constructed yet";
|
|
11445
|
+
readonly SweeperIntervalTooLong: (entity: string, interval: number) => string;
|
|
11177
11446
|
readonly DispatchHandlerConflict: (event: string) => string;
|
|
11178
11447
|
readonly DispatchTimeout: (type: string, shardId: number, partition: string | null, timeout: number) => string;
|
|
11179
11448
|
readonly SessionStoreFailed: (operation: "get" | "set", shardId: number) => string;
|
|
@@ -11324,6 +11593,11 @@ export interface CollectionCacheOptions<Value, Raw> {
|
|
|
11324
11593
|
* @default Infinity
|
|
11325
11594
|
*/
|
|
11326
11595
|
maxSize?: number;
|
|
11596
|
+
/**
|
|
11597
|
+
* Decides which entries `maxSize` never evicts: the oldest one it answers `false` for is evicted instead. When it
|
|
11598
|
+
* answers `true` for every entry, the cache grows past `maxSize`.
|
|
11599
|
+
*/
|
|
11600
|
+
keepOverLimit?: (value: Value, key: string, cache: Map<string, Value>) => boolean;
|
|
11327
11601
|
}
|
|
11328
11602
|
/**
|
|
11329
11603
|
* The default {@link Cache}: an in-memory `Collection` of structure instances, like discord.js's caches.
|
|
@@ -11620,6 +11894,37 @@ export declare class GatewaySessionStoreError extends GatewayError<"SessionStore
|
|
|
11620
11894
|
constructor(operation: "get" | "set", shardId: number, cause: unknown);
|
|
11621
11895
|
}
|
|
11622
11896
|
//#endregion
|
|
11897
|
+
//#region src/util/limits.d.ts
|
|
11898
|
+
/**
|
|
11899
|
+
* Builds the {@link GatewayClientOptions.cacheOptions} bounding the caches of some entities, like discord.js's
|
|
11900
|
+
* `Options.cacheWithLimits`. A number is the `maxSize` of the entity, an object is its whole options.
|
|
11901
|
+
*
|
|
11902
|
+
* @example
|
|
11903
|
+
* ```typescript
|
|
11904
|
+
* const client = new GatewayClient({
|
|
11905
|
+
* intents,
|
|
11906
|
+
* // Keep the 200 most recent messages, no presences, and up to 1000 members, plus any member in a voice channel.
|
|
11907
|
+
* cacheOptions: cacheWithLimits({
|
|
11908
|
+
* messages: 200,
|
|
11909
|
+
* presences: 0,
|
|
11910
|
+
* members: { maxSize: 1_000, keepOverLimit: (member) => member.voice?.channelId != null },
|
|
11911
|
+
* }),
|
|
11912
|
+
* });
|
|
11913
|
+
* ```
|
|
11914
|
+
*
|
|
11915
|
+
* @param limits The `maxSize` or the options of each entity.
|
|
11916
|
+
*/
|
|
11917
|
+
export declare function cacheWithLimits(limits: Partial<Record<CacheEntityName, number | CacheEntityOptions>>): Partial<Record<CacheEntityName, CacheEntityOptions>>;
|
|
11918
|
+
/**
|
|
11919
|
+
* Ready-made {@link GatewayClientOptions.sweepers}, for a bot that only needs recent messages and threads: every hour,
|
|
11920
|
+
* messages untouched for 30 minutes are evicted, and so are threads archived for more than four hours.
|
|
11921
|
+
*
|
|
11922
|
+
* @remarks
|
|
11923
|
+
* Unlike discord.js's `Options.DefaultSweeperSettings`, which is empty, it sweeps. Spread it to override an entity:
|
|
11924
|
+
* `sweepers: { ...DefaultSweeperSettings, users: { interval: 3_600, filter: () => (user) => user.bot } }`.
|
|
11925
|
+
*/
|
|
11926
|
+
export declare const DefaultSweeperSettings: SweeperOptions;
|
|
11927
|
+
//#endregion
|
|
11623
11928
|
//#region src/util/permissions.d.ts
|
|
11624
11929
|
/**
|
|
11625
11930
|
* What {@link computeGuildPermissions} needs to know about a member of a guild.
|