From f6844f921bb0a9ae38e721f756f95a8ba22e8722 Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sun, 13 Oct 2024 12:16:03 +0530 Subject: [PATCH 1/2] fix: throw error on invalid thread channel --- packages/discord.js/src/errors/ErrorCodes.js | 2 ++ packages/discord.js/src/errors/Messages.js | 1 + packages/discord.js/src/managers/ThreadManager.js | 9 +++++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/discord.js/src/errors/ErrorCodes.js b/packages/discord.js/src/errors/ErrorCodes.js index c1552392aa90..27946b5c9b8f 100644 --- a/packages/discord.js/src/errors/ErrorCodes.js +++ b/packages/discord.js/src/errors/ErrorCodes.js @@ -118,6 +118,7 @@ * @property {'MessageThreadParent'} MessageThreadParent * @property {'MessageExistingThread'} MessageExistingThread * @property {'ThreadInvitableType'} ThreadInvitableType + * @property {'InvalidThreadChannel'} InvalidThreadChannel * @property {'WebhookMessage'} WebhookMessage * @property {'WebhookTokenUnavailable'} WebhookTokenUnavailable @@ -278,6 +279,7 @@ const keys = [ 'MessageThreadParent', 'MessageExistingThread', 'ThreadInvitableType', + 'InvalidThreadChannel', 'WebhookMessage', 'WebhookTokenUnavailable', diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 2ee1ab3405e4..74ccf74cdd85 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -78,6 +78,7 @@ const Messages = { [DjsErrorCodes.MessageThreadParent]: 'The message was not sent in a guild text or announcement channel', [DjsErrorCodes.MessageExistingThread]: 'The message already has a thread', [DjsErrorCodes.ThreadInvitableType]: type => `Invitable cannot be edited on ${type}`, + [DjsErrorCodes.InvalidThreadChannel]: 'Provided ThreadChannelResolvable is not a thread of the parent channel.', [DjsErrorCodes.WebhookMessage]: 'The message was not sent by a webhook.', [DjsErrorCodes.WebhookTokenUnavailable]: 'This action requires a webhook token, but none is available.', diff --git a/packages/discord.js/src/managers/ThreadManager.js b/packages/discord.js/src/managers/ThreadManager.js index a8bf2a67e83c..43a076b74b4e 100644 --- a/packages/discord.js/src/managers/ThreadManager.js +++ b/packages/discord.js/src/managers/ThreadManager.js @@ -83,10 +83,15 @@ class ThreadManager extends CachedManager { * .then(channel => console.log(channel.name)) * .catch(console.error); */ - fetch(options, { cache, force } = {}) { + async fetch(options, { cache, force } = {}) { if (!options) return this.fetchActive(cache); const channel = this.client.channels.resolveId(options); - if (channel) return this.client.channels.fetch(channel, { cache, force }); + if (channel) { + const threadChannel = await this.client.channels.fetch(channel, { cache, force }); + if (threadChannel.parentId !== this.channel.id) throw new DiscordjsTypeError(ErrorCodes.InvalidThreadChannel); + return threadChannel; + } + if (options.archived) { return this.fetchArchived(options.archived, cache); } From 31bfb5b460dcd9f44811986ce0fb09ffd6657cbf Mon Sep 17 00:00:00 2001 From: Naiyar <137700126+imnaiyar@users.noreply.github.com> Date: Sun, 13 Oct 2024 19:31:46 +0530 Subject: [PATCH 2/2] chore: rename error code --- packages/discord.js/src/errors/ErrorCodes.js | 4 ++-- packages/discord.js/src/errors/Messages.js | 2 +- packages/discord.js/src/managers/ThreadManager.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/discord.js/src/errors/ErrorCodes.js b/packages/discord.js/src/errors/ErrorCodes.js index 27946b5c9b8f..21ea48b319e2 100644 --- a/packages/discord.js/src/errors/ErrorCodes.js +++ b/packages/discord.js/src/errors/ErrorCodes.js @@ -118,7 +118,7 @@ * @property {'MessageThreadParent'} MessageThreadParent * @property {'MessageExistingThread'} MessageExistingThread * @property {'ThreadInvitableType'} ThreadInvitableType - * @property {'InvalidThreadChannel'} InvalidThreadChannel + * @property {'NotAThreadOfParent'} NotAThreadOfParent * @property {'WebhookMessage'} WebhookMessage * @property {'WebhookTokenUnavailable'} WebhookTokenUnavailable @@ -279,7 +279,7 @@ const keys = [ 'MessageThreadParent', 'MessageExistingThread', 'ThreadInvitableType', - 'InvalidThreadChannel', + 'NotAThreadOfParent', 'WebhookMessage', 'WebhookTokenUnavailable', diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 74ccf74cdd85..133c4d0cee50 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -78,7 +78,7 @@ const Messages = { [DjsErrorCodes.MessageThreadParent]: 'The message was not sent in a guild text or announcement channel', [DjsErrorCodes.MessageExistingThread]: 'The message already has a thread', [DjsErrorCodes.ThreadInvitableType]: type => `Invitable cannot be edited on ${type}`, - [DjsErrorCodes.InvalidThreadChannel]: 'Provided ThreadChannelResolvable is not a thread of the parent channel.', + [DjsErrorCodes.NotAThreadOfParent]: 'Provided ThreadChannelResolvable is not a thread of the parent channel.', [DjsErrorCodes.WebhookMessage]: 'The message was not sent by a webhook.', [DjsErrorCodes.WebhookTokenUnavailable]: 'This action requires a webhook token, but none is available.', diff --git a/packages/discord.js/src/managers/ThreadManager.js b/packages/discord.js/src/managers/ThreadManager.js index 43a076b74b4e..afb75a1e0b7a 100644 --- a/packages/discord.js/src/managers/ThreadManager.js +++ b/packages/discord.js/src/managers/ThreadManager.js @@ -88,7 +88,7 @@ class ThreadManager extends CachedManager { const channel = this.client.channels.resolveId(options); if (channel) { const threadChannel = await this.client.channels.fetch(channel, { cache, force }); - if (threadChannel.parentId !== this.channel.id) throw new DiscordjsTypeError(ErrorCodes.InvalidThreadChannel); + if (threadChannel.parentId !== this.channel.id) throw new DiscordjsTypeError(ErrorCodes.NotAThreadOfParent); return threadChannel; }