Invite.js
8.91 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
313
314
315
316
317
318
319
320
321
322
323
324
'use strict';
const Base = require('./Base');
const { GuildScheduledEvent } = require('./GuildScheduledEvent');
const IntegrationApplication = require('./IntegrationApplication');
const InviteStageInstance = require('./InviteStageInstance');
const { Error } = require('../errors');
const { Endpoints } = require('../util/Constants');
const Permissions = require('../util/Permissions');
// TODO: Convert `inviter` and `channel` in this class to a getter.
/**
* Represents an invitation to a guild channel.
* @extends {Base}
*/
class Invite extends Base {
constructor(client, data) {
super(client);
this._patch(data);
}
_patch(data) {
const InviteGuild = require('./InviteGuild');
/**
* The guild the invite is for including welcome screen data if present
* @type {?(Guild|InviteGuild)}
*/
this.guild ??= null;
if (data.guild) {
this.guild = this.client.guilds.resolve(data.guild.id) ?? new InviteGuild(this.client, data.guild);
}
if ('code' in data) {
/**
* The code for this invite
* @type {string}
*/
this.code = data.code;
}
if ('approximate_presence_count' in data) {
/**
* The approximate number of online members of the guild this invite is for
* <info>This is only available when the invite was fetched through {@link Client#fetchInvite}.</info>
* @type {?number}
*/
this.presenceCount = data.approximate_presence_count;
} else {
this.presenceCount ??= null;
}
if ('approximate_member_count' in data) {
/**
* The approximate total number of members of the guild this invite is for
* <info>This is only available when the invite was fetched through {@link Client#fetchInvite}.</info>
* @type {?number}
*/
this.memberCount = data.approximate_member_count;
} else {
this.memberCount ??= null;
}
if ('temporary' in data) {
/**
* Whether or not this invite only grants temporary membership
* <info>This is only available when the invite was fetched through {@link GuildInviteManager#fetch}
* or created through {@link GuildInviteManager#create}.</info>
* @type {?boolean}
*/
this.temporary = data.temporary ?? null;
} else {
this.temporary ??= null;
}
if ('max_age' in data) {
/**
* The maximum age of the invite, in seconds, 0 if never expires
* <info>This is only available when the invite was fetched through {@link GuildInviteManager#fetch}
* or created through {@link GuildInviteManager#create}.</info>
* @type {?number}
*/
this.maxAge = data.max_age;
} else {
this.maxAge ??= null;
}
if ('uses' in data) {
/**
* How many times this invite has been used
* <info>This is only available when the invite was fetched through {@link GuildInviteManager#fetch}
* or created through {@link GuildInviteManager#create}.</info>
* @type {?number}
*/
this.uses = data.uses;
} else {
this.uses ??= null;
}
if ('max_uses' in data) {
/**
* The maximum uses of this invite
* <info>This is only available when the invite was fetched through {@link GuildInviteManager#fetch}
* or created through {@link GuildInviteManager#create}.</info>
* @type {?number}
*/
this.maxUses = data.max_uses;
} else {
this.maxUses ??= null;
}
if ('inviter_id' in data) {
/**
* The user's id who created this invite
* @type {?Snowflake}
*/
this.inviterId = data.inviter_id;
this.inviter = this.client.users.resolve(data.inviter_id);
} else {
this.inviterId ??= null;
}
if ('inviter' in data) {
/**
* The user who created this invite
* @type {?User}
*/
this.inviter ??= this.client.users._add(data.inviter);
this.inviterId = data.inviter.id;
} else {
this.inviter ??= null;
}
if ('target_user' in data) {
/**
* The user whose stream to display for this voice channel stream invite
* @type {?User}
*/
this.targetUser = this.client.users._add(data.target_user);
} else {
this.targetUser ??= null;
}
if ('target_application' in data) {
/**
* The embedded application to open for this voice channel embedded application invite
* @type {?IntegrationApplication}
*/
this.targetApplication = new IntegrationApplication(this.client, data.target_application);
} else {
this.targetApplication ??= null;
}
/**
* The type of the invite target:
* * 1: STREAM
* * 2: EMBEDDED_APPLICATION
* @typedef {number} TargetType
* @see {@link https://discord.com/developers/docs/resources/invite#invite-object-invite-target-types}
*/
if ('target_type' in data) {
/**
* The target type
* @type {?TargetType}
*/
this.targetType = data.target_type;
} else {
this.targetType ??= null;
}
if ('channel_id' in data) {
/**
* The channel's id this invite is for
* @type {Snowflake}
*/
this.channelId = data.channel_id;
this.channel = this.client.channels.cache.get(data.channel_id);
}
if ('channel' in data) {
/**
* The channel this invite is for
* @type {Channel}
*/
this.channel ??= this.client.channels._add(data.channel, this.guild, { cache: false });
this.channelId ??= data.channel.id;
}
if ('created_at' in data) {
/**
* The timestamp this invite was created at
* @type {?number}
*/
this.createdTimestamp = new Date(data.created_at).getTime();
} else {
this.createdTimestamp ??= null;
}
if ('expires_at' in data) this._expiresTimestamp = new Date(data.expires_at).getTime();
else this._expiresTimestamp ??= null;
if ('stage_instance' in data) {
/**
* The stage instance data if there is a public {@link StageInstance} in the stage channel this invite is for
* @type {?InviteStageInstance}
*/
this.stageInstance = new InviteStageInstance(this.client, data.stage_instance, this.channel.id, this.guild.id);
} else {
this.stageInstance ??= null;
}
if ('guild_scheduled_event' in data) {
/**
* The guild scheduled event data if there is a {@link GuildScheduledEvent} in the channel this invite is for
* @type {?GuildScheduledEvent}
*/
this.guildScheduledEvent = new GuildScheduledEvent(this.client, data.guild_scheduled_event);
} else {
this.guildScheduledEvent ??= null;
}
}
/**
* The time the invite was created at
* @type {?Date}
* @readonly
*/
get createdAt() {
return this.createdTimestamp ? new Date(this.createdTimestamp) : null;
}
/**
* Whether the invite is deletable by the client user
* @type {boolean}
* @readonly
*/
get deletable() {
const guild = this.guild;
if (!guild || !this.client.guilds.cache.has(guild.id)) return false;
if (!guild.me) throw new Error('GUILD_UNCACHED_ME');
return (
this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) ||
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)
);
}
/**
* The timestamp the invite will expire at
* @type {?number}
* @readonly
*/
get expiresTimestamp() {
return (
this._expiresTimestamp ??
(this.createdTimestamp && this.maxAge ? this.createdTimestamp + this.maxAge * 1_000 : null)
);
}
/**
* The time the invite will expire at
* @type {?Date}
* @readonly
*/
get expiresAt() {
const { expiresTimestamp } = this;
return expiresTimestamp ? new Date(expiresTimestamp) : null;
}
/**
* The URL to the invite
* @type {string}
* @readonly
*/
get url() {
return Endpoints.invite(this.client.options.http.invite, this.code);
}
/**
* Deletes this invite.
* @param {string} [reason] Reason for deleting this invite
* @returns {Promise<Invite>}
*/
async delete(reason) {
await this.client.api.invites[this.code].delete({ reason });
return this;
}
/**
* When concatenated with a string, this automatically concatenates the invite's URL instead of the object.
* @returns {string}
* @example
* // Logs: Invite: https://discord.gg/A1b2C3
* console.log(`Invite: ${invite}`);
*/
toString() {
return this.url;
}
toJSON() {
return super.toJSON({
url: true,
expiresTimestamp: true,
presenceCount: false,
memberCount: false,
uses: false,
channel: 'channelId',
inviter: 'inviterId',
guild: 'guildId',
});
}
valueOf() {
return this.code;
}
}
/**
* Regular expression that globally matches Discord invite links
* @type {RegExp}
*/
Invite.INVITES_PATTERN = /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/gi;
module.exports = Invite;