Sticker.js
7.47 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
'use strict';
const process = require('node:process');
const Base = require('./Base');
const { StickerFormatTypes, StickerTypes } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
* @type {WeakSet<StageInstance>}
* @private
* @internal
*/
const deletedStickers = new WeakSet();
let deprecationEmittedForDeleted = false;
/**
* Represents a Sticker.
* @extends {Base}
*/
class Sticker extends Base {
constructor(client, sticker) {
super(client);
this._patch(sticker);
}
_patch(sticker) {
/**
* The sticker's id
* @type {Snowflake}
*/
this.id = sticker.id;
if ('description' in sticker) {
/**
* The description of the sticker
* @type {?string}
*/
this.description = sticker.description;
} else {
this.description ??= null;
}
if ('type' in sticker) {
/**
* The type of the sticker
* @type {?StickerType}
*/
this.type = StickerTypes[sticker.type];
} else {
this.type ??= null;
}
if ('format_type' in sticker) {
/**
* The format of the sticker
* @type {StickerFormatType}
*/
this.format = StickerFormatTypes[sticker.format_type];
}
if ('name' in sticker) {
/**
* The name of the sticker
* @type {string}
*/
this.name = sticker.name;
}
if ('pack_id' in sticker) {
/**
* The id of the pack the sticker is from, for standard stickers
* @type {?Snowflake}
*/
this.packId = sticker.pack_id;
} else {
this.packId ??= null;
}
if ('tags' in sticker) {
/**
* An array of tags for the sticker
* @type {?string[]}
*/
this.tags = sticker.tags.split(', ');
} else {
this.tags ??= null;
}
if ('available' in sticker) {
/**
* Whether or not the guild sticker is available
* @type {?boolean}
*/
this.available = sticker.available;
} else {
this.available ??= null;
}
if ('guild_id' in sticker) {
/**
* The id of the guild that owns this sticker
* @type {?Snowflake}
*/
this.guildId = sticker.guild_id;
} else {
this.guildId ??= null;
}
if ('user' in sticker) {
/**
* The user that uploaded the guild sticker
* @type {?User}
*/
this.user = this.client.users._add(sticker.user);
} else {
this.user ??= null;
}
if ('sort_value' in sticker) {
/**
* The standard sticker's sort order within its pack
* @type {?number}
*/
this.sortValue = sticker.sort_value;
} else {
this.sortValue ??= null;
}
}
/**
* The timestamp the sticker was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.timestampFrom(this.id);
}
/**
* The time the sticker was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* Whether or not the sticker 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(
'Sticker#deleted is deprecated, see https://github.com/discordjs/discord.js/issues/7091.',
'DeprecationWarning',
);
}
return deletedStickers.has(this);
}
set deleted(value) {
if (!deprecationEmittedForDeleted) {
deprecationEmittedForDeleted = true;
process.emitWarning(
'Sticker#deleted is deprecated, see https://github.com/discordjs/discord.js/issues/7091.',
'DeprecationWarning',
);
}
if (value) deletedStickers.add(this);
else deletedStickers.delete(this);
}
/**
* Whether this sticker is partial
* @type {boolean}
* @readonly
*/
get partial() {
return !this.type;
}
/**
* The guild that owns this sticker
* @type {?Guild}
* @readonly
*/
get guild() {
return this.client.guilds.resolve(this.guildId);
}
/**
* A link to the sticker
* <info>If the sticker's format is LOTTIE, it returns the URL of the Lottie JSON file.</info>
* @type {string}
*/
get url() {
return this.client.rest.cdn.Sticker(this.id, this.format);
}
/**
* Fetches this sticker.
* @returns {Promise<Sticker>}
*/
async fetch() {
const data = await this.client.api.stickers(this.id).get();
this._patch(data);
return this;
}
/**
* Fetches the pack this sticker is part of from Discord, if this is a Nitro sticker.
* @returns {Promise<?StickerPack>}
*/
async fetchPack() {
return (this.packId && (await this.client.fetchPremiumStickerPacks()).get(this.packId)) ?? null;
}
/**
* Fetches the user who uploaded this sticker, if this is a guild sticker.
* @returns {Promise<?User>}
*/
async fetchUser() {
if (this.partial) await this.fetch();
if (!this.guildId) throw new Error('NOT_GUILD_STICKER');
const data = await this.client.api.guilds(this.guildId).stickers(this.id).get();
this._patch(data);
return this.user;
}
/**
* Data for editing a sticker.
* @typedef {Object} GuildStickerEditData
* @property {string} [name] The name of the sticker
* @property {?string} [description] The description of the sticker
* @property {string} [tags] The Discord name of a unicode emoji representing the sticker's expression
*/
/**
* Edits the sticker.
* @param {GuildStickerEditData} [data] The new data for the sticker
* @param {string} [reason] Reason for editing this sticker
* @returns {Promise<Sticker>}
* @example
* // Update the name of a sticker
* sticker.edit({ name: 'new name' })
* .then(s => console.log(`Updated the name of the sticker to ${s.name}`))
* .catch(console.error);
*/
edit(data, reason) {
return this.guild.stickers.edit(this, data, reason);
}
/**
* Deletes the sticker.
* @returns {Promise<Sticker>}
* @param {string} [reason] Reason for deleting this sticker
* @example
* // Delete a message
* sticker.delete()
* .then(s => console.log(`Deleted sticker ${s.name}`))
* .catch(console.error);
*/
async delete(reason) {
await this.guild.stickers.delete(this, reason);
return this;
}
/**
* Whether this sticker is the same as another one.
* @param {Sticker|APISticker} other The sticker to compare it to
* @returns {boolean}
*/
equals(other) {
if (other instanceof Sticker) {
return (
other.id === this.id &&
other.description === this.description &&
other.type === this.type &&
other.format === this.format &&
other.name === this.name &&
other.packId === this.packId &&
other.tags.length === this.tags.length &&
other.tags.every(tag => this.tags.includes(tag)) &&
other.available === this.available &&
other.guildId === this.guildId &&
other.sortValue === this.sortValue
);
} else {
return (
other.id === this.id &&
other.description === this.description &&
other.name === this.name &&
other.tags === this.tags.join(', ')
);
}
}
}
exports.Sticker = Sticker;
exports.deletedStickers = deletedStickers;
/**
* @external APISticker
* @see {@link https://discord.com/developers/docs/resources/sticker#sticker-object}
*/