GuildEmoji.js
4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'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 {
/**
* @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
*/
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
* @type {Guild}
* @name GuildEmoji#guild
*/
_clone() {
const clone = super._clone();
clone._roles = this._roles.slice();
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
* @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>}
*/
async fetchAuthor() {
if (this.managed) {
throw new Error('EMOJI_MANAGED');
} else {
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME');
if (!this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS)) {
throw new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild);
}
}
const data = await this.client.api.guilds(this.guild.id).emojis(this.id).get();
this._patch(data);
return this.author;
}
/**
* 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;