feat(StageChannel): add createStageInstance method & use better naming convention (#5951)

Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
Shubham Parihar
2021-07-01 14:28:11 +05:30
committed by GitHub
parent 388e05b4af
commit 71fb33a5fe
4 changed files with 26 additions and 18 deletions

View File

@@ -28,31 +28,30 @@ class StageInstanceManager extends BaseManager {
/** /**
* Options used to create a stage instance. * Options used to create a stage instance.
* @typedef {Object} CreateStageInstanceOptions * @typedef {Object} StageInstanceCreateOptions
* @property {StageChannel|Snowflake} channel The stage channel whose instance is to be created
* @property {string} topic The topic of the stage instance * @property {string} topic The topic of the stage instance
* @property {PrivacyLevel|number} [privacyLevel] The privacy level of the stage instance * @property {PrivacyLevel|number} [privacyLevel] The privacy level of the stage instance
*/ */
/** /**
* Creates a new 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<StageInstance>} * @returns {Promise<StageInstance>}
* @example * @example
* // Create a stage instance * // Create a stage instance
* guild.stageInstances.create({ * guild.stageInstances.create('1234567890123456789', {
* channel: '1234567890123456789',
* topic: 'A very creative topic', * topic: 'A very creative topic',
* privacyLevel: 'GUILD_ONLY' * privacyLevel: 'GUILD_ONLY'
* }) * })
* .then(stageInstance => console.log(stageInstance)) * .then(stageInstance => console.log(stageInstance))
* .catch(console.error); * .catch(console.error);
*/ */
async create(options) { async create(channel, options) {
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
let { channel, topic, privacyLevel } = options;
const channelID = this.guild.channels.resolveID(channel); const channelID = this.guild.channels.resolveID(channel);
if (!channelID) throw new Error('STAGE_CHANNEL_RESOLVE'); 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]; 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. * 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 * @param {BaseFetchOptions} [options] Additional options for this fetch
* @returns {Promise<StageInstance>} * @returns {Promise<StageInstance>}
* @example * @example
@@ -100,7 +99,7 @@ class StageInstanceManager extends BaseManager {
/** /**
* Edits an existing stage instance. * 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 * @param {StageInstanceEditOptions} options The options to edit the stage instance
* @returns {Promise<StageInstance>} * @returns {Promise<StageInstance>}
* @example * @example
@@ -136,7 +135,7 @@ class StageInstanceManager extends BaseManager {
/** /**
* Deletes an existing stage instance. * 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<void>} * @returns {Promise<void>}
*/ */
async delete(channel) { async delete(channel) {

View File

@@ -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} * @type {?StageInstance}
* @readonly * @readonly
*/ */
get instance() { get stageInstance() {
return this.guild.stageInstances.cache.find(stageInstance => stageInstance.channelID === this.id) ?? null; 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<StageInstance>}
*/
createStageInstance(options) {
return this.guild.stageInstances.create(this.id, options);
}
/** /**
* Sets the RTC region of the channel. * Sets the RTC region of the channel.
* @name StageChannel#setRTCRegion * @name StageChannel#setRTCRegion

View File

@@ -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} * @type {?StageChannel}
* @readonly * @readonly
*/ */

8
typings/index.d.ts vendored
View File

@@ -1882,7 +1882,8 @@ declare module 'discord.js' {
export class StageChannel extends BaseGuildVoiceChannel { export class StageChannel extends BaseGuildVoiceChannel {
public topic: string | null; public topic: string | null;
public type: 'stage'; public type: 'stage';
public readonly instance: StageInstance | null; public readonly stageInstance: StageInstance | null;
public createStageInstance(options: StageInstanceCreateOptions): Promise<StageInstance>;
} }
export class StageInstance extends Base { export class StageInstance extends Base {
@@ -2643,7 +2644,7 @@ declare module 'discord.js' {
export class StageInstanceManager extends BaseManager<Snowflake, StageInstance, StageInstanceResolvable> { export class StageInstanceManager extends BaseManager<Snowflake, StageInstance, StageInstanceResolvable> {
constructor(guild: Guild, iterable?: Iterable<any>); constructor(guild: Guild, iterable?: Iterable<any>);
public guild: Guild; public guild: Guild;
public create(options: CreateStageInstanceOptions): Promise<StageInstance>; public create(channel: StageChannel | Snowflake, options: StageInstanceCreateOptions): Promise<StageInstance>;
public fetch(channel: StageChannel | Snowflake, options?: BaseFetchOptions): Promise<StageInstance>; public fetch(channel: StageChannel | Snowflake, options?: BaseFetchOptions): Promise<StageInstance>;
public edit(channel: StageChannel | Snowflake, options: StageInstanceEditOptions): Promise<StageInstance>; public edit(channel: StageChannel | Snowflake, options: StageInstanceEditOptions): Promise<StageInstance>;
public delete(channel: StageChannel | Snowflake): Promise<void>; public delete(channel: StageChannel | Snowflake): Promise<void>;
@@ -3226,8 +3227,7 @@ declare module 'discord.js' {
reason?: string; reason?: string;
} }
interface CreateStageInstanceOptions { interface StageInstanceCreateOptions {
channel: StageChannel | Snowflake;
topic: string; topic: string;
privacyLevel?: PrivacyLevel | number; privacyLevel?: PrivacyLevel | number;
} }