From 71fb33a5fea7398598b603a888e07519fddd56a9 Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Thu, 1 Jul 2021 14:28:11 +0530 Subject: [PATCH] feat(StageChannel): add createStageInstance method & use better naming convention (#5951) Co-authored-by: SpaceEEC --- src/managers/StageInstanceManager.js | 21 ++++++++++----------- src/structures/StageChannel.js | 13 +++++++++++-- src/structures/StageInstance.js | 2 +- typings/index.d.ts | 8 ++++---- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/managers/StageInstanceManager.js b/src/managers/StageInstanceManager.js index a1bdfcd60..3ae0d8f17 100644 --- a/src/managers/StageInstanceManager.js +++ b/src/managers/StageInstanceManager.js @@ -28,31 +28,30 @@ class StageInstanceManager extends BaseManager { /** * Options used to create a stage instance. - * @typedef {Object} CreateStageInstanceOptions - * @property {StageChannel|Snowflake} channel The stage channel whose instance is to be created + * @typedef {Object} StageInstanceCreateOptions * @property {string} topic The topic of the stage instance * @property {PrivacyLevel|number} [privacyLevel] The privacy level of the stage instance */ /** * Creates a new stage instance. - * @param {CreateStageInstanceOptions} options The options to create the stage instance + * @param {StageChannel|Snowflake} channel The stage channel to associate the created stage instance to + * @param {StageInstanceCreateOptions} options The options to create the stage instance * @returns {Promise} * @example * // Create a stage instance - * guild.stageInstances.create({ - * channel: '1234567890123456789', + * guild.stageInstances.create('1234567890123456789', { * topic: 'A very creative topic', * privacyLevel: 'GUILD_ONLY' * }) * .then(stageInstance => console.log(stageInstance)) * .catch(console.error); */ - async create(options) { - if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true); - let { channel, topic, privacyLevel } = options; + async create(channel, options) { const channelID = this.guild.channels.resolveID(channel); if (!channelID) throw new Error('STAGE_CHANNEL_RESOLVE'); + if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true); + let { topic, privacyLevel } = options; if (privacyLevel) privacyLevel = typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel]; @@ -69,7 +68,7 @@ class StageInstanceManager extends BaseManager { /** * Fetches the stage instance associated with a stage channel, if it exists. - * @param {StageChannel|Snowflake} channel The stage channel whose instance is to be fetched + * @param {StageChannel|Snowflake} channel The stage channel whose associated stage instance is to be fetched * @param {BaseFetchOptions} [options] Additional options for this fetch * @returns {Promise} * @example @@ -100,7 +99,7 @@ class StageInstanceManager extends BaseManager { /** * Edits an existing stage instance. - * @param {StageChannel|Snowflake} channel The stage channel whose instance is to be edited + * @param {StageChannel|Snowflake} channel The stage channel whose associated stage instance is to be edited * @param {StageInstanceEditOptions} options The options to edit the stage instance * @returns {Promise} * @example @@ -136,7 +135,7 @@ class StageInstanceManager extends BaseManager { /** * Deletes an existing stage instance. - * @param {StageChannel|Snowflake} channel The stage channel whose instance is to be deleted + * @param {StageChannel|Snowflake} channel The stage channel whose associated stage instance is to be deleted * @returns {Promise} */ async delete(channel) { diff --git a/src/structures/StageChannel.js b/src/structures/StageChannel.js index 788bf9f40..952e1418a 100644 --- a/src/structures/StageChannel.js +++ b/src/structures/StageChannel.js @@ -20,14 +20,23 @@ class StageChannel extends BaseGuildVoiceChannel { } /** - * The instance of this stage channel, if it exists + * The stage instance of this stage channel, if it exists * @type {?StageInstance} * @readonly */ - get instance() { + get stageInstance() { return this.guild.stageInstances.cache.find(stageInstance => stageInstance.channelID === this.id) ?? null; } + /** + * Creates a stage instance associated to this stage channel. + * @param {StageInstanceCreateOptions} options The options to create the stage instance + * @returns {Promise} + */ + createStageInstance(options) { + return this.guild.stageInstances.create(this.id, options); + } + /** * Sets the RTC region of the channel. * @name StageChannel#setRTCRegion diff --git a/src/structures/StageInstance.js b/src/structures/StageInstance.js index 8ea2c4dfa..f6b4771f0 100644 --- a/src/structures/StageInstance.js +++ b/src/structures/StageInstance.js @@ -60,7 +60,7 @@ class StageInstance extends Base { } /** - * The stage channel associated with this instance + * The stage channel associated with this stage instance * @type {?StageChannel} * @readonly */ diff --git a/typings/index.d.ts b/typings/index.d.ts index 7a5377b30..c2f2e196d 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1882,7 +1882,8 @@ declare module 'discord.js' { export class StageChannel extends BaseGuildVoiceChannel { public topic: string | null; public type: 'stage'; - public readonly instance: StageInstance | null; + public readonly stageInstance: StageInstance | null; + public createStageInstance(options: StageInstanceCreateOptions): Promise; } export class StageInstance extends Base { @@ -2643,7 +2644,7 @@ declare module 'discord.js' { export class StageInstanceManager extends BaseManager { constructor(guild: Guild, iterable?: Iterable); public guild: Guild; - public create(options: CreateStageInstanceOptions): Promise; + public create(channel: StageChannel | Snowflake, options: StageInstanceCreateOptions): Promise; public fetch(channel: StageChannel | Snowflake, options?: BaseFetchOptions): Promise; public edit(channel: StageChannel | Snowflake, options: StageInstanceEditOptions): Promise; public delete(channel: StageChannel | Snowflake): Promise; @@ -3226,8 +3227,7 @@ declare module 'discord.js' { reason?: string; } - interface CreateStageInstanceOptions { - channel: StageChannel | Snowflake; + interface StageInstanceCreateOptions { topic: string; privacyLevel?: PrivacyLevel | number; }