From 13f2c6408d395bc21a797c53eb6194a16b518e32 Mon Sep 17 00:00:00 2001 From: Idris Date: Tue, 25 Oct 2022 23:55:16 +0200 Subject: [PATCH 1/9] feat(ThreadChannel): add a helper for pin and unpin --- packages/discord.js/src/errors/ErrorCodes.js | 4 ++++ packages/discord.js/src/errors/Messages.js | 2 ++ .../src/structures/ThreadChannel.js | 23 ++++++++++++++++++- packages/discord.js/typings/index.d.ts | 2 ++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/discord.js/src/errors/ErrorCodes.js b/packages/discord.js/src/errors/ErrorCodes.js index f0566a0433a8..53ccf861898c 100644 --- a/packages/discord.js/src/errors/ErrorCodes.js +++ b/packages/discord.js/src/errors/ErrorCodes.js @@ -90,6 +90,8 @@ * @property {'StageChannelResolve'} StageChannelResolve * @property {'GuildScheduledEventResolve'} GuildScheduledEventResolve * @property {'FetchOwnerId'} FetchOwnerId + * @property {'CannotPinThreadOutOfForumChannel'} CannotPinThreadOutOfForumChannel + * @property {'CannotUnpinThreadOutOfForumChannel'} CannotUnpinThreadOutOfForumChannel * @property {'InvalidType'} InvalidType * @property {'InvalidElement'} InvalidElement @@ -236,6 +238,8 @@ const keys = [ 'StageChannelResolve', 'GuildScheduledEventResolve', 'FetchOwnerId', + 'CannotPinThreadOutOfForumChannel', + 'CannotUnpinThreadOutOfForumChannel', 'InvalidType', 'InvalidElement', diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 24437b5023c9..786685040da3 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -95,6 +95,8 @@ const Messages = { [DjsErrorCodes.StageChannelResolve]: 'Could not resolve channel to a stage channel.', [DjsErrorCodes.GuildScheduledEventResolve]: 'Could not resolve the guild scheduled event.', [DjsErrorCodes.FetchOwnerId]: "Couldn't resolve the guild ownerId to fetch the member.", + [DjsErrorCodes.CannotPinThreadOutOfForumChannel]: 'Cannot pin a thread which is not in a ForumChannel.', + [DjsErrorCodes.CannotUnpinThreadOutOfForumChannel]: 'Cannot unpin a thread which is not in a ForumChannel.', [DjsErrorCodes.InvalidType]: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`, [DjsErrorCodes.InvalidElement]: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`, diff --git a/packages/discord.js/src/structures/ThreadChannel.js b/packages/discord.js/src/structures/ThreadChannel.js index ea10d1f8781c..3d95d38d3275 100644 --- a/packages/discord.js/src/structures/ThreadChannel.js +++ b/packages/discord.js/src/structures/ThreadChannel.js @@ -1,6 +1,6 @@ 'use strict'; -const { ChannelType, PermissionFlagsBits, Routes } = require('discord-api-types/v10'); +const { ChannelType, PermissionFlagsBits, Routes, ChannelFlags } = require('discord-api-types/v10'); const { BaseChannel } = require('./BaseChannel'); const TextBasedChannel = require('./interfaces/TextBasedChannel'); const { DiscordjsRangeError, ErrorCodes } = require('../errors'); @@ -456,6 +456,27 @@ class ThreadChannel extends BaseChannel { setAppliedTags(appliedTags, reason) { return this.edit({ appliedTags, reason }); } + + /** + * Pins this thread channel from the forum channel. + * @param {string} [reason] Reason for pinning + * @returns {Promise} + */ + pin(reason) { + if (this.parent.type !== ChannelType.ForumChannel) throw new DiscordjsError(ErrorCodes.CannotPinThreadOutOfForumChannel); + return this.edit({ flags: [ChannelFlags.Pinned], reason }); + } + + /** + * Unpins this thread channel from the forum channel. + * @param {string} [reason] Reason for unpinning + * @returns {Promise} + */ + unpin(reason) { + if (this.parent.type !== ChannelType.ForumChannel) throw new DiscordjsError(ErrorCodes.CannotUnpinThreadOutOfForumChannel); + const flags = [this.flags.remove(ChannelFlags.Pinned)]; + return this.edit({ flags, reason }); + } /** * Whether the client user is a member of the thread. diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index e2586331a58d..f75fe270ae87 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -2640,6 +2640,8 @@ export class ThreadChannel extends TextBasedCha public setLocked(locked?: boolean, reason?: string): Promise; public setName(name: string, reason?: string): Promise; public setAppliedTags(appliedTags: Snowflake[], reason?: string): Promise>; + public pin(reason?: string): Promise>; + public unpin(reason?: string): Promise>; public toString(): ChannelMention; } From 443518ec49d4797a2894f3ef6ab75e1dc60c3f20 Mon Sep 17 00:00:00 2001 From: Idris Date: Wed, 26 Oct 2022 00:20:41 +0200 Subject: [PATCH 2/9] fix: remove erros --- packages/discord.js/src/errors/ErrorCodes.js | 4 ---- packages/discord.js/src/errors/Messages.js | 2 -- packages/discord.js/src/structures/ThreadChannel.js | 10 ++++------ 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/discord.js/src/errors/ErrorCodes.js b/packages/discord.js/src/errors/ErrorCodes.js index 53ccf861898c..f0566a0433a8 100644 --- a/packages/discord.js/src/errors/ErrorCodes.js +++ b/packages/discord.js/src/errors/ErrorCodes.js @@ -90,8 +90,6 @@ * @property {'StageChannelResolve'} StageChannelResolve * @property {'GuildScheduledEventResolve'} GuildScheduledEventResolve * @property {'FetchOwnerId'} FetchOwnerId - * @property {'CannotPinThreadOutOfForumChannel'} CannotPinThreadOutOfForumChannel - * @property {'CannotUnpinThreadOutOfForumChannel'} CannotUnpinThreadOutOfForumChannel * @property {'InvalidType'} InvalidType * @property {'InvalidElement'} InvalidElement @@ -238,8 +236,6 @@ const keys = [ 'StageChannelResolve', 'GuildScheduledEventResolve', 'FetchOwnerId', - 'CannotPinThreadOutOfForumChannel', - 'CannotUnpinThreadOutOfForumChannel', 'InvalidType', 'InvalidElement', diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 786685040da3..24437b5023c9 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -95,8 +95,6 @@ const Messages = { [DjsErrorCodes.StageChannelResolve]: 'Could not resolve channel to a stage channel.', [DjsErrorCodes.GuildScheduledEventResolve]: 'Could not resolve the guild scheduled event.', [DjsErrorCodes.FetchOwnerId]: "Couldn't resolve the guild ownerId to fetch the member.", - [DjsErrorCodes.CannotPinThreadOutOfForumChannel]: 'Cannot pin a thread which is not in a ForumChannel.', - [DjsErrorCodes.CannotUnpinThreadOutOfForumChannel]: 'Cannot unpin a thread which is not in a ForumChannel.', [DjsErrorCodes.InvalidType]: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`, [DjsErrorCodes.InvalidElement]: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`, diff --git a/packages/discord.js/src/structures/ThreadChannel.js b/packages/discord.js/src/structures/ThreadChannel.js index 3d95d38d3275..44749df08652 100644 --- a/packages/discord.js/src/structures/ThreadChannel.js +++ b/packages/discord.js/src/structures/ThreadChannel.js @@ -456,24 +456,22 @@ class ThreadChannel extends BaseChannel { setAppliedTags(appliedTags, reason) { return this.edit({ appliedTags, reason }); } - + /** * Pins this thread channel from the forum channel. * @param {string} [reason] Reason for pinning * @returns {Promise} */ - pin(reason) { - if (this.parent.type !== ChannelType.ForumChannel) throw new DiscordjsError(ErrorCodes.CannotPinThreadOutOfForumChannel); + pin(reason) { return this.edit({ flags: [ChannelFlags.Pinned], reason }); } - + /** * Unpins this thread channel from the forum channel. * @param {string} [reason] Reason for unpinning * @returns {Promise} */ - unpin(reason) { - if (this.parent.type !== ChannelType.ForumChannel) throw new DiscordjsError(ErrorCodes.CannotUnpinThreadOutOfForumChannel); + unpin(reason) { const flags = [this.flags.remove(ChannelFlags.Pinned)]; return this.edit({ flags, reason }); } From e83aa2325d4feb7b981d1471a5f19ec6bacf73bb Mon Sep 17 00:00:00 2001 From: Idris <78701338+Idris1401@users.noreply.github.com> Date: Wed, 26 Oct 2022 00:29:09 +0200 Subject: [PATCH 3/9] Update packages/discord.js/src/structures/ThreadChannel.js Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> --- packages/discord.js/src/structures/ThreadChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/structures/ThreadChannel.js b/packages/discord.js/src/structures/ThreadChannel.js index 44749df08652..b67d1a650839 100644 --- a/packages/discord.js/src/structures/ThreadChannel.js +++ b/packages/discord.js/src/structures/ThreadChannel.js @@ -467,7 +467,7 @@ class ThreadChannel extends BaseChannel { } /** - * Unpins this thread channel from the forum channel. + * Unpins this thread from the forum channel. * @param {string} [reason] Reason for unpinning * @returns {Promise} */ From 0099b23d150ca42a1942541d43cc472665293085 Mon Sep 17 00:00:00 2001 From: Idris <78701338+Idris1401@users.noreply.github.com> Date: Wed, 26 Oct 2022 00:29:24 +0200 Subject: [PATCH 4/9] Update packages/discord.js/src/structures/ThreadChannel.js Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> --- packages/discord.js/src/structures/ThreadChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/structures/ThreadChannel.js b/packages/discord.js/src/structures/ThreadChannel.js index b67d1a650839..32d41acaa3ad 100644 --- a/packages/discord.js/src/structures/ThreadChannel.js +++ b/packages/discord.js/src/structures/ThreadChannel.js @@ -458,7 +458,7 @@ class ThreadChannel extends BaseChannel { } /** - * Pins this thread channel from the forum channel. + * Pins this thread from the forum channel. * @param {string} [reason] Reason for pinning * @returns {Promise} */ From 93dfaf383e6d81d20dbf557c9a9e9d8e84f8c170 Mon Sep 17 00:00:00 2001 From: Idris <78701338+Idris1401@users.noreply.github.com> Date: Wed, 26 Oct 2022 00:31:02 +0200 Subject: [PATCH 5/9] Update packages/discord.js/src/structures/ThreadChannel.js Co-authored-by: Almeida --- packages/discord.js/src/structures/ThreadChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/structures/ThreadChannel.js b/packages/discord.js/src/structures/ThreadChannel.js index 32d41acaa3ad..c6e7cd0b2c04 100644 --- a/packages/discord.js/src/structures/ThreadChannel.js +++ b/packages/discord.js/src/structures/ThreadChannel.js @@ -463,7 +463,7 @@ class ThreadChannel extends BaseChannel { * @returns {Promise} */ pin(reason) { - return this.edit({ flags: [ChannelFlags.Pinned], reason }); + return this.edit({ flags: this.flags.add(ChannelFlags.Pinned), reason }); } /** From e52e5145130fedd80d7f6c479727075f81fde611 Mon Sep 17 00:00:00 2001 From: Idris <78701338+Idris1401@users.noreply.github.com> Date: Wed, 26 Oct 2022 00:31:16 +0200 Subject: [PATCH 6/9] Update packages/discord.js/src/structures/ThreadChannel.js Co-authored-by: Almeida --- packages/discord.js/src/structures/ThreadChannel.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/discord.js/src/structures/ThreadChannel.js b/packages/discord.js/src/structures/ThreadChannel.js index c6e7cd0b2c04..975ec3ada03b 100644 --- a/packages/discord.js/src/structures/ThreadChannel.js +++ b/packages/discord.js/src/structures/ThreadChannel.js @@ -472,8 +472,7 @@ class ThreadChannel extends BaseChannel { * @returns {Promise} */ unpin(reason) { - const flags = [this.flags.remove(ChannelFlags.Pinned)]; - return this.edit({ flags, reason }); + return this.edit({ flags: this.flags.remove(ChannelFlags.Pinned), reason }); } /** From 3c54e9b6050412b5ff32e63abb92bf01525c23b4 Mon Sep 17 00:00:00 2001 From: Idris <78701338+Idris1401@users.noreply.github.com> Date: Thu, 27 Oct 2022 13:12:50 +0200 Subject: [PATCH 7/9] Update packages/discord.js/typings/index.d.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Aura Román --- packages/discord.js/typings/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index f75fe270ae87..07dfc3e70867 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -2640,8 +2640,8 @@ export class ThreadChannel extends TextBasedCha public setLocked(locked?: boolean, reason?: string): Promise; public setName(name: string, reason?: string): Promise; public setAppliedTags(appliedTags: Snowflake[], reason?: string): Promise>; - public pin(reason?: string): Promise>; - public unpin(reason?: string): Promise>; + public pin(reason?: string): Promise; + public unpin(reason?: string): Promise; public toString(): ChannelMention; } From 88ed02e0f32abdd6c5621a4288f3966639ee1a0e Mon Sep 17 00:00:00 2001 From: Idris Date: Thu, 27 Oct 2022 17:11:09 +0200 Subject: [PATCH 8/9] docs(ThreadChannel): improve description --- packages/discord.js/src/structures/ThreadChannel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/discord.js/src/structures/ThreadChannel.js b/packages/discord.js/src/structures/ThreadChannel.js index 975ec3ada03b..dc135560f67c 100644 --- a/packages/discord.js/src/structures/ThreadChannel.js +++ b/packages/discord.js/src/structures/ThreadChannel.js @@ -458,7 +458,7 @@ class ThreadChannel extends BaseChannel { } /** - * Pins this thread from the forum channel. + * Pins this thread from the forum channel (only applicable to forum threads). * @param {string} [reason] Reason for pinning * @returns {Promise} */ @@ -467,7 +467,7 @@ class ThreadChannel extends BaseChannel { } /** - * Unpins this thread from the forum channel. + * Unpins this thread from the forum channel (only applicable to forum threads). * @param {string} [reason] Reason for unpinning * @returns {Promise} */ From fdcb99f7e0d558c4cfc4b50aea439f09fa69ceb4 Mon Sep 17 00:00:00 2001 From: Idris Date: Tue, 1 Nov 2022 19:15:52 +0100 Subject: [PATCH 9/9] types(ThreadChannel): fix types --- packages/discord.js/typings/index.d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 2558c64d112d..d4aee5848062 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -2815,9 +2815,10 @@ export class ThreadChannel extends TextBasedCha public setInvitable(invitable?: boolean, reason?: string): Promise; public setLocked(locked?: boolean, reason?: string): Promise; public setName(name: string, reason?: string): Promise; + // The following 3 methods can only be run on forum threads. public setAppliedTags(appliedTags: Snowflake[], reason?: string): Promise>; - public pin(reason?: string): Promise; - public unpin(reason?: string): Promise; + public pin(reason?: string): Promise>; + public unpin(reason?: string): Promise>; public toString(): ChannelMention; }