forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sticker.js
167 lines (143 loc) · 3.61 KB
/
Sticker.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
'use strict';
const Base = require('./Base');
const { StickerFormatTypes, StickerTypes } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
* Represents a Sticker.
* @extends {Base}
*/
class Sticker extends Base {
/**
* @param {Client} client The instantiating client
* @param {Object} sticker The data for the sticker
*/
constructor(client, sticker) {
super(client);
this._patch(sticker);
}
_patch(sticker) {
/**
* The ID of the sticker
* @type {Snowflake}
*/
this.id = sticker.id;
/**
* The description of the sticker
* @type {?string}
*/
this.description = sticker.description ?? null;
/**
* The type of the sticker
* @type {?StickerType}
*/
this.type = StickerTypes[sticker.type] ?? null;
/**
* The format of the sticker
* @type {StickerFormatType}
*/
this.format = StickerFormatTypes[sticker.format_type];
/**
* The name of the sticker
* @type {string}
*/
this.name = sticker.name;
/**
* The ID of the pack the sticker is from, for standard stickers
* @type {?Snowflake}
*/
this.packID = sticker.pack_id ?? null;
/**
* An array of tags for the sticker
* @type {string[]}
*/
this.tags = sticker.tags?.split(', ') ?? null;
/**
* Whether or not the guild sticker is available
* @type {?boolean}
*/
this.available = sticker.available ?? null;
/**
* The ID of the guild that owns this sticker
* @type {?Snowflake}
*/
this.guildID = sticker.guild_id ?? null;
/**
* The user that uploaded the guild sticker
* @type {?User}
*/
this.user = sticker.user ? this.client.users.add(sticker.user) : null;
/**
* The standard sticker's sort order within its pack
* @type {?number}
*/
this.sortValue = sticker.sort_value ?? null;
}
/**
* The timestamp the sticker was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.deconstruct(this.id).timestamp;
}
/**
* The time the sticker was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* Whether this sticker is partial
* @type {boolean}
* @readonly
*/
get partial() {
return 'type' in this;
}
/**
* 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;
}
}
module.exports = Sticker;