Skip to content

Commit 8db6df3

Browse files
authored
types(Options): add types for cacheWithLimits (#6095)
1 parent 576eee8 commit 8db6df3

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/util/Options.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @typedef {Function} CacheFactory
2323
* @param {Function} manager The manager class the cache is being requested from.
2424
* @param {Function} holds The class that the cache will hold.
25-
* @returns {Collection} Cache instance that follows collection interface.
25+
* @returns {Collection} A Collection used to store the cache of the manager.
2626
*/
2727

2828
/**
@@ -34,8 +34,7 @@
3434
* @property {number} [shardCount=1] The total amount of shards used by all processes of this bot
3535
* (e.g. recommended shard count, shard count of the ShardingManager)
3636
* @property {CacheFactory} [makeCache] Function to create a cache.
37-
* (-1 or Infinity for unlimited - don't do this without message sweeping, otherwise memory usage will climb
38-
* indefinitely)
37+
* You can use your own function, or the {@link Options} class to customize the Collection used for the cache.
3938
* @property {number} [messageCacheLifetime=0] How long a message should stay in the cache until it is considered
4039
* sweepable (in seconds, 0 for forever)
4140
* @property {number} [messageSweepInterval=0] How frequently to remove messages from the cache that are older than

typings/index.d.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ export class ClientUser extends User {
366366
export class Options extends null {
367367
private constructor();
368368
public static createDefaultOptions(): ClientOptions;
369-
public static cacheWithLimits(limits?: Record<string, number>): CacheFactory;
369+
public static cacheWithLimits(limits?: CacheWithLimitOptions): CacheFactory;
370370
public static cacheEverything(): CacheFactory;
371371
}
372372

@@ -2739,7 +2739,36 @@ export type BitFieldResolvable<T extends string, N extends number | bigint> =
27392739

27402740
export type BufferResolvable = Buffer | string;
27412741

2742-
export type CacheFactory = <T>(manager: { name: string }, holds: { name: string }) => Collection<Snowflake, T>;
2742+
export type CachedManagerTypes = keyof CacheFactoryArgs;
2743+
2744+
export type CacheFactory = <T extends CachedManagerTypes>(
2745+
...args: CacheFactoryArgs[T]
2746+
) => Collection<Snowflake, CacheFactoryArgs[T][1]>;
2747+
2748+
export interface CacheFactoryArgs {
2749+
ApplicationCommandManager: [manager: typeof ApplicationCommandManager, holds: typeof ApplicationCommand];
2750+
BaseGuildEmojiManager: [manager: typeof BaseGuildEmojiManager, holds: typeof GuildEmoji];
2751+
ChannelManager: [manager: typeof ChannelManager, holds: typeof Channel];
2752+
GuildChannelManager: [manager: typeof GuildChannelManager, holds: typeof GuildChannel];
2753+
GuildManager: [manager: typeof GuildManager, holds: typeof Guild];
2754+
GuildMemberManager: [manager: typeof GuildMemberManager, holds: typeof GuildMember];
2755+
GuildBanManager: [manager: typeof GuildBanManager, holds: typeof GuildBan];
2756+
MessageManager: [manager: typeof MessageManager, holds: typeof Message];
2757+
PermissionOverwriteManager: [manager: typeof PermissionOverwriteManager, holds: typeof PermissionOverwrites];
2758+
PresenceManager: [manager: typeof PresenceManager, holds: typeof Presence];
2759+
ReactionManager: [manager: typeof ReactionManager, holds: typeof MessageReaction];
2760+
ReactionUserManager: [manager: typeof ReactionUserManager, holds: typeof User];
2761+
RoleManager: [manager: typeof RoleManager, holds: typeof Role];
2762+
StageInstanceManager: [manager: typeof StageInstanceManager, holds: typeof StageInstance];
2763+
ThreadManager: [manager: typeof ThreadManager, holds: typeof ThreadChannel];
2764+
ThreadMemberManager: [manager: typeof ThreadMemberManager, holds: typeof ThreadMember];
2765+
UserManager: [manager: typeof UserManager, holds: typeof User];
2766+
VoiceStateManager: [manager: typeof VoiceStateManager, holds: typeof VoiceState];
2767+
}
2768+
2769+
export type CacheWithLimitOptions = {
2770+
[K in CachedManagerTypes]?: number;
2771+
};
27432772

27442773
export interface ChannelCreationOverwrites {
27452774
allow?: PermissionResolvable;

typings/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const client: Client = new Client({
5353
intents: Intents.FLAGS.GUILDS,
5454
makeCache: Options.cacheWithLimits({
5555
MessageManager: 200,
56+
// @ts-expect-error
57+
Message: 100,
5658
}),
5759
});
5860

0 commit comments

Comments
 (0)