Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport(Guild): createChannel with options object #2888

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/client/rest/RESTMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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' ?
Expand Down
26 changes: 21 additions & 5 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Snowflake, PermissionOverwrites>} [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<Snowflake, PermissionOverwrites>} [permissionOverwrites]
* Permission overwrites **(deprecated, use options)**
* @param {string} [reason] Reason for creating this channel **(deprecated, use options)**
* @returns {Promise<CategoryChannel|TextChannel|VoiceChannel>}
* @example
* // Create a new text channel
Expand All @@ -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);
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/structures/GuildChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Snowflake, PermissionOverwrites>} [overwrites]
* @property {CategoryChannel|Snowflake} [parent] The parent or parent ID of the channel
* @property {ChannelCreationOverwrites[]|Collection<Snowflake, PermissionOverwrites>} [permissionOverwrites]
* Overwrites of the channel
*/

Expand Down Expand Up @@ -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,
});
}

/**
Expand Down
6 changes: 5 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,8 @@ declare module 'discord.js' {
public addMember(user: UserResolvable, options: AddGuildMemberOptions): Promise<GuildMember>;
public allowDMs(allow: boolean): Promise<Guild>;
public ban(user: UserResolvable, options?: BanOptions | number | string): Promise<GuildMember | User | string>;
public createChannel(name: string, type?: 'category' | 'text' | 'voice', overwrites?: PermissionOverwrites[] | ChannelCreationOverwrites[], reason?: string): Promise<CategoryChannel | TextChannel | VoiceChannel>;
public createChannel(name: string, options?: ChannelData): Promise<CategoryChannel | TextChannel | VoiceChannel>;
public createChannel(name: string, type?: 'category' | 'text' | 'voice', permissionOverwrites?: PermissionOverwrites[] | ChannelCreationOverwrites[], reason?: string): Promise<CategoryChannel | TextChannel | VoiceChannel>;
public createEmoji(attachment: BufferResolvable | Base64Resolvable, name: string, roles?: Collection<Snowflake, Role> | Role[], reason?: string): Promise<Emoji>;
public createRole(data?: RoleData, reason?: string): Promise<Role>;
public delete(): Promise<Guild>;
Expand Down Expand Up @@ -1615,12 +1616,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;
};

Expand Down