Emoji.js
3.6 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
'use strict';
const process = require('node:process');
const Base = require('./Base');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
* @type {WeakSet<Emoji>}
* @private
* @internal
*/
const deletedEmojis = new WeakSet();
let deprecationEmittedForDeleted = false;
/**
* Represents raw emoji data from the API
* @typedef {APIEmoji} RawEmoji
* @property {?Snowflake} id The emoji's id
* @property {?string} name The emoji's name
* @property {?boolean} animated Whether the emoji is animated
*/
/**
* Represents an emoji, see {@link GuildEmoji} and {@link ReactionEmoji}.
* @extends {Base}
*/
class Emoji extends Base {
constructor(client, emoji) {
super(client);
/**
* Whether or not the emoji is animated
* @type {?boolean}
*/
this.animated = emoji.animated ?? null;
/**
* The emoji's name
* @type {?string}
*/
this.name = emoji.name ?? null;
/**
* The emoji's id
* @type {?Snowflake}
*/
this.id = emoji.id;
}
/**
* Whether or not the structure has been deleted
* @type {boolean}
* @deprecated This will be removed in the next major version, see https://github.com/discordjs/discord.js/issues/7091
*/
get deleted() {
if (!deprecationEmittedForDeleted) {
deprecationEmittedForDeleted = true;
process.emitWarning(
'Emoji#deleted is deprecated, see https://github.com/discordjs/discord.js/issues/7091.',
'DeprecationWarning',
);
}
return deletedEmojis.has(this);
}
set deleted(value) {
if (!deprecationEmittedForDeleted) {
deprecationEmittedForDeleted = true;
process.emitWarning(
'Emoji#deleted is deprecated, see https://github.com/discordjs/discord.js/issues/7091.',
'DeprecationWarning',
);
}
if (value) deletedEmojis.add(this);
else deletedEmojis.delete(this);
}
/**
* The identifier of this emoji, used for message reactions
* @type {string}
* @readonly
*/
get identifier() {
if (this.id) return `${this.animated ? 'a:' : ''}${this.name}:${this.id}`;
return encodeURIComponent(this.name);
}
/**
* The URL to the emoji file if it's a custom emoji
* @type {?string}
* @readonly
*/
get url() {
return this.id && this.client.rest.cdn.Emoji(this.id, this.animated ? 'gif' : 'png');
}
/**
* The timestamp the emoji was created at, or null if unicode
* @type {?number}
* @readonly
*/
get createdTimestamp() {
return this.id && SnowflakeUtil.timestampFrom(this.id);
}
/**
* The time the emoji was created at, or null if unicode
* @type {?Date}
* @readonly
*/
get createdAt() {
return this.id && new Date(this.createdTimestamp);
}
/**
* When concatenated with a string, this automatically returns the text required to form a graphical emoji on Discord
* instead of the Emoji object.
* @returns {string}
* @example
* // Send a custom emoji from a guild:
* const emoji = guild.emojis.cache.first();
* msg.channel.send(`Hello! ${emoji}`);
* @example
* // Send the emoji used in a reaction to the channel the reaction is part of
* reaction.message.channel.send(`The emoji used was: ${reaction.emoji}`);
*/
toString() {
return this.id ? `<${this.animated ? 'a' : ''}:${this.name}:${this.id}>` : this.name;
}
toJSON() {
return super.toJSON({
guild: 'guildId',
createdTimestamp: true,
url: true,
identifier: true,
});
}
}
exports.Emoji = Emoji;
exports.deletedEmojis = deletedEmojis;
/**
* @external APIEmoji
* @see {@link https://discord.com/developers/docs/resources/emoji#emoji-object}
*/