From 8db6df3d1eb58c542624e875f7da2a848153d11c Mon Sep 17 00:00:00 2001 From: DTrombett <73136330+DTrombett@users.noreply.github.com> Date: Fri, 16 Jul 2021 13:20:38 +0200 Subject: [PATCH] types(Options): add types for cacheWithLimits (#6095) --- src/util/Options.js | 5 ++--- typings/index.d.ts | 33 +++++++++++++++++++++++++++++++-- typings/index.ts | 2 ++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/util/Options.js b/src/util/Options.js index 2cc3caea9..01685be66 100644 --- a/src/util/Options.js +++ b/src/util/Options.js @@ -22,7 +22,7 @@ * @typedef {Function} CacheFactory * @param {Function} manager The manager class the cache is being requested from. * @param {Function} holds The class that the cache will hold. - * @returns {Collection} Cache instance that follows collection interface. + * @returns {Collection} A Collection used to store the cache of the manager. */ /** @@ -34,8 +34,7 @@ * @property {number} [shardCount=1] The total amount of shards used by all processes of this bot * (e.g. recommended shard count, shard count of the ShardingManager) * @property {CacheFactory} [makeCache] Function to create a cache. - * (-1 or Infinity for unlimited - don't do this without message sweeping, otherwise memory usage will climb - * indefinitely) + * You can use your own function, or the {@link Options} class to customize the Collection used for the cache. * @property {number} [messageCacheLifetime=0] How long a message should stay in the cache until it is considered * sweepable (in seconds, 0 for forever) * @property {number} [messageSweepInterval=0] How frequently to remove messages from the cache that are older than diff --git a/typings/index.d.ts b/typings/index.d.ts index c3c8eed08..1dab6bd51 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -366,7 +366,7 @@ export class ClientUser extends User { export class Options extends null { private constructor(); public static createDefaultOptions(): ClientOptions; - public static cacheWithLimits(limits?: Record): CacheFactory; + public static cacheWithLimits(limits?: CacheWithLimitOptions): CacheFactory; public static cacheEverything(): CacheFactory; } @@ -2739,7 +2739,36 @@ export type BitFieldResolvable = export type BufferResolvable = Buffer | string; -export type CacheFactory = (manager: { name: string }, holds: { name: string }) => Collection; +export type CachedManagerTypes = keyof CacheFactoryArgs; + +export type CacheFactory = ( + ...args: CacheFactoryArgs[T] +) => Collection; + +export interface CacheFactoryArgs { + ApplicationCommandManager: [manager: typeof ApplicationCommandManager, holds: typeof ApplicationCommand]; + BaseGuildEmojiManager: [manager: typeof BaseGuildEmojiManager, holds: typeof GuildEmoji]; + ChannelManager: [manager: typeof ChannelManager, holds: typeof Channel]; + GuildChannelManager: [manager: typeof GuildChannelManager, holds: typeof GuildChannel]; + GuildManager: [manager: typeof GuildManager, holds: typeof Guild]; + GuildMemberManager: [manager: typeof GuildMemberManager, holds: typeof GuildMember]; + GuildBanManager: [manager: typeof GuildBanManager, holds: typeof GuildBan]; + MessageManager: [manager: typeof MessageManager, holds: typeof Message]; + PermissionOverwriteManager: [manager: typeof PermissionOverwriteManager, holds: typeof PermissionOverwrites]; + PresenceManager: [manager: typeof PresenceManager, holds: typeof Presence]; + ReactionManager: [manager: typeof ReactionManager, holds: typeof MessageReaction]; + ReactionUserManager: [manager: typeof ReactionUserManager, holds: typeof User]; + RoleManager: [manager: typeof RoleManager, holds: typeof Role]; + StageInstanceManager: [manager: typeof StageInstanceManager, holds: typeof StageInstance]; + ThreadManager: [manager: typeof ThreadManager, holds: typeof ThreadChannel]; + ThreadMemberManager: [manager: typeof ThreadMemberManager, holds: typeof ThreadMember]; + UserManager: [manager: typeof UserManager, holds: typeof User]; + VoiceStateManager: [manager: typeof VoiceStateManager, holds: typeof VoiceState]; +} + +export type CacheWithLimitOptions = { + [K in CachedManagerTypes]?: number; +}; export interface ChannelCreationOverwrites { allow?: PermissionResolvable; diff --git a/typings/index.ts b/typings/index.ts index ed9377ff6..4d2f84f8f 100644 --- a/typings/index.ts +++ b/typings/index.ts @@ -53,6 +53,8 @@ const client: Client = new Client({ intents: Intents.FLAGS.GUILDS, makeCache: Options.cacheWithLimits({ MessageManager: 200, + // @ts-expect-error + Message: 100, }), });