Skip to content

refactor(Client)!: Remove emojis getter #10754

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

Merged
merged 19 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
14 changes: 0 additions & 14 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const { ActionsManager } = require('./actions/ActionsManager.js');
const { ClientVoiceManager } = require('./voice/ClientVoiceManager.js');
const { PacketHandlers } = require('./websocket/handlers/index.js');
const { DiscordjsError, DiscordjsTypeError, ErrorCodes } = require('../errors/index.js');
const { BaseGuildEmojiManager } = require('../managers/BaseGuildEmojiManager.js');
const { ChannelManager } = require('../managers/ChannelManager.js');
const { GuildManager } = require('../managers/GuildManager.js');
const { UserManager } = require('../managers/UserManager.js');
Expand Down Expand Up @@ -217,19 +216,6 @@ class Client extends BaseClient {
this._attachEvents();
}

/**
* A manager of all the custom emojis that the client has access to
* @type {BaseGuildEmojiManager}
* @readonly
*/
get emojis() {
const emojis = new BaseGuildEmojiManager(this);
for (const guild of this.guilds.cache.values()) {
if (guild.available) for (const emoji of guild.emojis.cache.values()) emojis.cache.set(emoji.id, emoji);
}
return emojis;
}

/**
* Time at which the client was last regarded as being in the {@link Status.Ready} state
* (each time the client disconnects and successfully reconnects, this will be overwritten)
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.cache.get(this._emoji.id) ?? new Emoji(this.client, this._emoji);
return this.guild.emojis.cache.get(this._emoji.id) ?? new Emoji(this.client, this._emoji);
}
}

Expand Down
11 changes: 3 additions & 8 deletions packages/discord.js/src/structures/MessageReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { ApplicationEmoji } = require('./ApplicationEmoji.js');
const { GuildEmoji } = require('./GuildEmoji.js');
const { ReactionEmoji } = require('./ReactionEmoji.js');
const { ReactionUserManager } = require('../managers/ReactionUserManager.js');
const { flatten } = require('../util/Util.js');
const { flatten, resolveGuildEmoji } = require('../util/Util.js');

/**
* Represents a reaction to a message.
Expand Down Expand Up @@ -127,14 +127,9 @@ class MessageReaction {
this._emoji = emoji;
return emoji;
}
const emojis = this.message.client.emojis.cache;
if (emojis.has(this._emoji.id)) {
const emoji = emojis.get(this._emoji.id);
this._emoji = emoji;
return emoji;
}
}
return this._emoji;
const emoji = resolveGuildEmoji(this.client, this._emoji.id)
return emoji ?? this._emoji;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/discord.js/src/structures/PollAnswer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { resolveGuildEmoji } = require('../util/Util.js');
const { Base } = require('./Base.js');
const { Emoji } = require('./Emoji.js');

Expand Down Expand Up @@ -61,7 +62,7 @@ class PollAnswer extends Base {
*/
get emoji() {
if (!this._emoji || (!this._emoji.id && !this._emoji.name)) return null;
return this.client.emojis.cache.get(this._emoji.id) ?? new Emoji(this.client, this._emoji);
return resolveGuildEmoji(this.client, this._emoji.id) ?? new Emoji(this.client, this._emoji);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/discord.js/src/structures/WelcomeChannel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { resolveGuildEmoji } = require('../util/Util.js');
const { Base } = require('./Base.js');
const { Emoji } = require('./Emoji.js');

Expand Down Expand Up @@ -53,7 +54,7 @@ class WelcomeChannel extends Base {
* @type {GuildEmoji|Emoji}
*/
get emoji() {
return this.client.emojis.cache.get(this._emoji.id) ?? new Emoji(this.client, this._emoji);
return this.guild.emojis.cache.get(this._emoji.id) ?? new Emoji(this.client, this._emoji)
}
}

Expand Down
27 changes: 27 additions & 0 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ function resolvePartialEmoji(emoji) {
return { id, name, animated: Boolean(animated) };
}

/**
* Resolve an emoji object from a Guild
* @param {Client} client The Client
* @param {string} emojiId The id of the emoji
* @returns {GuildEmoji | undefined}
* @private
*/
export function resolveGuildEmoji(client, emojiId) {
for (const guild of client.guilds.cache.values()) {
if (!guild.available) {
continue;
}

const emoji = guild.emojis.cache.get(emojiId);

if (emoji) {
return emoji;
}
}

return null;
}

/**
* Options used to make an error object.
* @typedef {Object} MakeErrorOptions
Expand Down Expand Up @@ -503,6 +526,7 @@ exports.flatten = flatten;
exports.fetchRecommendedShardCount = fetchRecommendedShardCount;
exports.parseEmoji = parseEmoji;
exports.resolvePartialEmoji = resolvePartialEmoji;
exports.resolveGuildEmoji = resolveGuildEmoji;
exports.makeError = makeError;
exports.makePlainError = makePlainError;
exports.getSortableGroupTypes = getSortableGroupTypes;
Expand All @@ -518,7 +542,10 @@ exports.parseWebhookURL = parseWebhookURL;
exports.transformResolved = transformResolved;
exports.resolveSKUId = resolveSKUId;


// Fixes Circular
const { Attachment } = require('../structures/Attachment.js');
const { GuildChannel } = require('../structures/GuildChannel.js');
const { SKU } = require('../structures/SKU.js');
const { GuildEmoji } = require('../structures/GuildEmoji.js');

4 changes: 3 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,6 @@ export class Client<Ready extends boolean = boolean> extends BaseClient<ClientEv

public application: If<Ready, ClientApplication>;
public channels: ChannelManager;
public get emojis(): BaseGuildEmojiManager;
public guilds: GuildManager;
public lastPingTimestamps: ReadonlyCollection<number, number>;
public options: Omit<ClientOptions, 'intents'> & { intents: IntentsBitField };
Expand Down Expand Up @@ -3459,6 +3458,7 @@ export function discordSort<Key, Value extends { rawPosition: number; id: Snowfl
export function cleanCodeBlockContent(text: string): string;
export function fetchRecommendedShardCount(token: string, options?: FetchRecommendedShardCountOptions): Promise<number>;
export function flatten(obj: unknown, ...props: Record<string, boolean | string>[]): unknown;

/** @internal */
export function makeError(obj: MakeErrorOptions): Error;
/** @internal */
Expand All @@ -3477,6 +3477,8 @@ export function resolveColor(color: ColorResolvable): number;
export function resolvePartialEmoji(emoji: Snowflake): PartialEmojiOnlyId;
/** @internal */
export function resolvePartialEmoji(emoji: Emoji | EmojiIdentifierResolvable): PartialEmoji | null;
/** @internal */
export function resolveGuildEmoji(client: Client, emojiId: string): GuildEmoji | null;
export function verifyString(data: string, error?: typeof Error, errorMessage?: string, allowEmpty?: boolean): string;
/** @internal */
export function setPosition<Item extends Channel | Role>(
Expand Down
Loading