/
Entitlement.js
184 lines (161 loc) · 4.12 KB
/
Entitlement.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
'use strict';
const Base = require('./Base');
/**
* Represents an Entitlement
* @extends {Base}
*/
class Entitlement extends Base {
constructor(client, data) {
super(client);
/**
* The id of the entitlement
* @type {Snowflake}
*/
this.id = data.id;
this._patch(data);
}
_patch(data) {
if ('sku_id' in data) {
/**
* The id of the associated SKU
* @type {Snowflake}
*/
this.skuId = data.sku_id;
}
if ('user_id' in data) {
/**
* The id of the user that is granted access to this entitlement's SKU
* @type {Snowflake}
*/
this.userId = data.user_id;
}
if ('guild_id' in data) {
/**
* The id of the guild that is granted access to this entitlement's SKU
* @type {?Snowflake}
*/
this.guildId = data.guild_id;
} else {
this.guildId ??= null;
}
if ('application_id' in data) {
/**
* The id of the parent application
* @type {Snowflake}
*/
this.applicationId = data.application_id;
}
if ('type' in data) {
/**
* The type of this entitlement
* @type {EntitlementType}
*/
this.type = data.type;
}
if ('deleted' in data) {
/**
* Whether this entitlement was deleted
* @type {boolean}
*/
this.deleted = data.deleted;
}
if ('starts_at' in data) {
/**
* The timestamp at which this entitlement is valid
* <info>This is only `null` for test entitlements</info>
* @type {?number}
*/
this.startsTimestamp = Date.parse(data.starts_at);
} else {
this.startsTimestamp ??= null;
}
if ('ends_at' in data) {
/**
* The timestamp at which this entitlement is no longer valid
* <info>This is only `null` for test entitlements</info>
* @type {?number}
*/
this.endsTimestamp = Date.parse(data.ends_at);
} else {
this.endsTimestamp ??= null;
}
if ('consumed' in data) {
/**
* Whether this entitlement has been consumed
* @type {boolean}
*/
this.consumed = data.consumed;
} else {
this.consumed ??= false;
}
}
/**
* The guild that is granted access to this entitlement's SKU
* @type {?Guild}
*/
get guild() {
if (!this.guildId) return null;
return this.client.guilds.cache.get(this.guildId) ?? null;
}
/**
* The start date at which this entitlement is valid
* <info>This is only `null` for test entitlements</info>
* @type {?Date}
*/
get startsAt() {
return this.startsTimestamp && new Date(this.startsTimestamp);
}
/**
* The end date at which this entitlement is no longer valid
* <info>This is only `null` for test entitlements</info>
* @type {?Date}
*/
get endsAt() {
return this.endsTimestamp && new Date(this.endsTimestamp);
}
/**
* Indicates whether this entitlement is active
* @returns {boolean}
*/
isActive() {
return !this.deleted && (!this.endsTimestamp || this.endsTimestamp > Date.now());
}
/**
* Indicates whether this entitlement is a test entitlement
* @returns {boolean}
*/
isTest() {
return this.startsTimestamp === null;
}
/**
* Indicates whether this entitlement is a user subscription
* @returns {boolean}
*/
isUserSubscription() {
return this.guildId === null;
}
/**
* Indicates whether this entitlement is a guild subscription
* @returns {boolean}
*/
isGuildSubscription() {
return this.guildId !== null;
}
/**
* Fetches the user that is granted access to this entitlement's SKU
* @returns {Promise<User>}
*/
fetchUser() {
return this.client.users.fetch(this.userId);
}
/**
* Marks this entitlement as consumed
* <info>Only available for One-Time Purchase consumable SKUs.</info>
* @returns {Promise<void>}
*/
async consume() {
// Return this.client.rest.post(Routes.consumeEntitlement(this.applicationId, this.id));
await this.client.rest.post(`/applications/${this.applicationId}/entitlements/${this.id}/consume`);
}
}
exports.Entitlement = Entitlement;