forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadChannel.js
426 lines (381 loc) · 12.7 KB
/
ThreadChannel.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
'use strict';
const Channel = require('./Channel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const MessageManager = require('../managers/MessageManager');
const ThreadMemberManager = require('../managers/ThreadMemberManager');
const Permissions = require('../util/Permissions');
/**
* Represents a thread channel on Discord.
* @extends {Channel}
* @implements {TextBasedChannel}
*/
class ThreadChannel extends Channel {
/**
* @param {Guild} guild The guild the thread channel is part of
* @param {APIChannel} data The data for the thread channel
* @param {Client} [client] A safety parameter for the client that instantiated this
* @param {boolean} [fromInteraction=false] Whether the data was from an interaction (partial)
*/
constructor(guild, data, client, fromInteraction = false) {
super(guild?.client ?? client, data, false);
/**
* The guild the thread is in
* @type {Guild}
*/
this.guild = guild;
/**
* The id of the guild the channel is in
* @type {Snowflake}
*/
this.guildId = guild?.id ?? data.guild_id;
/**
* A manager of the messages sent to this thread
* @type {MessageManager}
*/
this.messages = new MessageManager(this);
/**
* A manager of the members that are part of this thread
* @type {ThreadMemberManager}
*/
this.members = new ThreadMemberManager(this);
if (data) this._patch(data, fromInteraction);
}
_patch(data, partial = false) {
super._patch(data);
/**
* The name of the thread
* @type {string}
*/
this.name = data.name;
if ('guild_id' in data) {
this.guildId = data.guild_id;
}
if ('parent_id' in data) {
/**
* The id of the parent channel of this thread
* @type {?Snowflake}
*/
this.parentId = data.parent_id;
}
if ('thread_metadata' in data) {
/**
* Whether the thread is locked
* @type {?boolean}
*/
this.locked = data.thread_metadata.locked ?? false;
/**
* Whether the thread is archived
* @type {?boolean}
*/
this.archived = data.thread_metadata.archived;
/**
* The amount of time (in minutes) after which the thread will automatically archive in case of no recent activity
* @type {?number}
*/
this.autoArchiveDuration = data.thread_metadata.auto_archive_duration;
/**
* The timestamp when the thread's archive status was last changed
* <info>If the thread was never archived or unarchived, this is the timestamp at which the thread was
* created</info>
* @type {?number}
*/
this.archiveTimestamp = new Date(data.thread_metadata.archive_timestamp).getTime();
}
if ('owner_id' in data) {
/**
* The id of the member who created this thread
* @type {?Snowflake}
*/
this.ownerId = data.owner_id;
}
if ('last_message_id' in data) {
/**
* The last message id sent in this thread, if one was sent
* @type {?Snowflake}
*/
this.lastMessageId = data.last_message_id;
}
if ('last_pin_timestamp' in data) {
/**
* The timestamp when the last pinned message was pinned, if there was one
* @type {?number}
*/
this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;
}
if ('rate_limit_per_user' in data || !partial) {
/**
* The ratelimit per user for this thread (in seconds)
* @type {?number}
*/
this.rateLimitPerUser = data.rate_limit_per_user ?? 0;
}
if ('message_count' in data) {
/**
* The approximate count of messages in this thread
* <info>This stops counting at 50. If you need an approximate value higher than that, use
* `ThreadChannel#messages.cache.size`</info>
* @type {?number}
*/
this.messageCount = data.message_count;
}
if ('member_count' in data) {
/**
* The approximate count of users in this thread
* <info>This stops counting at 50. If you need an approximate value higher than that, use
* `ThreadChannel#members.cache.size`</info>
* @type {?number}
*/
this.memberCount = data.member_count;
}
if (data.member && this.client.user) this.members._add({ user_id: this.client.user.id, ...data.member });
if (data.messages) for (const message of data.messages) this.messages._add(message);
}
/**
* A collection of associated guild member objects of this thread's members
* @type {Collection<Snowflake, GuildMember>}
* @readonly
*/
get guildMembers() {
return this.members.cache.mapValues(member => member.guildMember);
}
/**
* The time at which this thread's archive status was last changed
* <info>If the thread was never archived or unarchived, this is the time at which the thread was created</info>
* @type {?Date}
* @readonly
*/
get archivedAt() {
if (!this.archiveTimestamp) return null;
return new Date(this.archiveTimestamp);
}
/**
* The parent channel of this thread
* @type {?(NewsChannel|TextChannel)}
* @readonly
*/
get parent() {
return this.guild.channels.resolve(this.parentId);
}
/**
* Makes the client user join the thread.
* @returns {Promise<ThreadChannel>}
*/
join() {
return this.members.add('@me').then(() => this);
}
/**
* Makes the client user leave the thread.
* @returns {Promise<ThreadChannel>}
*/
leave() {
return this.members.remove('@me').then(() => this);
}
/**
* Gets the overall set of permissions for a member or role in this thread's parent channel, taking overwrites into
* account.
* @param {GuildMemberResolvable|RoleResolvable} memberOrRole The member or role to obtain the overall permissions for
* @returns {?Readonly<Permissions>}
*/
permissionsFor(memberOrRole) {
return this.parent?.permissionsFor(memberOrRole) ?? null;
}
/**
* The options used to edit a thread channel
* @typedef {Object} ThreadEditData
* @property {string} [name] The new name for the thread
* @property {boolean} [archived] Whether the thread is archived
* @property {ThreadAutoArchiveDuration} [autoArchiveDuration] The amount of time (in minutes) after which the thread
* should automatically archive in case of no recent activity
* @property {number} [rateLimitPerUser] The ratelimit per user for the thread in seconds
* @property {boolean} [locked] Whether the thread is locked
*/
/**
* Edits this thread.
* @param {ThreadEditData} data The new data for this thread
* @param {string} [reason] Reason for editing this thread
* @returns {Promise<ThreadChannel>}
* @example
* // Edit a thread
* thread.edit({ name: 'new-thread' })
* .then(editedThread => console.log(editedThread))
* .catch(console.error);
*/
async edit(data, reason) {
const newData = await this.client.api.channels(this.id).patch({
data: {
name: (data.name ?? this.name).trim(),
archived: data.archived,
auto_archive_duration: data.autoArchiveDuration,
rate_limit_per_user: data.rateLimitPerUser,
locked: data.locked,
},
reason,
});
return this.client.actions.ChannelUpdate.handle(newData).updated;
}
/**
* Sets whether the thread is archived.
* @param {boolean} [archived=true] Whether the thread is archived
* @param {string} [reason] Reason for archiving or unarchiving
* @returns {Promise<ThreadChannel>}
* @example
* // Archive the thread
* thread.setArchived(true)
* .then(newThread => console.log(`Thread is now ${newThread.archived ? 'archived' : 'active'}`))
* .catch(console.error);
*/
setArchived(archived = true, reason) {
return this.edit({ archived }, reason);
}
/**
* Sets the duration after which the thread will automatically archive in case of no recent activity.
* @param {ThreadAutoArchiveDuration} autoArchiveDuration The amount of time (in minutes) after which the thread
* should automatically archive in case of no recent activity
* @param {string} [reason] Reason for changing the auto archive duration
* @returns {Promise<ThreadChannel>}
* @example
* // Set the thread's auto archive time to 1 hour
* thread.setAutoArchiveDuration(60)
* .then(newThread => {
* console.log(`Thread will now archive after ${newThread.autoArchiveDuration} minutes of inactivity`);
* });
* .catch(console.error);
*/
setAutoArchiveDuration(autoArchiveDuration, reason) {
return this.edit({ autoArchiveDuration }, reason);
}
/**
* Sets whether the thread can be **unarchived** by anyone with `SEND_MESSAGES` permission.
* When a thread is locked only members with `MANAGE_THREADS` can unarchive it.
* @param {boolean} [locked=true] Whether the thread is locked
* @param {string} [reason] Reason for locking or unlocking the thread
* @returns {Promise<ThreadChannel>}
* @example
* // Set the thread to locked
* thread.setLocked(true)
* .then(newThread => console.log(`Thread is now ${newThread.locked ? 'locked' : 'unlocked'}`))
* .catch(console.error);
*/
setLocked(locked = true, reason) {
return this.edit({ locked }, reason);
}
/**
* Sets a new name for this thread.
* @param {string} name The new name for the thread
* @param {string} [reason] Reason for changing the thread's name
* @returns {Promise<ThreadChannel>}
* @example
* // Change the thread's name
* thread.setName('not_general')
* .then(newThread => console.log(`Thread's new name is ${newThread.name}`))
* .catch(console.error);
*/
setName(name, reason) {
return this.edit({ name }, reason);
}
/**
* Sets the rate limit per user for this thread.
* @param {number} rateLimitPerUser The new ratelimit in seconds
* @param {string} [reason] Reason for changing the thread's ratelimits
* @returns {Promise<ThreadChannel>}
*/
setRateLimitPerUser(rateLimitPerUser, reason) {
return this.edit({ rateLimitPerUser }, reason);
}
/**
* Whether the client user is a member of the thread.
* @type {boolean}
* @readonly
*/
get joined() {
return this.members.cache.has(this.client.user?.id);
}
/**
* Whether the thread is editable by the client user (name, archived, autoArchiveDuration)
* @type {boolean}
* @readonly
*/
get editable() {
return (this.ownerId === this.client.user.id && (this.type !== 'private_thread' || this.joined)) || this.manageable;
}
/**
* Whether the thread is joinable by the client user
* @type {boolean}
* @readonly
*/
get joinable() {
return (
!this.archived &&
!this.joined &&
this.permissionsFor(this.client.user)?.has(
this.type === 'GUILD_PRIVATE_THREAD' ? Permissions.FLAGS.MANAGE_THREADS : Permissions.FLAGS.VIEW_CHANNEL,
false,
)
);
}
/**
* Whether the thread is manageable by the client user, for deleting or editing rateLimitPerUser or locked.
* @type {boolean}
* @readonly
*/
get manageable() {
return this.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_THREADS, false);
}
/**
* Whether the client user can send messages in this thread
* @type {boolean}
* @readonly
*/
get sendable() {
return (
!this.archived &&
(this.type !== 'private_thread' || this.joined || this.manageable) &&
this.permissionsFor(this.client.user)?.any(
[
Permissions.FLAGS.SEND_MESSAGES,
this.type === 'GUILD_PRIVATE_THREAD'
? Permissions.FLAGS.USE_PRIVATE_THREADS
: Permissions.FLAGS.USE_PUBLIC_THREADS,
],
false,
)
);
}
/**
* Whether the thread is unarchivable by the client user
* @type {boolean}
* @readonly
*/
get unarchivable() {
return this.archived && (this.locked ? this.manageable : this.sendable);
}
/**
* Deletes this thread.
* @param {string} [reason] Reason for deleting this thread
* @returns {Promise<ThreadChannel>}
* @example
* // Delete the thread
* thread.delete('cleaning out old threads')
* .then(deletedThread => console.log(deletedThread))
* .catch(console.error);
*/
delete(reason) {
return this.client.api
.channels(this.id)
.delete({ reason })
.then(() => this);
}
// These are here only for documentation purposes - they are implemented by TextBasedChannel
/* eslint-disable no-empty-function */
get lastMessage() {}
get lastPinAt() {}
send() {}
sendTyping() {}
createMessageCollector() {}
awaitMessages() {}
createMessageComponentCollector() {}
awaitMessageComponent() {}
bulkDelete() {}
}
TextBasedChannel.applyToClass(ThreadChannel, true);
module.exports = ThreadChannel;