feat(GuildEmoji): cache the author (#4334)

Co-authored-by: Papaia <43409674+Papaia@users.noreply.github.com>
This commit is contained in:
Bence
2020-08-12 09:27:00 +02:00
committed by GitHub
parent 599cde3627
commit b7740d4859
2 changed files with 23 additions and 10 deletions

View File

@@ -11,13 +11,19 @@ const Permissions = require('../util/Permissions');
*/ */
class GuildEmoji extends BaseGuildEmoji { class GuildEmoji extends BaseGuildEmoji {
/** /**
* @name GuildEmoji
* @kind constructor
* @memberof GuildEmoji
* @param {Client} client The instantiating client * @param {Client} client The instantiating client
* @param {Object} data The data for the guild emoji * @param {Object} data The data for the guild emoji
* @param {Guild} guild The guild the guild emoji is part of * @param {Guild} guild The guild the guild emoji is part of
*/ */
constructor(client, data, guild) {
super(client, data, guild);
/**
* The user who created this emoji
* @type {?User}
*/
this.author = null;
}
/** /**
* The guild this emoji is part of * The guild this emoji is part of
@@ -31,6 +37,11 @@ class GuildEmoji extends BaseGuildEmoji {
return clone; return clone;
} }
_patch(data) {
super._patch(data);
if (typeof data.user !== 'undefined') this.author = this.client.users.add(data.user);
}
/** /**
* Whether the emoji is deletable by the client user * Whether the emoji is deletable by the client user
* @type {boolean} * @type {boolean}
@@ -54,20 +65,21 @@ class GuildEmoji extends BaseGuildEmoji {
* Fetches the author for this emoji * Fetches the author for this emoji
* @returns {Promise<User>} * @returns {Promise<User>}
*/ */
fetchAuthor() { async fetchAuthor() {
if (this.managed) { if (this.managed) {
return Promise.reject(new Error('EMOJI_MANAGED')); throw new Error('EMOJI_MANAGED');
} else { } else {
if (!this.guild.me) return Promise.reject(new Error('GUILD_UNCACHED_ME')); if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME');
if (!this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS)) { if (!this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS)) {
return Promise.reject(new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild)); throw new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild);
} }
} }
return this.client.api const data = await this.client.api
.guilds(this.guild.id) .guilds(this.guild.id)
.emojis(this.id) .emojis(this.id)
.get() .get();
.then(emoji => this.client.users.add(emoji.user)); this._patch(data);
return this.author;
} }
/** /**

1
typings/index.d.ts vendored
View File

@@ -793,6 +793,7 @@ declare module 'discord.js' {
export class GuildEmoji extends BaseGuildEmoji { export class GuildEmoji extends BaseGuildEmoji {
public readonly deletable: boolean; public readonly deletable: boolean;
public guild: Guild; public guild: Guild;
public author: User | null;
public readonly roles: GuildEmojiRoleManager; public readonly roles: GuildEmojiRoleManager;
public readonly url: string; public readonly url: string;
public delete(reason?: string): Promise<GuildEmoji>; public delete(reason?: string): Promise<GuildEmoji>;