From 5cf5071061760c2f9c1e36d7648aef544b03323a Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Wed, 16 Feb 2022 13:02:12 +0000 Subject: [PATCH] feat!: add CategoryChannelChildManager (#7320) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- .../managers/CategoryChannelChildManager.js | 69 +++++++++++++++++++ .../src/structures/CategoryChannel.js | 38 ++-------- packages/discord.js/typings/index.d.ts | 36 ++++++---- packages/discord.js/typings/index.test-d.ts | 19 ++--- 4 files changed, 107 insertions(+), 55 deletions(-) create mode 100644 packages/discord.js/src/managers/CategoryChannelChildManager.js diff --git a/packages/discord.js/src/managers/CategoryChannelChildManager.js b/packages/discord.js/src/managers/CategoryChannelChildManager.js new file mode 100644 index 000000000..028025184 --- /dev/null +++ b/packages/discord.js/src/managers/CategoryChannelChildManager.js @@ -0,0 +1,69 @@ +'use strict'; + +const DataManager = require('./DataManager'); +const GuildChannel = require('../structures/GuildChannel'); + +/** + * Manages API methods for CategoryChannels' children. + * @extends {DataManager} + */ +class CategoryChannelChildManager extends DataManager { + constructor(channel) { + super(channel.client, GuildChannel); + /** + * The category channel this manager belongs to + * @type {CategoryChannel} + */ + this.channel = channel; + } + + /** + * The channels that are a part of this category + * @type {Collection} + * @readonly + */ + get cache() { + return this.guild.channels.cache.filter(c => c.parentId === this.channel.id); + } + + /** + * The guild this manager belongs to + * @type {Guild} + * @readonly + */ + get guild() { + return this.channel.guild; + } + + /** + * Options for creating a channel using {@link CategoryChannel#createChannel}. + * @typedef {Object} CategoryCreateChannelOptions + * @property {ChannelType} [type=ChannelType.GuildText] The type of the new channel. + * @property {string} [topic] The topic for the new channel + * @property {boolean} [nsfw] Whether the new channel is NSFW + * @property {number} [bitrate] Bitrate of the new channel in bits (only voice) + * @property {number} [userLimit] Maximum amount of users allowed in the new channel (only voice) + * @property {OverwriteResolvable[]|Collection} [permissionOverwrites] + * Permission overwrites of the new channel + * @property {number} [position] Position of the new channel + * @property {number} [rateLimitPerUser] The rate limit per user (slowmode) for the new channel in seconds + * @property {string} [rtcRegion] The specific region of the new channel. + * @property {string} [reason] Reason for creating the new channel + */ + + /** + * Creates a new channel within this category. + * You cannot create a channel of type {@link ChannelType.GuildCategory} inside a CategoryChannel. + * @param {string} name The name of the new channel + * @param {CategoryCreateChannelOptions} options Options for creating the new channel + * @returns {Promise} + */ + create(name, options) { + return this.guild.channels.create(name, { + ...options, + parent: this.channel.id, + }); + } +} + +module.exports = CategoryChannelChildManager; diff --git a/packages/discord.js/src/structures/CategoryChannel.js b/packages/discord.js/src/structures/CategoryChannel.js index bebe976a2..6f53858ce 100644 --- a/packages/discord.js/src/structures/CategoryChannel.js +++ b/packages/discord.js/src/structures/CategoryChannel.js @@ -1,6 +1,7 @@ 'use strict'; const GuildChannel = require('./GuildChannel'); +const CategoryChannelChildManager = require('../managers/CategoryChannelChildManager'); /** * Represents a guild category channel on Discord. @@ -8,12 +9,12 @@ const GuildChannel = require('./GuildChannel'); */ class CategoryChannel extends GuildChannel { /** - * Channels that are a part of this category - * @type {Collection} + * A manager of the channels belonging to this category + * @type {CategoryChannelChildManager} * @readonly */ get children() { - return this.guild.channels.cache.filter(c => c.parentId === this.id); + return new CategoryChannelChildManager(this); } /** @@ -26,37 +27,6 @@ class CategoryChannel extends GuildChannel { * @param {SetParentOptions} [options={}] The options for setting the parent * @returns {Promise} */ - - /** - * Options for creating a channel using {@link CategoryChannel#createChannel}. - * @typedef {Object} CategoryCreateChannelOptions - * @property {ChannelType} [type=ChannelType.GuildText] The type of the new channel. - * @property {string} [topic] The topic for the new channel - * @property {boolean} [nsfw] Whether the new channel is NSFW - * @property {number} [bitrate] Bitrate of the new channel in bits (only voice) - * @property {number} [userLimit] Maximum amount of users allowed in the new channel (only voice) - * @property {OverwriteResolvable[]|Collection} [permissionOverwrites] - * Permission overwrites of the new channel - * @property {number} [position] Position of the new channel - * @property {number} [rateLimitPerUser] The rate limit per user (slowmode) for the new channel in seconds - * @property {string} [rtcRegion] The specific region of the new channel. - * @property {string} [reason] Reason for creating the new channel - */ - - /** - * Creates a new channel within this category. - * You cannot create a channel of type {@link ChannelType.GuildCategory} inside a - * CategoryChannel. - * @param {string} name The name of the new channel - * @param {CategoryCreateChannelOptions} options Options for creating the new channel - * @returns {Promise} - */ - createChannel(name, options) { - return this.guild.channels.create(name, { - ...options, - parent: this.id, - }); - } } module.exports = CategoryChannel; diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 203b80ded..433f4d921 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -526,19 +526,8 @@ export type CategoryChannelType = Exclude< >; export class CategoryChannel extends GuildChannel { - public readonly children: Collection>; + public readonly children: CategoryChannelChildManager; public type: ChannelType.GuildCategory; - - public createChannel>( - name: string, - options: CategoryCreateChannelOptions & { type: T }, - ): Promise; - /** @deprecated See [Self-serve Game Selling Deprecation](https://support-dev.discord.com/hc/en-us/articles/4414590563479) for more information */ - public createChannel( - name: string, - options: CategoryCreateChannelOptions & { type: ChannelType.GuildStore }, - ): Promise; - public createChannel(name: string, options?: CategoryCreateChannelOptions): Promise; } export type CategoryChannelResolvable = Snowflake | CategoryChannel; @@ -2792,6 +2781,27 @@ export class BaseGuildEmojiManager extends CachedManager { + private constructor(channel: CategoryChannel); + + public channel: CategoryChannel; + public readonly guild: Guild; + public create>( + name: string, + options: CategoryCreateChannelOptions & { type: T }, + ): Promise; + /** @deprecated See [Self-serve Game Selling Deprecation](https://support-dev.discord.com/hc/en-us/articles/4414590563479) for more information */ + public create( + name: string, + options: CategoryCreateChannelOptions & { type: ChannelType.GuildStore }, + ): Promise; + public create(name: string, options?: CategoryCreateChannelOptions): Promise; +} + export class ChannelManager extends CachedManager { private constructor(client: Client, iterable: Iterable); public fetch(id: Snowflake, options?: FetchChannelOptions): Promise; @@ -4958,6 +4968,8 @@ export type VoiceBasedChannel = Extract; export type GuildBasedChannel = Extract; +export type NonCategoryGuildBasedChannel = Exclude; + export type NonThreadGuildBasedChannel = Exclude; export type GuildTextBasedChannel = Extract; diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index 279afcad8..db55b6284 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -101,6 +101,7 @@ import { Events, ShardEvents, Status, + CategoryChannelChildManager, } from '.'; import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd'; import { Embed } from '@discordjs/builders'; @@ -824,7 +825,7 @@ expectType(newsChannel.lastMessage); expectType(textChannel.lastMessage); expectDeprecated(storeChannel.clone()); -expectDeprecated(categoryChannel.createChannel('Store', { type: ChannelType.GuildStore })); +expectDeprecated(categoryChannelChildManager.create('Store', { type: ChannelType.GuildStore })); expectDeprecated(guild.channels.create('Store', { type: ChannelType.GuildStore })); notPropertyOf(user, 'lastMessage'); @@ -904,15 +905,15 @@ expectType>>(guildApplicationC expectType>>(guildApplicationCommandManager.fetch(undefined, {})); expectType>(guildApplicationCommandManager.fetch('0')); -declare const categoryChannel: CategoryChannel; +declare const categoryChannelChildManager: CategoryChannelChildManager; { - expectType>(categoryChannel.createChannel('name', { type: ChannelType.GuildVoice })); - expectType>(categoryChannel.createChannel('name', { type: ChannelType.GuildText })); - expectType>(categoryChannel.createChannel('name', { type: ChannelType.GuildNews })); - expectDeprecated(categoryChannel.createChannel('name', { type: ChannelType.GuildStore })); - expectType>(categoryChannel.createChannel('name', { type: ChannelType.GuildStageVoice })); - expectType>(categoryChannel.createChannel('name', {})); - expectType>(categoryChannel.createChannel('name')); + expectType>(categoryChannelChildManager.create('name', { type: ChannelType.GuildVoice })); + expectType>(categoryChannelChildManager.create('name', { type: ChannelType.GuildText })); + expectType>(categoryChannelChildManager.create('name', { type: ChannelType.GuildNews })); + expectDeprecated(categoryChannelChildManager.create('name', { type: ChannelType.GuildStore })); + expectType>(categoryChannelChildManager.create('name', { type: ChannelType.GuildStageVoice })); + expectType>(categoryChannelChildManager.create('name', {})); + expectType>(categoryChannelChildManager.create('name')); } declare const guildChannelManager: GuildChannelManager;