From 555961b3b8da8759349cd0e88f89f98d2e8a6363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Fri, 17 May 2024 14:14:03 +0100 Subject: [PATCH] refactor(GuildChannelManager): improve addFollower errors (#10277) refactor(GuildChannelManager): improve errors Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../src/managers/GuildChannelManager.js | 16 ++++++++++++++-- packages/discord.js/typings/index.d.ts | 4 +++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/discord.js/src/managers/GuildChannelManager.js b/packages/discord.js/src/managers/GuildChannelManager.js index d4c472ebbd48..ffc6f4cc0286 100644 --- a/packages/discord.js/src/managers/GuildChannelManager.js +++ b/packages/discord.js/src/managers/GuildChannelManager.js @@ -98,17 +98,29 @@ class GuildChannelManager extends CachedManager { return super.resolveId(channel); } + /** + * Data that can be resolved to a News Channel object. This can be: + * * A NewsChannel object + * * A Snowflake + * @typedef {NewsChannel|Snowflake} NewsChannelResolvable + */ + /** * Adds the target channel to a channel's followers. - * @param {NewsChannel|Snowflake} channel The channel to follow + * @param {NewsChannelResolvable} channel The channel to follow * @param {TextChannelResolvable} targetChannel The channel where published announcements will be posted at * @param {string} [reason] Reason for creating the webhook * @returns {Promise} Returns created target webhook id. */ async addFollower(channel, targetChannel, reason) { const channelId = this.resolveId(channel); + if (!channelId) { + throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'channel', 'NewsChannelResolvable'); + } const targetChannelId = this.resolveId(targetChannel); - if (!channelId || !targetChannelId) throw new Error(ErrorCodes.GuildChannelResolve); + if (!targetChannelId) { + throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'targetChannel', 'TextChannelResolvable'); + } const { webhook_id } = await this.client.rest.post(Routes.channelFollowers(channelId), { body: { webhook_channel_id: targetChannelId }, reason, diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index c7ed605ce5d8..a4a8896d0c15 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -2464,6 +2464,8 @@ export class NewsChannel extends BaseGuildTextChannel { public addFollower(channel: TextChannelResolvable, reason?: string): Promise; } +export type NewsChannelResolvable = NewsChannel | Snowflake; + export class OAuth2Guild extends BaseGuild { private constructor(client: Client, data: RawOAuth2GuildData); public owner: boolean; @@ -4219,7 +4221,7 @@ export class GuildChannelManager extends CachedManager;