From e121454444d610f998fc5772c70d2cb433219e81 Mon Sep 17 00:00:00 2001 From: Vaporox Date: Sat, 24 Oct 2020 12:36:50 +0200 Subject: [PATCH 1/4] refactor: move ChannelManager under BaseManager --- typings/index.d.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 041039f9c884..5bbb779fa550 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1886,11 +1886,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; @@ -1903,6 +1898,11 @@ declare module 'discord.js' { public valueOf(): Collection; } + 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; From 6066bd5686e0e092191defdd427758dcb31a7517 Mon Sep 17 00:00:00 2001 From: Vaporox Date: Sat, 24 Oct 2020 12:49:19 +0200 Subject: [PATCH 2/4] feat: BaseGuildEmojiManager --- esm/discord.mjs | 1 + src/client/Client.js | 4 +- src/index.js | 1 + src/managers/BaseGuildEmojiManager.js | 80 +++++++++++++++++++++++++++ src/managers/GuildEmojiManager.js | 74 ++----------------------- typings/index.d.ts | 10 +++- 6 files changed, 96 insertions(+), 74 deletions(-) create mode 100644 src/managers/BaseGuildEmojiManager.js diff --git a/esm/discord.mjs b/esm/discord.mjs index d8a451f1f654..fbb13b66355b 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 fd02197cdaa1..1676ff9fe407 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'); @@ -169,7 +169,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 32b98a075154..b9ed66473476 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..8b8597c56495 --- /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 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>`, `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); + else 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 5bbb779fa550..7c372580e436 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; @@ -1898,6 +1898,11 @@ 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; @@ -1915,7 +1920,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( @@ -1923,7 +1928,6 @@ declare module 'discord.js' { name: string, options?: GuildEmojiCreateOptions, ): Promise; - public resolveIdentifier(emoji: EmojiIdentifierResolvable): string | null; } export class GuildEmojiRoleManager { From 9d02078c564dc4fbf2e0ff6656904e065dac67c8 Mon Sep 17 00:00:00 2001 From: Vaporox Date: Sat, 24 Oct 2020 12:54:57 +0200 Subject: [PATCH 3/4] docs: typo --- src/managers/BaseGuildEmojiManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managers/BaseGuildEmojiManager.js b/src/managers/BaseGuildEmojiManager.js index 8b8597c56495..42f004d922a6 100644 --- a/src/managers/BaseGuildEmojiManager.js +++ b/src/managers/BaseGuildEmojiManager.js @@ -21,7 +21,7 @@ class BaseGuildEmojiManager extends BaseManager { */ /** - * Data that can be resolved into an GuildEmoji object. This can be: + * Data that can be resolved into a GuildEmoji object. This can be: * * A custom emoji ID * * A GuildEmoji object * * A ReactionEmoji object From 4762b7cbb7e24b8a8c740463a9ef5ebfa79f0dcc Mon Sep 17 00:00:00 2001 From: Jan <66554238+Vaporox@users.noreply.github.com> Date: Sun, 22 Nov 2020 18:44:02 +0100 Subject: [PATCH 4/4] Update src/managers/BaseGuildEmojiManager.js Co-authored-by: Vlad Frangu --- src/managers/BaseGuildEmojiManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managers/BaseGuildEmojiManager.js b/src/managers/BaseGuildEmojiManager.js index 42f004d922a6..5a188d72c1ad 100644 --- a/src/managers/BaseGuildEmojiManager.js +++ b/src/managers/BaseGuildEmojiManager.js @@ -71,7 +71,7 @@ class BaseGuildEmojiManager extends BaseManager { emoji = `${res.animated ? 'a:' : ''}${res.name}${res.id ? `:${res.id}` : ''}`; } if (!emoji.includes('%')) return encodeURIComponent(emoji); - else return emoji; + return emoji; } return null; }