Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use cache.get() for snowflakes, resolve() otherwise #10626

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/discord.js/src/client/actions/ThreadListSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ThreadListSyncAction extends Action {

if (data.channel_ids) {
for (const id of data.channel_ids) {
const channel = client.channels.resolve(id);
const channel = client.channels.cache.get(id);
if (channel) this.removeStale(channel);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/managers/BaseGuildEmojiManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class BaseGuildEmojiManager extends CachedManager {
* @returns {?GuildEmoji}
*/
resolve(emoji) {
if (emoji instanceof ReactionEmoji) return super.resolve(emoji.id);
if (emoji instanceof ApplicationEmoji) return super.resolve(emoji.id);
if (emoji instanceof ReactionEmoji) return super.cache.get(emoji.id) ?? null;
if (emoji instanceof ApplicationEmoji) return super.cache.get(emoji.id) ?? null;
return super.resolve(emoji);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/managers/GuildBanManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class GuildBanManager extends CachedManager {
reason: options.reason,
});
if (user instanceof GuildMember) return user;
const _user = this.client.users.resolve(id);
const _user = this.client.users.cache.get(id) ?? null;
Jiralite marked this conversation as resolved.
Show resolved Hide resolved
if (_user) {
return this.guild.members.resolve(_user) ?? _user;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/discord.js/src/managers/GuildChannelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class GuildChannelManager extends CachedManager {
* @returns {?(GuildChannel|ThreadChannel)}
*/
resolve(channel) {
if (channel instanceof ThreadChannel) return super.resolve(channel.id);
if (channel instanceof ThreadChannel) return super.cache.get(channel.id) ?? null;
return super.resolve(channel);
}

Expand Down Expand Up @@ -287,7 +287,7 @@ class GuildChannelManager extends CachedManager {
const resolvedChannel = this.resolve(channel);
if (!resolvedChannel) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'channel', 'GuildChannelResolvable');

const parent = options.parent && this.client.channels.resolveId(options.parent);
const parentId = options.parent && this.client.channels.resolveId(options.parent);

if (options.position !== undefined) {
await this.setPosition(resolvedChannel, options.position, { position: options.position, reason: options.reason });
Expand All @@ -298,8 +298,8 @@ class GuildChannelManager extends CachedManager {
);

if (options.lockPermissions) {
if (parent) {
const newParent = this.resolve(parent);
if (parentId) {
const newParent = this.cache.get(parentId);
if (newParent?.type === ChannelType.GuildCategory) {
permission_overwrites = newParent.permissionOverwrites.cache.map(overwrite =>
PermissionOverwrites.resolve(overwrite, this.guild),
Expand All @@ -322,7 +322,7 @@ class GuildChannelManager extends CachedManager {
user_limit: options.userLimit,
rtc_region: options.rtcRegion,
video_quality_mode: options.videoQualityMode,
parent_id: parent,
parent_id: parentId,
lock_permissions: options.lockPermissions,
rate_limit_per_user: options.rateLimitPerUser,
default_auto_archive_duration: options.defaultAutoArchiveDuration,
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/managers/GuildMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GuildMemberManager extends CachedManager {
const memberResolvable = super.resolve(member);
if (memberResolvable) return memberResolvable;
const userResolvable = this.client.users.resolveId(member);
if (userResolvable) return super.resolve(userResolvable);
if (userResolvable) return super.cache.get(userResolvable) ?? null;
return null;
}

Expand Down Expand Up @@ -144,7 +144,7 @@ class GuildMemberManager extends CachedManager {
*/
get me() {
return (
this.resolve(this.client.user.id) ??
this.cache.get(this.client.user.id) ??
(this.client.options.partials.includes(Partials.GuildMember)
? this._add({ user: { id: this.client.user.id } }, true)
: null)
Expand Down
8 changes: 4 additions & 4 deletions packages/discord.js/src/managers/PresenceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class PresenceManager extends CachedManager {
resolve(presence) {
const presenceResolvable = super.resolve(presence);
if (presenceResolvable) return presenceResolvable;
const UserResolvable = this.client.users.resolveId(presence);
return super.resolve(UserResolvable);
const userId = this.client.users.resolveId(presence);
return super.cache.get(userId) ?? null;
}

/**
Expand All @@ -50,8 +50,8 @@ class PresenceManager extends CachedManager {
resolveId(presence) {
const presenceResolvable = super.resolveId(presence);
if (presenceResolvable) return presenceResolvable;
const userResolvable = this.client.users.resolveId(presence);
return this.cache.has(userResolvable) ? userResolvable : null;
const userId = this.client.users.resolveId(presence);
return this.cache.has(userId) ? userId : null;
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/src/managers/ThreadMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ThreadMemberManager extends CachedManager {
* @readonly
*/
get me() {
return this.resolve(this.client.user.id);
return this.cache.get(this.client.user.id) ?? null;
}

/**
Expand All @@ -71,8 +71,8 @@ class ThreadMemberManager extends CachedManager {
resolve(member) {
const memberResolvable = super.resolve(member);
if (memberResolvable) return memberResolvable;
const userResolvable = this.client.users.resolveId(member);
if (userResolvable) return super.resolve(userResolvable);
const userId = this.client.users.resolveId(member);
if (userId) return super.cache.get(userId) ?? null;
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/GuildMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class GuildMember extends Base {
* @readonly
*/
get presence() {
return this.guild.presences.resolve(this.id);
return this.guild.presences.cache.get(this.id) ?? null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class GuildOnboardingPromptOption extends Base {
*/
get emoji() {
if (!this._emoji.id && !this._emoji.name) return null;
return this.client.emojis.resolve(this._emoji.id) ?? new Emoji(this.client, this._emoji);
return this.client.emojis.cache.get(this._emoji.id) ?? new Emoji(this.client, this._emoji);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/Invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Invite extends Base {
*/
this.guild ??= null;
if (data.guild) {
this.guild = this.client.guilds.resolve(data.guild.id) ?? new InviteGuild(this.client, data.guild);
this.guild = this.client.guilds.cache.get(data.guild.id) ?? new InviteGuild(this.client, data.guild);
}

if ('code' in data) {
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class Message extends Base {
* @readonly
*/
get thread() {
return this.channel?.threads?.resolve(this.id) ?? null;
return this.channel?.threads?.cache.get(this.id) ?? null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class PermissionOverwrites extends Base {
};
}

const userOrRole = guild.roles.resolve(overwrite.id) ?? guild.client.users.resolve(overwrite.id);
const userOrRole = guild.roles.cache.get(overwrite.id) ?? guild.client.users.cache.get(overwrite.id);
if (!userOrRole) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'parameter', 'User nor a Role');
const type = userOrRole instanceof Role ? OverwriteType.Role : OverwriteType.Member;

Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/PollAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PollAnswer extends Base {
*/
get emoji() {
if (!this._emoji || (!this._emoji.id && !this._emoji.name)) return null;
return this.client.emojis.resolve(this._emoji.id) ?? new Emoji(this.client, this._emoji);
return this.client.emojis.cache.get(this._emoji.id) ?? new Emoji(this.client, this._emoji);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/structures/ThreadMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ThreadMember extends Base {
* @readonly
*/
get guildMember() {
return this.member ?? this.thread.guild.members.resolve(this.id);
return this.member ?? this.thread.guild.members.cache.get(this.id) ?? null;
}

/**
Expand All @@ -87,7 +87,7 @@ class ThreadMember extends Base {
* @readonly
*/
get user() {
return this.client.users.resolve(this.id);
return this.client.users.cache.get(this.id) ?? null;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Webhook {
* The source guild of the webhook
* @type {?(Guild|APIGuild)}
*/
this.sourceGuild = this.client.guilds?.resolve(data.source_guild.id) ?? data.source_guild;
this.sourceGuild = this.client.guilds?.cache.get(data.source_guild.id) ?? data.source_guild;
} else {
this.sourceGuild ??= null;
}
Expand All @@ -118,7 +118,7 @@ class Webhook {
* The source channel of the webhook
* @type {?(AnnouncementChannel|APIChannel)}
*/
this.sourceChannel = this.client.channels?.resolve(data.source_channel?.id) ?? data.source_channel;
this.sourceChannel = this.client.channels?.cache.get(data.source_channel?.id) ?? data.source_channel;
} else {
this.sourceChannel ??= null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/WelcomeChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class WelcomeChannel extends Base {
* @type {GuildEmoji|Emoji}
*/
get emoji() {
return this.client.emojis.resolve(this._emoji.id) ?? new Emoji(this.client, this._emoji);
return this.client.emojis.cache.get(this._emoji.id) ?? new Emoji(this.client, this._emoji);
}
}

Expand Down