From 4a87da356ec095354d9fba5518336baf6da3d3a3 Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Sun, 1 Nov 2020 22:22:31 +0600 Subject: [PATCH 01/10] feat: implement GuildChannelManager#fetch --- src/managers/GuildChannelManager.js | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/managers/GuildChannelManager.js b/src/managers/GuildChannelManager.js index ab4f1f2d7591..705d3ac70236 100644 --- a/src/managers/GuildChannelManager.js +++ b/src/managers/GuildChannelManager.js @@ -3,6 +3,7 @@ const BaseManager = require('./BaseManager'); const GuildChannel = require('../structures/GuildChannel'); const PermissionOverwrites = require('../structures/PermissionOverwrites'); +const Collection = require('../util/Collection'); const { ChannelTypes } = require('../util/Constants'); /** @@ -126,6 +127,36 @@ class GuildChannelManager extends BaseManager { }); return this.client.actions.ChannelCreate.handle(data).channel; } + + /** + * Obtains one or more guild channels from Discord, or the channel cache if they're already available. + * @param {Snowflake} [id] ID of the channel + * @param {boolean} [cache=true] Whether to cache the new channel objects if it weren't already + * @param {boolean} [force=false] Whether to skip the cache check and request the API + * @returns {Promise>} + * @example + * // Fetch all channels from the guild + * message.guild.channels.fetch() + * .then(channels => console.log(`There are ${channels.size} channels.`)) + * .catch(console.error); + * @example + * // Fetch a single channel + * message.guild.channels.fetch('222197033908436994') + * .then(channel => console.log(`The channel name is: ${channel.name}`)) + * .catch(console.error); + */ + async fetch(id, cache = true, force = false) { + if (id && !force) { + const existing = this.cache.get(id); + if (existing) return existing; + } + + // We cannot fetch a single guild channel, as of this commit's date, Discord API throws with 404 + const data = await this.client.api.guilds(this.guild.id).channels.get(); + const channels = new Collection(); + for (const channel of data) channels.set(channel.id, this.add(channel, cache)); + return id ? channels.get(id) || null : channels; + } } module.exports = GuildChannelManager; From ad6032e4b38e83dee67886933a52e74d07c4704d Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Sun, 1 Nov 2020 22:49:35 +0600 Subject: [PATCH 02/10] typings: GuildChannelManager#fetch --- typings/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/typings/index.d.ts b/typings/index.d.ts index 041039f9c884..3614e1549bdf 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1913,6 +1913,8 @@ declare module 'discord.js' { name: string, options: GuildCreateChannelOptions, ): Promise; + public fetch(id: Snowflake, cache?: Boolean, force?: Boolean): Promise; + public fetch(id?: Snowflake, cache?: Boolean, force?: Boolean): Promise>; } export class GuildEmojiManager extends BaseManager { From 567f8c56d084537741f77535d1872d06d6fcae28 Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Sun, 1 Nov 2020 23:02:16 +0600 Subject: [PATCH 03/10] typings: fix typo --- typings/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 3614e1549bdf..77e1a4c50189 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1913,8 +1913,8 @@ declare module 'discord.js' { name: string, options: GuildCreateChannelOptions, ): Promise; - public fetch(id: Snowflake, cache?: Boolean, force?: Boolean): Promise; - public fetch(id?: Snowflake, cache?: Boolean, force?: Boolean): Promise>; + public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise; + public fetch(id?: Snowflake, cache?: boolean, force?: boolean): Promise>; } export class GuildEmojiManager extends BaseManager { From 33960b153eac7be3814ee711473348cf2050e5c3 Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Fri, 6 Nov 2020 14:38:20 +0600 Subject: [PATCH 04/10] GuildChannelManager#fetch: fix return type Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> --- src/managers/GuildChannelManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managers/GuildChannelManager.js b/src/managers/GuildChannelManager.js index 705d3ac70236..43b903179fed 100644 --- a/src/managers/GuildChannelManager.js +++ b/src/managers/GuildChannelManager.js @@ -133,7 +133,7 @@ class GuildChannelManager extends BaseManager { * @param {Snowflake} [id] ID of the channel * @param {boolean} [cache=true] Whether to cache the new channel objects if it weren't already * @param {boolean} [force=false] Whether to skip the cache check and request the API - * @returns {Promise>} + * @returns {Promise<(?GuildChannel)|Collection>} * @example * // Fetch all channels from the guild * message.guild.channels.fetch() From 9edb4d62d0c0a2f508641f5214ae1ef56c0e2b0c Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Fri, 6 Nov 2020 15:59:08 +0600 Subject: [PATCH 05/10] GuildChannelManager#fetch: fix incorrect jsdoc --- src/managers/GuildChannelManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managers/GuildChannelManager.js b/src/managers/GuildChannelManager.js index 43b903179fed..8b7c9687ac78 100644 --- a/src/managers/GuildChannelManager.js +++ b/src/managers/GuildChannelManager.js @@ -133,7 +133,7 @@ class GuildChannelManager extends BaseManager { * @param {Snowflake} [id] ID of the channel * @param {boolean} [cache=true] Whether to cache the new channel objects if it weren't already * @param {boolean} [force=false] Whether to skip the cache check and request the API - * @returns {Promise<(?GuildChannel)|Collection>} + * @returns {Promise>} * @example * // Fetch all channels from the guild * message.guild.channels.fetch() From e3df0cf1c05b2f3bb865e66b68f65b271b24354d Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Fri, 6 Nov 2020 16:02:28 +0600 Subject: [PATCH 06/10] GuildChannelManager#fetch: properly cache the channel(s) Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> --- src/managers/GuildChannelManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managers/GuildChannelManager.js b/src/managers/GuildChannelManager.js index 8b7c9687ac78..bae4d5ea1faf 100644 --- a/src/managers/GuildChannelManager.js +++ b/src/managers/GuildChannelManager.js @@ -154,7 +154,7 @@ class GuildChannelManager extends BaseManager { // We cannot fetch a single guild channel, as of this commit's date, Discord API throws with 404 const data = await this.client.api.guilds(this.guild.id).channels.get(); const channels = new Collection(); - for (const channel of data) channels.set(channel.id, this.add(channel, cache)); + for (const channel of data) channels.set(channel.id, this.client.channels.add(channel, this, cache)); return id ? channels.get(id) || null : channels; } } From e63b2b930f1ffd4ca42cd81e2042198a9e85831f Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Thu, 12 Nov 2020 12:21:20 +0600 Subject: [PATCH 07/10] ChannelManager#add: pass correct parameters Co-authored-by: Avocado <43632131+Awoocado@users.noreply.github.com> --- src/managers/GuildChannelManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managers/GuildChannelManager.js b/src/managers/GuildChannelManager.js index bae4d5ea1faf..f14866d4452b 100644 --- a/src/managers/GuildChannelManager.js +++ b/src/managers/GuildChannelManager.js @@ -154,7 +154,7 @@ class GuildChannelManager extends BaseManager { // We cannot fetch a single guild channel, as of this commit's date, Discord API throws with 404 const data = await this.client.api.guilds(this.guild.id).channels.get(); const channels = new Collection(); - for (const channel of data) channels.set(channel.id, this.client.channels.add(channel, this, cache)); + for (const channel of data) channels.set(channel.id, this.client.channels.add(channel, this.guild, cache)); return id ? channels.get(id) || null : channels; } } From fa6084c66697e214813ee0122c06bfd7e0fa9f68 Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Fri, 18 Dec 2020 17:22:37 +0600 Subject: [PATCH 08/10] refactor --- src/managers/GuildChannelManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managers/GuildChannelManager.js b/src/managers/GuildChannelManager.js index f14866d4452b..7d2dc13e3713 100644 --- a/src/managers/GuildChannelManager.js +++ b/src/managers/GuildChannelManager.js @@ -155,7 +155,7 @@ class GuildChannelManager extends BaseManager { const data = await this.client.api.guilds(this.guild.id).channels.get(); const channels = new Collection(); for (const channel of data) channels.set(channel.id, this.client.channels.add(channel, this.guild, cache)); - return id ? channels.get(id) || null : channels; + return id ? channels.get(id) ?? null : channels; } } From af287ef01f565d44e811ae3fbb36152da642ac2d Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Thu, 6 May 2021 12:09:06 +0600 Subject: [PATCH 09/10] feat: fix typings for create --- typings/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index fa1bd7ce4f76..ddab53e0f72a 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1984,7 +1984,7 @@ declare module 'discord.js' { public create( name: string, options: GuildCreateChannelOptions, - ): Promise; + ): Promise; public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise; public fetch(id?: Snowflake, cache?: boolean, force?: boolean): Promise>; } From e00e46c4fb0f75859edeacfba85ad97bca3c34b5 Mon Sep 17 00:00:00 2001 From: Ishmaam Khan Date: Thu, 6 May 2021 15:34:57 +0600 Subject: [PATCH 10/10] typings: +1 --- typings/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index ddab53e0f72a..68fa23024d46 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1986,7 +1986,7 @@ declare module 'discord.js' { options: GuildCreateChannelOptions, ): Promise; public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise; - public fetch(id?: Snowflake, cache?: boolean, force?: boolean): Promise>; + public fetch(id?: Snowflake, cache?: boolean, force?: boolean): Promise>; } export class GuildEmojiManager extends BaseGuildEmojiManager {