From 7ea88adeca3e5916b88ffeb2252c0271c6b92783 Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Wed, 10 Oct 2018 10:05:32 +0200 Subject: [PATCH] backport(Guild): support for createChannel with options object (#2888) --- src/client/rest/RESTMethods.js | 19 +++++++++++++------ src/structures/Guild.js | 26 +++++++++++++++++++++----- src/structures/GuildChannel.js | 13 +++++++++---- typings/index.d.ts | 6 +++++- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 5e78d91a5..b80b463a7 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -260,12 +260,19 @@ class RESTMethods { }); } - createChannel(guild, channelName, channelType, overwrites, reason) { + createChannel(guild, name, { type, topic, nsfw, bitrate, userLimit, parent, permissionOverwrites, reason }) { return this.rest.makeRequest('post', Endpoints.Guild(guild).channels, true, { - name: channelName, - type: channelType ? Constants.ChannelTypes[channelType.toUpperCase()] : 'text', - permission_overwrites: resolvePermissions.call(this, overwrites, guild), - }, undefined, reason).then(data => this.client.actions.ChannelCreate.handle(data).channel); + name, + topic, + type: type ? Constants.ChannelTypes[type.toUpperCase()] : 'text', + nsfw, + bitrate, + user_limit: userLimit, + parent_id: parent instanceof Channel ? parent.id : parent, + permission_overwrites: resolvePermissions.call(this, permissionOverwrites, guild), + }, + undefined, + reason).then(data => this.client.actions.ChannelCreate.handle(data).channel); } createDM(recipient) { @@ -328,7 +335,7 @@ class RESTMethods { data.position = _data.position || channel.position; data.bitrate = _data.bitrate || (channel.bitrate ? channel.bitrate * 1000 : undefined); data.user_limit = typeof _data.userLimit !== 'undefined' ? _data.userLimit : channel.userLimit; - data.parent_id = _data.parent; + data.parent_id = _data.parent instanceof Channel ? _data.parent.id : _data.parent; data.permission_overwrites = _data.permissionOverwrites ? resolvePermissions.call(this, _data.permissionOverwrites, channel.guild) : undefined; data.rate_limit_per_user = typeof _data.rateLimitPerUser !== 'undefined' ? diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 93ebb8667..4316123e1 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -1022,9 +1022,12 @@ class Guild { /** * Creates a new channel in the guild. * @param {string} name The name of the new channel - * @param {string} [type='text'] The type of the new channel, either `text` or `voice` or `category` - * @param {ChannelCreationOverwrites[]|Collection} [overwrites] Permission overwrites - * @param {string} [reason] Reason for creating this channel + * @param {string|ChannelData} [typeOrOptions='text'] + * The type of the new channel, either `text` or `voice` or `category`. **(deprecated, use options)** + * Alternatively options for the new channel, overriding the following parameters. + * @param {ChannelCreationOverwrites[]|Collection} [permissionOverwrites] + * Permission overwrites **(deprecated, use options)** + * @param {string} [reason] Reason for creating this channel **(deprecated, use options)** * @returns {Promise} * @example * // Create a new text channel @@ -1041,8 +1044,21 @@ class Guild { * .then(console.log) * .catch(console.error); */ - createChannel(name, type, overwrites, reason) { - return this.client.rest.methods.createChannel(this, name, type, overwrites, reason); + createChannel(name, typeOrOptions, permissionOverwrites, reason) { + if (!typeOrOptions || (typeof typeOrOptions === 'string')) { + if (typeOrOptions) { + process.emitWarning( + 'Guild#createChannel: Create channels with an options object instead of separate parameters', + 'DeprecationWarning' + ); + } + typeOrOptions = { + type: typeOrOptions, + permissionOverwrites, + reason, + }; + } + return this.client.rest.methods.createChannel(this, name, typeOrOptions); } /** diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index c385908f6..72e5db5af 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -276,14 +276,15 @@ class GuildChannel extends Channel { /** * The data for a guild channel. * @typedef {Object} ChannelData + * @property {string} [type] The type of the channel (Only when creating) * @property {string} [name] The name of the channel * @property {number} [position] The position of the channel * @property {string} [topic] The topic of the text channel * @property {boolean} [nsfw] Whether the channel is NSFW * @property {number} [bitrate] The bitrate of the voice channel * @property {number} [userLimit] The user limit of the channel - * @property {string} [parent] The parent ID of the channel - * @property {ChannelCreationOverwrites[]|Collection} [overwrites] + * @property {CategoryChannel|Snowflake} [parent] The parent or parent ID of the channel + * @property {ChannelCreationOverwrites[]|Collection} [permissionOverwrites] * Overwrites of the channel */ @@ -398,8 +399,12 @@ class GuildChannel extends Channel { * .catch(console.error); */ clone(name = this.name, withPermissions = true, withTopic = true, reason) { - return this.guild.createChannel(name, this.type, withPermissions ? this.permissionOverwrites : [], reason) - .then(channel => withTopic ? channel.setTopic(this.topic) : channel); + return this.guild.createChannel(name, { + type: this.type, + overwritePermissions: withPermissions ? this.overwritePermissions : undefined, + topic: withTopic ? this.topic : undefined, + reason, + }); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index 8cb332349..290ffb7c3 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -524,7 +524,8 @@ declare module 'discord.js' { public addMember(user: UserResolvable, options: AddGuildMemberOptions): Promise; public allowDMs(allow: boolean): Promise; public ban(user: UserResolvable, options?: BanOptions | number | string): Promise; - public createChannel(name: string, type?: 'category' | 'text' | 'voice', overwrites?: PermissionOverwrites[] | ChannelCreationOverwrites[], reason?: string): Promise; + public createChannel(name: string, options?: ChannelData): Promise; + public createChannel(name: string, type?: 'category' | 'text' | 'voice', permissionOverwrites?: PermissionOverwrites[] | ChannelCreationOverwrites[], reason?: string): Promise; public createEmoji(attachment: BufferResolvable | Base64Resolvable, name: string, roles?: Collection | Role[], reason?: string): Promise; public createRole(data?: RoleData, reason?: string): Promise; public delete(): Promise; @@ -1617,12 +1618,15 @@ declare module 'discord.js' { }; type ChannelData = { + type?: 'category' | 'text' | 'voice'; name?: string; position?: number; topic?: string; nsfw?: boolean; bitrate?: number; userLimit?: number; + parent?: ChannelResolvable; + permissionOverwrites?: PermissionOverwrites[] | ChannelCreationOverwrites[]; rateLimitPerUser?: number; };