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

feat: BaseGuildEmojiManager #4934

Merged
merged 4 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions esm/discord.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const {
UserFlags,
Util,
version,
BaseGuildEmojiManager,
ChannelManager,
GuildChannelManager,
GuildEmojiManager,
Expand Down
4 changes: 2 additions & 2 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
80 changes: 80 additions & 0 deletions src/managers/BaseGuildEmojiManager.js
Original file line number Diff line number Diff line change
@@ -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<Snowflake, GuildEmoji>}
* @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 `<a:name:id>`, `<: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;
74 changes: 5 additions & 69 deletions src/managers/GuildEmojiManager.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
'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}
*/
this.guild = guild;
}

/**
* The cache of GuildEmojis
* @type {Collection<Snowflake, GuildEmoji>}
* @name GuildEmojiManager#cache
*/

add(data, cache) {
return super.add(data, cache, { extras: [this.guild] });
}
Expand Down Expand Up @@ -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 `<a:name:id>`, `<: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;
20 changes: 12 additions & 8 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1886,11 +1886,6 @@ declare module 'discord.js' {

//#region Managers

export class ChannelManager extends BaseManager<Snowflake, Channel, ChannelResolvable> {
constructor(client: Client, iterable: Iterable<any>);
public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise<Channel>;
}

export abstract class BaseManager<K, Holds, R> {
constructor(client: Client, iterable: Iterable<any>, holds: Constructable<Holds>, cacheType: Collection<K, Holds>);
public holds: Constructable<Holds>;
Expand All @@ -1903,6 +1898,16 @@ declare module 'discord.js' {
public valueOf(): Collection<K, Holds>;
}

export class BaseGuildEmojiManager extends BaseManager<Snowflake, GuildEmoji, EmojiResolvable> {
constructor(client: Client, iterable?: Iterable<any>);
public resolveIdentifier(emoji: EmojiIdentifierResolvable): string | null;
}

export class ChannelManager extends BaseManager<Snowflake, Channel, ChannelResolvable> {
constructor(client: Client, iterable: Iterable<any>);
public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise<Channel>;
}

export class GuildChannelManager extends BaseManager<Snowflake, GuildChannel, GuildChannelResolvable> {
constructor(guild: Guild, iterable?: Iterable<any>);
public guild: Guild;
Expand All @@ -1915,15 +1920,14 @@ declare module 'discord.js' {
): Promise<TextChannel | VoiceChannel | CategoryChannel>;
}

export class GuildEmojiManager extends BaseManager<Snowflake, GuildEmoji, EmojiResolvable> {
export class GuildEmojiManager extends BaseGuildEmojiManager {
constructor(guild: Guild, iterable?: Iterable<any>);
public guild: Guild;
public create(
attachment: BufferResolvable | Base64Resolvable,
name: string,
options?: GuildEmojiCreateOptions,
): Promise<GuildEmoji>;
public resolveIdentifier(emoji: EmojiIdentifierResolvable): string | null;
}

export class GuildEmojiRoleManager {
Expand Down