Skip to content

Commit 346fa57

Browse files
fix(CachedManager): allow overriding constructor for makeCache (#9763)
* fix(CachedManager): allow overriding constructor for makeCache * feat: allow determining makeCache based on non-overriden constructor * types: cleanup leftovers --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent b3c85d3 commit 346fa57

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

packages/discord.js/src/managers/CachedManager.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const DataManager = require('./DataManager');
4+
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
45

56
/**
67
* Manages the API methods of a data model with a mutable cache of instances.
@@ -18,7 +19,13 @@ class CachedManager extends DataManager {
1819
* @readonly
1920
* @name CachedManager#_cache
2021
*/
21-
Object.defineProperty(this, '_cache', { value: this.client.options.makeCache(this.constructor, this.holds) });
22+
Object.defineProperty(this, '_cache', {
23+
value: this.client.options.makeCache(
24+
this.constructor[MakeCacheOverrideSymbol] ?? this.constructor,
25+
this.holds,
26+
this.constructor,
27+
),
28+
});
2229

2330
if (iterable) {
2431
for (const item of iterable) {

packages/discord.js/src/managers/MessageManager.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const CachedManager = require('./CachedManager');
77
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
88
const { Message } = require('../structures/Message');
99
const MessagePayload = require('../structures/MessagePayload');
10+
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
1011
const { resolvePartialEmoji } = require('../util/Util');
1112

1213
/**
@@ -15,6 +16,8 @@ const { resolvePartialEmoji } = require('../util/Util');
1516
* @abstract
1617
*/
1718
class MessageManager extends CachedManager {
19+
static [MakeCacheOverrideSymbol] = MessageManager;
20+
1821
constructor(channel, iterable) {
1922
super(channel.client, Message, iterable);
2023

packages/discord.js/src/managers/ThreadManager.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ const { Routes } = require('discord-api-types/v10');
66
const CachedManager = require('./CachedManager');
77
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
88
const ThreadChannel = require('../structures/ThreadChannel');
9+
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
910

1011
/**
1112
* Manages API methods for thread-based channels and stores their cache.
1213
* @extends {CachedManager}
1314
*/
1415
class ThreadManager extends CachedManager {
16+
static [MakeCacheOverrideSymbol] = ThreadManager;
17+
1518
constructor(channel, iterable) {
1619
super(channel.client, ThreadChannel, iterable);
1720

packages/discord.js/src/util/Options.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const { DefaultRestOptions, DefaultUserAgentAppendix } = require('@discordjs/res
44
const { toSnakeCase } = require('./Transformers');
55
const { version } = require('../../package.json');
66

7+
// TODO(ckohen): switch order of params so full manager is first and "type" is optional
78
/**
89
* @typedef {Function} CacheFactory
9-
* @param {Function} manager The manager class the cache is being requested from.
10+
* @param {Function} managerType The base manager class the cache is being requested from.
1011
* @param {Function} holds The class that the cache will hold.
12+
* @param {Function} manager The fully extended manager class the cache is being requested from.
1113
* @returns {Collection} A Collection used to store the cache of the manager.
1214
*/
1315

@@ -150,8 +152,8 @@ class Options extends null {
150152
const { Collection } = require('@discordjs/collection');
151153
const LimitedCollection = require('./LimitedCollection');
152154

153-
return manager => {
154-
const setting = settings[manager.name];
155+
return (managerType, _, manager) => {
156+
const setting = settings[manager.name] ?? settings[managerType.name];
155157
/* eslint-disable-next-line eqeqeq */
156158
if (setting == null) {
157159
return new Collection();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
exports.MakeCacheOverrideSymbol = Symbol('djs.managers.makeCacheOverride');

packages/discord.js/typings/index.d.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4720,17 +4720,19 @@ export interface Caches {
47204720
AutoModerationRuleManager: [manager: typeof AutoModerationRuleManager, holds: typeof AutoModerationRule];
47214721
ApplicationCommandManager: [manager: typeof ApplicationCommandManager, holds: typeof ApplicationCommand];
47224722
BaseGuildEmojiManager: [manager: typeof BaseGuildEmojiManager, holds: typeof GuildEmoji];
4723+
DMMessageManager: [manager: typeof MessageManager, holds: typeof Message<false>];
47234724
GuildEmojiManager: [manager: typeof GuildEmojiManager, holds: typeof GuildEmoji];
47244725
// TODO: ChannelManager: [manager: typeof ChannelManager, holds: typeof Channel];
47254726
// TODO: GuildChannelManager: [manager: typeof GuildChannelManager, holds: typeof GuildChannel];
47264727
// TODO: GuildManager: [manager: typeof GuildManager, holds: typeof Guild];
47274728
GuildMemberManager: [manager: typeof GuildMemberManager, holds: typeof GuildMember];
47284729
GuildBanManager: [manager: typeof GuildBanManager, holds: typeof GuildBan];
4729-
GuildForumThreadManager: [manager: typeof GuildForumThreadManager, holds: typeof ThreadChannel];
4730+
GuildForumThreadManager: [manager: typeof GuildForumThreadManager, holds: typeof ThreadChannel<true>];
47304731
GuildInviteManager: [manager: typeof GuildInviteManager, holds: typeof Invite];
4732+
GuildMessageManager: [manager: typeof GuildMessageManager, holds: typeof Message<true>];
47314733
GuildScheduledEventManager: [manager: typeof GuildScheduledEventManager, holds: typeof GuildScheduledEvent];
47324734
GuildStickerManager: [manager: typeof GuildStickerManager, holds: typeof Sticker];
4733-
GuildTextThreadManager: [manager: typeof GuildTextThreadManager, holds: typeof ThreadChannel];
4735+
GuildTextThreadManager: [manager: typeof GuildTextThreadManager, holds: typeof ThreadChannel<false>];
47344736
MessageManager: [manager: typeof MessageManager, holds: typeof Message];
47354737
// TODO: PermissionOverwriteManager: [manager: typeof PermissionOverwriteManager, holds: typeof PermissionOverwrites];
47364738
PresenceManager: [manager: typeof PresenceManager, holds: typeof Presence];
@@ -4748,11 +4750,18 @@ export type CacheConstructors = {
47484750
[K in keyof Caches]: Caches[K][0] & { name: K };
47494751
};
47504752

4753+
type OverriddenCaches =
4754+
| 'DMMessageManager'
4755+
| 'GuildForumThreadManager'
4756+
| 'GuildMessageManager'
4757+
| 'GuildTextThreadManager';
4758+
47514759
// This doesn't actually work the way it looks 😢.
47524760
// Narrowing the type of `manager.name` doesn't propagate type information to `holds` and the return type.
47534761
export type CacheFactory = (
4754-
manager: CacheConstructors[keyof Caches],
4762+
managerType: CacheConstructors[Exclude<keyof Caches, OverriddenCaches>],
47554763
holds: Caches[(typeof manager)['name']][1],
4764+
manager: CacheConstructors[keyof Caches],
47564765
) => (typeof manager)['prototype'] extends DataManager<infer K, infer V, any> ? Collection<K, V> : never;
47574766

47584767
export type CacheWithLimitsOptions = {

0 commit comments

Comments
 (0)