diff --git a/esm/discord.mjs b/esm/discord.mjs index 8ebf08652233..d4625a0b9448 100644 --- a/esm/discord.mjs +++ b/esm/discord.mjs @@ -28,6 +28,7 @@ export const { UserFlags, Util, version, + BaseGuildEmojiManager, ChannelManager, GuildChannelManager, GuildEmojiManager, diff --git a/src/client/Client.js b/src/client/Client.js index 5847f7573708..a8ec7e496e4e 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -5,8 +5,8 @@ const ActionsManager = require('./actions/ActionsManager'); const ClientVoiceManager = require('./voice/ClientVoiceManager'); const WebSocketManager = require('./websocket/WebSocketManager'); const { Error, TypeError, RangeError } = require('../errors'); +const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager'); const ChannelManager = require('../managers/ChannelManager'); -const GuildEmojiManager = require('../managers/GuildEmojiManager'); const GuildManager = require('../managers/GuildManager'); const UserManager = require('../managers/UserManager'); const ShardClientUtil = require('../sharding/ShardClientUtil'); @@ -170,7 +170,7 @@ class Client extends BaseClient { * @readonly */ get emojis() { - const emojis = new GuildEmojiManager({ client: this }); + 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); } diff --git a/src/index.js b/src/index.js index 7319115fe1ef..d83e1bfa83e6 100644 --- a/src/index.js +++ b/src/index.js @@ -33,6 +33,7 @@ module.exports = { version: require('../package.json').version, // Managers + BaseGuildEmojiManager: require('./managers/BaseGuildEmojiManager'), ChannelManager: require('./managers/ChannelManager'), GuildChannelManager: require('./managers/GuildChannelManager'), GuildEmojiManager: require('./managers/GuildEmojiManager'), diff --git a/src/managers/BaseGuildEmojiManager.js b/src/managers/BaseGuildEmojiManager.js new file mode 100644 index 000000000000..5a188d72c1ad --- /dev/null +++ b/src/managers/BaseGuildEmojiManager.js @@ -0,0 +1,80 @@ +'use strict'; + +const BaseManager = require('./BaseManager'); +const GuildEmoji = require('../structures/GuildEmoji'); +const ReactionEmoji = require('../structures/ReactionEmoji'); +const { parseEmoji } = require('../util/Util'); + +/** + * Holds methods to resolve GuildEmojis and stores their cache. + * @extends {BaseManager} + */ +class BaseGuildEmojiManager extends BaseManager { + constructor(client, iterable) { + super(client, iterable, GuildEmoji); + } + + /** + * The cache of GuildEmojis + * @type {Collection} + * @name BaseGuildEmojiManager#cache + */ + + /** + * Data that can be resolved into a GuildEmoji object. This can be: + * * A custom emoji ID + * * A GuildEmoji object + * * A ReactionEmoji object + * @typedef {Snowflake|GuildEmoji|ReactionEmoji} EmojiResolvable + */ + + /** + * Resolves an EmojiResolvable to an Emoji object. + * @param {EmojiResolvable} emoji The Emoji resolvable to identify + * @returns {?GuildEmoji} + */ + resolve(emoji) { + if (emoji instanceof ReactionEmoji) return super.resolve(emoji.id); + return super.resolve(emoji); + } + + /** + * Resolves an EmojiResolvable to an Emoji ID string. + * @param {EmojiResolvable} emoji The Emoji resolvable to identify + * @returns {?Snowflake} + */ + resolveID(emoji) { + if (emoji instanceof ReactionEmoji) return emoji.id; + return super.resolveID(emoji); + } + + /** + * Data that can be resolved to give an emoji identifier. This can be: + * * The unicode representation of an emoji + * * The ``, `<:name:id>`, `a:name:id` or `name:id` emoji identifier string of an emoji + * * An EmojiResolvable + * @typedef {string|EmojiResolvable} EmojiIdentifierResolvable + */ + + /** + * Resolves an EmojiResolvable to an emoji identifier. + * @param {EmojiIdentifierResolvable} emoji The emoji resolvable to resolve + * @returns {?string} + */ + resolveIdentifier(emoji) { + const emojiResolvable = this.resolve(emoji); + if (emojiResolvable) return emojiResolvable.identifier; + if (emoji instanceof ReactionEmoji) return emoji.identifier; + if (typeof emoji === 'string') { + const res = parseEmoji(emoji); + if (res && res.name.length) { + emoji = `${res.animated ? 'a:' : ''}${res.name}${res.id ? `:${res.id}` : ''}`; + } + if (!emoji.includes('%')) return encodeURIComponent(emoji); + return emoji; + } + return null; + } +} + +module.exports = BaseGuildEmojiManager; diff --git a/src/managers/GuildEmojiManager.js b/src/managers/GuildEmojiManager.js index acf3576e80fd..244194f06e09 100644 --- a/src/managers/GuildEmojiManager.js +++ b/src/managers/GuildEmojiManager.js @@ -1,20 +1,18 @@ 'use strict'; -const BaseManager = require('./BaseManager'); +const BaseGuildEmojiManager = require('./BaseGuildEmojiManager'); const { TypeError } = require('../errors'); -const GuildEmoji = require('../structures/GuildEmoji'); -const ReactionEmoji = require('../structures/ReactionEmoji'); const Collection = require('../util/Collection'); const DataResolver = require('../util/DataResolver'); -const { parseEmoji } = require('../util/Util'); /** * Manages API methods for GuildEmojis and stores their cache. - * @extends {BaseManager} + * @extends {BaseGuildEmojiManager} */ -class GuildEmojiManager extends BaseManager { +class GuildEmojiManager extends BaseGuildEmojiManager { constructor(guild, iterable) { - super(guild.client, iterable, GuildEmoji); + super(guild.client, iterable); + /** * The guild this manager belongs to * @type {Guild} @@ -22,12 +20,6 @@ class GuildEmojiManager extends BaseManager { this.guild = guild; } - /** - * The cache of GuildEmojis - * @type {Collection} - * @name GuildEmojiManager#cache - */ - add(data, cache) { return super.add(data, cache, { extras: [this.guild] }); } @@ -74,62 +66,6 @@ class GuildEmojiManager extends BaseManager { .emojis.post({ data, reason }) .then(emoji => this.client.actions.GuildEmojiCreate.handle(this.guild, emoji).emoji); } - - /** - * Data that can be resolved into an GuildEmoji object. This can be: - * * A custom emoji ID - * * A GuildEmoji object - * * A ReactionEmoji object - * @typedef {Snowflake|GuildEmoji|ReactionEmoji} EmojiResolvable - */ - - /** - * Resolves an EmojiResolvable to an Emoji object. - * @param {EmojiResolvable} emoji The Emoji resolvable to identify - * @returns {?GuildEmoji} - */ - resolve(emoji) { - if (emoji instanceof ReactionEmoji) return super.resolve(emoji.id); - return super.resolve(emoji); - } - - /** - * Resolves an EmojiResolvable to an Emoji ID string. - * @param {EmojiResolvable} emoji The Emoji resolvable to identify - * @returns {?Snowflake} - */ - resolveID(emoji) { - if (emoji instanceof ReactionEmoji) return emoji.id; - return super.resolveID(emoji); - } - - /** - * Data that can be resolved to give an emoji identifier. This can be: - * * The unicode representation of an emoji - * * The ``, `<:name:id>`, `:name:id` or `a:name:id` emoji identifier string of an emoji - * * An EmojiResolvable - * @typedef {string|EmojiResolvable} EmojiIdentifierResolvable - */ - - /** - * Resolves an EmojiResolvable to an emoji identifier. - * @param {EmojiIdentifierResolvable} emoji The emoji resolvable to resolve - * @returns {?string} - */ - resolveIdentifier(emoji) { - const emojiResolvable = this.resolve(emoji); - if (emojiResolvable) return emojiResolvable.identifier; - if (emoji instanceof ReactionEmoji) return emoji.identifier; - if (typeof emoji === 'string') { - const res = parseEmoji(emoji); - if (res && res.name.length) { - emoji = `${res.animated ? 'a:' : ''}${res.name}${res.id ? `:${res.id}` : ''}`; - } - if (!emoji.includes('%')) return encodeURIComponent(emoji); - else return emoji; - } - return null; - } } module.exports = GuildEmojiManager; diff --git a/typings/index.d.ts b/typings/index.d.ts index a54a3540f91b..38c575fb364f 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -199,7 +199,7 @@ declare module 'discord.js' { private _validateOptions(options?: ClientOptions): void; public channels: ChannelManager; - public readonly emojis: GuildEmojiManager; + public readonly emojis: BaseGuildEmojiManager; public guilds: GuildManager; public readyAt: Date | null; public readonly readyTimestamp: number | null; @@ -1870,11 +1870,6 @@ declare module 'discord.js' { //#region Managers - export class ChannelManager extends BaseManager { - constructor(client: Client, iterable: Iterable); - public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise; - } - export abstract class BaseManager { constructor(client: Client, iterable: Iterable, holds: Constructable, cacheType: Collection); public holds: Constructable; @@ -1887,6 +1882,16 @@ declare module 'discord.js' { public valueOf(): Collection; } + export class BaseGuildEmojiManager extends BaseManager { + constructor(client: Client, iterable?: Iterable); + public resolveIdentifier(emoji: EmojiIdentifierResolvable): string | null; + } + + export class ChannelManager extends BaseManager { + constructor(client: Client, iterable: Iterable); + public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise; + } + export class GuildChannelManager extends BaseManager { constructor(guild: Guild, iterable?: Iterable); public guild: Guild; @@ -1899,7 +1904,7 @@ declare module 'discord.js' { ): Promise; } - export class GuildEmojiManager extends BaseManager { + export class GuildEmojiManager extends BaseGuildEmojiManager { constructor(guild: Guild, iterable?: Iterable); public guild: Guild; public create( @@ -1907,7 +1912,6 @@ declare module 'discord.js' { name: string, options?: GuildEmojiCreateOptions, ): Promise; - public resolveIdentifier(emoji: EmojiIdentifierResolvable): string | null; } export class GuildEmojiRoleManager {