forked from discordjs/discord.js
/
Emoji.js
273 lines (242 loc) · 6.7 KB
/
Emoji.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const Constants = require('../util/Constants');
const Collection = require('../util/Collection');
const Permissions = require('../util/Permissions');
const Snowflake = require('../util/Snowflake');
/**
* Represents a custom emoji.
*/
class Emoji {
constructor(guild, data) {
/**
* The client that instantiated this object
* @name Emoji#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: guild.client });
/**
* The guild this emoji is part of
* @type {Guild}
*/
this.guild = guild;
/**
* Whether this emoji has been deleted
* @type {boolean}
*/
this.deleted = false;
this.setup(data);
}
setup(data) {
/**
* The ID of the emoji
* @type {Snowflake}
*/
this.id = data.id;
/**
* The name of the emoji
* @type {string}
*/
this.name = data.name;
/**
* Whether or not this emoji requires colons surrounding it
* @type {boolean}
*/
this.requiresColons = data.require_colons;
/**
* Whether this emoji is managed by an external service
* @type {boolean}
*/
this.managed = data.managed;
/**
* Whether this emoji is animated
* @type {boolean}
*/
this.animated = data.animated;
/**
* Whether this emoji is available
* @type {boolean}
* @name Emoji#available
*/
if (typeof data.available !== 'undefined') this.available = data.available;
this._roles = data.roles;
}
/**
* The timestamp the emoji was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return Snowflake.deconstruct(this.id).timestamp;
}
/**
* The time the emoji was created
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* Whether the emoji is deletable by the client user
* @type {boolean}
* @readonly
*/
get deletable() {
return !this.managed && this.guild.me.hasPermission(Permissions.FLAGS.MANAGE_EMOJIS);
}
/**
* A collection of roles this emoji is active for (empty if all), mapped by role ID
* @type {Collection<Snowflake, Role>}
* @readonly
*/
get roles() {
const roles = new Collection();
for (const role of this._roles) {
if (this.guild.roles.has(role)) roles.set(role, this.guild.roles.get(role));
}
return roles;
}
/**
* The URL to the emoji file
* @type {string}
* @readonly
*/
get url() {
return Constants.Endpoints.CDN(this.client.options.http.cdn).Emoji(this.id, this.animated ? 'gif' : 'png');
}
/**
* The identifier of this emoji, used for message reactions
* @type {string}
* @readonly
*/
get identifier() {
if (this.id) return `${this.name}:${this.id}`;
return encodeURIComponent(this.name);
}
/**
* Data for editing an emoji.
* @typedef {Object} EmojiEditData
* @property {string} [name] The name of the emoji
* @property {Collection<Snowflake, Role>|Array<Snowflake|Role>} [roles] Roles to restrict emoji to
*/
/**
* Edits the emoji.
* @param {EmojiEditData} data The new data for the emoji
* @param {string} [reason] Reason for editing this emoji
* @returns {Promise<Emoji>}
* @example
* // Edit an emoji
* emoji.edit({name: 'newemoji'})
* .then(e => console.log(`Edited emoji ${e}`))
* .catch(console.error);
*/
edit(data, reason) {
return this.client.rest.methods.updateEmoji(this, data, reason);
}
/**
* Set the name of the emoji.
* @param {string} name The new name for the emoji
* @param {string} [reason] The reason for changing the emoji's name
* @returns {Promise<Emoji>}
*/
setName(name, reason) {
return this.edit({ name }, reason);
}
/**
* Fetches the author for this emoji
* @returns {Promise<User>}
*/
fetchAuthor() {
if (this.managed) return Promise.reject(new Error('Emoji is managed and has no Author.'));
if (!this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS)) {
return Promise.reject(
new Error(`Client must have Manage Emoji permission in guild ${this.guild} to see emoji authors.`)
);
}
return this.client.rest.makeRequest('get', Constants.Endpoints.Guild(this.guild).Emoji(this.id), true)
.then(emoji => this.client.dataManager.newUser(emoji.user));
}
/**
* Add a role to the list of roles that can use this emoji.
* @param {Role} role The role to add
* @returns {Promise<Emoji>}
*/
addRestrictedRole(role) {
return this.addRestrictedRoles([role]);
}
/**
* Add multiple roles to the list of roles that can use this emoji.
* @param {Role[]} roles Roles to add
* @returns {Promise<Emoji>}
*/
addRestrictedRoles(roles) {
const newRoles = new Collection(this.roles);
for (const role of roles) {
if (this.guild.roles.has(role.id)) newRoles.set(role.id, role);
}
return this.edit({ roles: newRoles });
}
/**
* Remove a role from the list of roles that can use this emoji.
* @param {Role} role The role to remove
* @returns {Promise<Emoji>}
*/
removeRestrictedRole(role) {
return this.removeRestrictedRoles([role]);
}
/**
* Remove multiple roles from the list of roles that can use this emoji.
* @param {Role[]} roles Roles to remove
* @returns {Promise<Emoji>}
*/
removeRestrictedRoles(roles) {
const newRoles = new Collection(this.roles);
for (const role of roles) {
if (newRoles.has(role.id)) newRoles.delete(role.id);
}
return this.edit({ roles: newRoles });
}
/**
* Deletes the emoji.
* @param {string} [reason] Reason for deleting the emoji
* @returns {Promise<Emoji>}
*/
delete(reason) {
return this.client.rest.methods.deleteEmoji(this, reason);
}
/**
* When concatenated with a string, this automatically returns the emoji mention rather than the object.
* @returns {string}
* @example
* // Send an emoji:
* const emoji = guild.emojis.first();
* msg.reply(`Hello! ${emoji}`);
*/
toString() {
if (!this.id || !this.requiresColons) {
return this.name;
}
return `<${this.animated ? 'a' : ''}:${this.name}:${this.id}>`;
}
/**
* Whether this emoji is the same as another one.
* @param {Emoji|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 Emoji) {
return (
other.id === this.id &&
other.name === this.name &&
other.managed === this.managed &&
other.requiresColons === this.requiresColons
);
} else {
return (
other.id === this.id &&
other.name === this.name
);
}
}
}
module.exports = Emoji;