Skip to content

Commit

Permalink
feat(Threads): add support for invitable in private threads (#6501)
Browse files Browse the repository at this point in the history
Co-authored-by: Rodry <38259440+ImRodry@users.noreply.github.com>
Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
Co-authored-by: Noel <buechler.noel@outlook.com>
  • Loading branch information
4 people committed Aug 29, 2021
1 parent 0fe5f88 commit a693254
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/errors/Messages.js
Expand Up @@ -99,6 +99,7 @@ const Messages = {

MESSAGE_THREAD_PARENT: 'The message was not sent in a guild text or news channel',
MESSAGE_EXISTING_THREAD: 'The message already has a thread',
THREAD_INVITABLE_TYPE: type => `Invitable cannot be edited on ${type}`,

WEBHOOK_MESSAGE: 'The message was not sent by a webhook.',
WEBHOOK_TOKEN_UNAVAILABLE: 'This action requires a webhook token, but none is available.',
Expand Down
5 changes: 4 additions & 1 deletion src/managers/ThreadManager.js
Expand Up @@ -78,6 +78,8 @@ class ThreadManager extends CachedManager {
* @property {ThreadChannelTypes|number} [type] The type of thread to create. Defaults to `GUILD_PUBLIC_THREAD` if
* created in a {@link TextChannel} <warn>When creating threads in a {@link NewsChannel} this is ignored and is always
* `GUILD_NEWS_THREAD`</warn>
* @property {boolean} [invitable] Whether non-moderators can add other non-moderators to the thread
* <info>Can only be set when type will be `GUILD_PRIVATE_THREAD`</info>
*/

/**
Expand Down Expand Up @@ -106,7 +108,7 @@ class ThreadManager extends CachedManager {
* .then(threadChannel => console.log(threadChannel))
* .catch(console.error);
*/
async create({ name, autoArchiveDuration, startMessage, type, reason } = {}) {
async create({ name, autoArchiveDuration, startMessage, type, invitable, reason } = {}) {
let path = this.client.api.channels(this.channel.id);
if (type && typeof type !== 'string' && typeof type !== 'number') {
throw new TypeError('INVALID_TYPE', 'type', 'ThreadChannelType or Number');
Expand Down Expand Up @@ -134,6 +136,7 @@ class ThreadManager extends CachedManager {
name,
auto_archive_duration: autoArchiveDuration,
type: resolvedType,
invitable: resolvedType === ChannelTypes.GUILD_PRIVATE_THREAD ? invitable : undefined,
},
reason,
});
Expand Down
24 changes: 24 additions & 0 deletions src/structures/ThreadChannel.js
Expand Up @@ -2,6 +2,7 @@

const Channel = require('./Channel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { RangeError } = require('../errors');
const MessageManager = require('../managers/MessageManager');
const ThreadMemberManager = require('../managers/ThreadMemberManager');
const Permissions = require('../util/Permissions');
Expand Down Expand Up @@ -77,6 +78,13 @@ class ThreadChannel extends Channel {
*/
this.locked = data.thread_metadata.locked ?? false;

/**
* Whether members without `MANAGE_THREADS` can invite other members without `MANAGE_THREADS`
* <info>Always `null` in public threads</info>
* @type {?boolean}
*/
this.invitable = this.type === 'GUILD_PRIVATE_THREAD' ? data.thread_metadata.invitable ?? false : null;

/**
* Whether the thread is archived
* @type {?boolean}
Expand Down Expand Up @@ -109,6 +117,7 @@ class ThreadChannel extends Channel {
if (!this.archiveTimestamp) {
this.archiveTimestamp = null;
}
this.invitable ??= null;
}

if ('owner_id' in data) {
Expand Down Expand Up @@ -273,6 +282,8 @@ class ThreadChannel extends Channel {
* 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
* @property {boolean} [invitable] Whether non-moderators can add other non-moderators to a thread
* <info>Can only be edited on `GUILD_PRIVATE_THREAD`</info>
*/

/**
Expand Down Expand Up @@ -303,6 +314,7 @@ class ThreadChannel extends Channel {
auto_archive_duration: autoArchiveDuration,
rate_limit_per_user: data.rateLimitPerUser,
locked: data.locked,
invitable: this.type === 'GUILD_PRIVATE_THREAD' ? data.invitable : undefined,
},
reason,
});
Expand Down Expand Up @@ -343,6 +355,18 @@ class ThreadChannel extends Channel {
return this.edit({ autoArchiveDuration }, reason);
}

/**
* Sets whether members without the `MANAGE_THREADS` permission can invite other members without the
* `MANAGE_THREADS` permission to this thread.
* @param {boolean} [invitable=true] Whether non-moderators can invite non-moderators to this thread
* @param {string} [reason] Reason for changing invite
* @returns {Promise<ThreadChannel>}
*/
setInvitable(invitable = true, reason) {
if (this.type !== 'GUILD_PRIVATE_THREAD') return Promise.reject(new RangeError('THREAD_INVITABLE_TYPE', this.type));
return this.edit({ invitable }, 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.
Expand Down
8 changes: 7 additions & 1 deletion typings/index.d.ts
Expand Up @@ -306,7 +306,7 @@ export class BaseGuildTextChannel extends TextBasedChannel(GuildChannel) {
public defaultAutoArchiveDuration?: ThreadAutoArchiveDuration;
public messages: MessageManager;
public nsfw: boolean;
public threads: ThreadManager<AllowedThreadTypeForTextChannel>;
public threads: ThreadManager<AllowedThreadTypeForTextChannel | AllowedThreadTypeForNewsChannel>;
public topic: string | null;
public createInvite(options?: CreateInviteOptions): Promise<Invite>;
public createWebhook(name: string, options?: ChannelWebhookCreateOptions): Promise<Webhook>;
Expand Down Expand Up @@ -1412,6 +1412,7 @@ export class MessageSelectMenu extends BaseMessageComponent {
}

export class NewsChannel extends BaseGuildTextChannel {
public threads: ThreadManager<AllowedThreadTypeForNewsChannel>;
public type: 'GUILD_NEWS';
public addFollower(channel: GuildChannelResolvable, reason?: string): Promise<NewsChannel>;
}
Expand Down Expand Up @@ -1782,6 +1783,7 @@ export class TeamMember extends Base {

export class TextChannel extends BaseGuildTextChannel {
public rateLimitPerUser: number;
public threads: ThreadManager<AllowedThreadTypeForTextChannel>;
public type: 'GUILD_TEXT';
public setRateLimitPerUser(rateLimitPerUser: number, reason?: string): Promise<TextChannel>;
}
Expand All @@ -1796,6 +1798,7 @@ export class ThreadChannel extends TextBasedChannel(Channel) {
public guild: Guild;
public guildId: Snowflake;
public readonly guildMembers: Collection<Snowflake, GuildMember>;
public invitable: boolean | null;
public readonly joinable: boolean;
public readonly joined: boolean;
public locked: boolean | null;
Expand Down Expand Up @@ -1825,6 +1828,7 @@ export class ThreadChannel extends TextBasedChannel(Channel) {
autoArchiveDuration: ThreadAutoArchiveDuration,
reason?: string,
): Promise<ThreadChannel>;
public setInvitable(invitable?: boolean, reason?: string): Promise<ThreadChannel>;
public setLocked(locked?: boolean, reason?: string): Promise<ThreadChannel>;
public setName(name: string, reason?: string): Promise<ThreadChannel>;
public setRateLimitPerUser(rateLimitPerUser: number, reason?: string): Promise<ThreadChannel>;
Expand Down Expand Up @@ -4593,6 +4597,7 @@ export type ThreadChannelTypes = 'GUILD_NEWS_THREAD' | 'GUILD_PUBLIC_THREAD' | '
export interface ThreadCreateOptions<AllowedThreadType> extends StartThreadOptions {
startMessage?: MessageResolvable;
type?: AllowedThreadType;
invitable?: AllowedThreadType extends 'GUILD_PRIVATE_THREAD' | 12 ? boolean : never;
}

export interface ThreadEditData {
Expand All @@ -4601,6 +4606,7 @@ export interface ThreadEditData {
autoArchiveDuration?: ThreadAutoArchiveDuration;
rateLimitPerUser?: number;
locked?: boolean;
invitable?: boolean;
}

export type ThreadMemberFlagsString = '';
Expand Down

0 comments on commit a693254

Please sign in to comment.