mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-04 10:00:08 +00:00
* feat(GuildPreview): method — fetchGuildPreview * feat(GuildPreview): structure — GuildPreview * feat(GuildPreview): update typings * fix(GuildPreview): remove typedef for Features — already exists * refactor(GuildPreview): update JSDocs & method * feat(GuildPreview): implement DiscoverySplash function * fix(GuildPreview): description & error handling for id * fix(GuildPreview): misleading description, assign emojis correctly * feat(GuildPreview): func DisplaySplash & GuildPreviewEmoji interface * fix(Typings): satisfy TSLint * fix(GuildPreview): toJSON - returns a value now * feat(GuildPreview): add fetchPreview method on instance of Guild * feat(GuildPreview): update typings * fix: missing client constructor * fix: typo in typings * feat(BaseEmoji): implement BaseEmoji — parent for emoji instances * feat(BaseEmoji): refactor - GuildEmoji extends BaseEmoji now * feat(BaseEmoji): refactor - adjust emojis prop to BaseEmoji instance * feat(BaseEmoji): not documented fully - GuildPreviewEmoji * feat(BaseEmoji): update typings * fix: remove duplicate typing properties - inherited * fix: remove redundant methods & properties - inherited / already set * fix: let -> const * fix: typings - put BaseEmoji after BaseClient * fix: remove _clone method - redundant * refactor(GuildPreview): emojis should be a collection * refactor: rename base class, move relevant props there and expose roles * fix(GuildPreview): update emojis in _patch * fix(Typings): remove empty line, add Client#fetchGuildPreview * feat: export GuildPreview * fix(Typings): add GuildPreview#discoverSplash, icon, and splash Co-authored-by: LxveFades <twitchisadeck@gmail.com> Co-authored-by: Crawl <icrawltogo@gmail.com> Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
160 lines
4.2 KiB
JavaScript
160 lines
4.2 KiB
JavaScript
'use strict';
|
|
|
|
const BaseGuildEmoji = require('./BaseGuildEmoji');
|
|
const { Error } = require('../errors');
|
|
const GuildEmojiRoleManager = require('../managers/GuildEmojiRoleManager');
|
|
const Permissions = require('../util/Permissions');
|
|
|
|
/**
|
|
* Represents a custom emoji.
|
|
* @extends {BaseGuildEmoji}
|
|
*/
|
|
class GuildEmoji extends BaseGuildEmoji {
|
|
/**
|
|
* @name GuildEmoji
|
|
* @kind constructor
|
|
* @memberof GuildEmoji
|
|
* @param {Client} client The instantiating client
|
|
* @param {Object} data The data for the guild emoji
|
|
* @param {Guild} guild The guild the guild emoji is part of
|
|
*/
|
|
|
|
/**
|
|
* The guild this emoji is part of
|
|
* @type {Guild}
|
|
* @name GuildEmoji#guild
|
|
*/
|
|
|
|
_clone() {
|
|
const clone = super._clone();
|
|
clone._roles = this._roles.slice();
|
|
return clone;
|
|
}
|
|
|
|
/**
|
|
* Whether the emoji is deletable by the client user
|
|
* @type {boolean}
|
|
* @readonly
|
|
*/
|
|
get deletable() {
|
|
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME');
|
|
return !this.managed && this.guild.me.hasPermission(Permissions.FLAGS.MANAGE_EMOJIS);
|
|
}
|
|
|
|
/**
|
|
* A manager for roles this emoji is active for.
|
|
* @type {GuildEmojiRoleManager}
|
|
* @readonly
|
|
*/
|
|
get roles() {
|
|
return new GuildEmojiRoleManager(this);
|
|
}
|
|
|
|
/**
|
|
* Fetches the author for this emoji
|
|
* @returns {Promise<User>}
|
|
*/
|
|
fetchAuthor() {
|
|
if (this.managed) {
|
|
return Promise.reject(new Error('EMOJI_MANAGED'));
|
|
} else {
|
|
if (!this.guild.me) return Promise.reject(new Error('GUILD_UNCACHED_ME'));
|
|
if (!this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS)) {
|
|
return Promise.reject(new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild));
|
|
}
|
|
}
|
|
return this.client.api
|
|
.guilds(this.guild.id)
|
|
.emojis(this.id)
|
|
.get()
|
|
.then(emoji => this.client.users.add(emoji.user));
|
|
}
|
|
|
|
/**
|
|
* Data for editing an emoji.
|
|
* @typedef {Object} GuildEmojiEditData
|
|
* @property {string} [name] The name of the emoji
|
|
* @property {Collection<Snowflake, Role>|RoleResolvable[]} [roles] Roles to restrict emoji to
|
|
*/
|
|
|
|
/**
|
|
* Edits the emoji.
|
|
* @param {GuildEmojiEditData} data The new data for the emoji
|
|
* @param {string} [reason] Reason for editing this emoji
|
|
* @returns {Promise<GuildEmoji>}
|
|
* @example
|
|
* // Edit an emoji
|
|
* emoji.edit({ name: 'newemoji' })
|
|
* .then(e => console.log(`Edited emoji ${e}`))
|
|
* .catch(console.error);
|
|
*/
|
|
edit(data, reason) {
|
|
const roles = data.roles ? data.roles.map(r => r.id || r) : undefined;
|
|
return this.client.api
|
|
.guilds(this.guild.id)
|
|
.emojis(this.id)
|
|
.patch({
|
|
data: {
|
|
name: data.name,
|
|
roles,
|
|
},
|
|
reason,
|
|
})
|
|
.then(newData => {
|
|
const clone = this._clone();
|
|
clone._patch(newData);
|
|
return clone;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Sets the name of the emoji.
|
|
* @param {string} name The new name for the emoji
|
|
* @param {string} [reason] Reason for changing the emoji's name
|
|
* @returns {Promise<GuildEmoji>}
|
|
*/
|
|
setName(name, reason) {
|
|
return this.edit({ name }, reason);
|
|
}
|
|
|
|
/**
|
|
* Deletes the emoji.
|
|
* @param {string} [reason] Reason for deleting the emoji
|
|
* @returns {Promise<GuildEmoji>}
|
|
*/
|
|
delete(reason) {
|
|
return this.client.api
|
|
.guilds(this.guild.id)
|
|
.emojis(this.id)
|
|
.delete({ reason })
|
|
.then(() => this);
|
|
}
|
|
|
|
/**
|
|
* Whether this emoji is the same as another one.
|
|
* @param {GuildEmoji|Object} other The emoji to compare it to
|
|
* @returns {boolean} Whether the emoji is equal to the given emoji or not
|
|
*/
|
|
equals(other) {
|
|
if (other instanceof GuildEmoji) {
|
|
return (
|
|
other.id === this.id &&
|
|
other.name === this.name &&
|
|
other.managed === this.managed &&
|
|
other.requiresColons === this.requiresColons &&
|
|
other.roles.cache.size === this.roles.cache.size &&
|
|
other.roles.cache.every(role => this.roles.cache.has(role.id))
|
|
);
|
|
} else {
|
|
return (
|
|
other.id === this.id &&
|
|
other.name === this.name &&
|
|
other.roles.length === this.roles.cache.size &&
|
|
other.roles.every(role => this.roles.cache.has(role))
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = GuildEmoji;
|