From db09d7942333dffad4fba875567758550d65bfef Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com> Date: Thu, 18 Nov 2021 01:58:05 -0500 Subject: [PATCH] feat(ThreadChannel): add `ThreadChannel#viewable` (#6975) --- src/structures/Message.js | 10 +++++++++- src/structures/ThreadChannel.js | 12 ++++++++++++ typings/index.d.ts | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/structures/Message.js b/src/structures/Message.js index 31d65be2e3e9..1bd600c19895 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -551,7 +551,15 @@ class Message extends Base { * @readonly */ get editable() { - return Boolean(this.author.id === this.client.user.id && !this.deleted && (!this.guild || this.channel?.viewable)); + const precheck = Boolean( + this.author.id === this.client.user.id && !this.deleted && (!this.guild || this.channel?.viewable), + ); + // Regardless of permissions thread messages cannot be edited if + // the thread is locked. + if (this.channel?.isThread()) { + return precheck && !this.channel.locked; + } + return precheck; } /** diff --git a/src/structures/ThreadChannel.js b/src/structures/ThreadChannel.js index 70c2b5949ca8..4726dbbd8326 100644 --- a/src/structures/ThreadChannel.js +++ b/src/structures/ThreadChannel.js @@ -442,6 +442,18 @@ class ThreadChannel extends Channel { return this.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_THREADS, false); } + /** + * Whether the thread is viewable by the client user + * @type {boolean} + * @readonly + */ + get viewable() { + if (this.client.user.id === this.guild.ownerId) return true; + const permissions = this.permissionsFor(this.client.user); + if (!permissions) return false; + return permissions.has(Permissions.FLAGS.VIEW_CHANNEL, false); + } + /** * Whether the client user can send messages in this thread * @type {boolean} diff --git a/typings/index.d.ts b/typings/index.d.ts index ec589cd1193f..d07cadcb68f8 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2099,6 +2099,7 @@ export class ThreadChannel extends TextBasedChannel(Channel) { public readonly joined: boolean; public locked: boolean | null; public readonly manageable: boolean; + public readonly viewable: boolean; public readonly sendable: boolean; public memberCount: number | null; public messageCount: number | null;