backport(Guild): support for createChannel with options object (#2888)

This commit is contained in:
SpaceEEC
2018-10-10 10:05:32 +02:00
committed by GitHub
parent ea3e575546
commit 7ea88adeca
4 changed files with 48 additions and 16 deletions

View File

@@ -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, { return this.rest.makeRequest('post', Endpoints.Guild(guild).channels, true, {
name: channelName, name,
type: channelType ? Constants.ChannelTypes[channelType.toUpperCase()] : 'text', topic,
permission_overwrites: resolvePermissions.call(this, overwrites, guild), type: type ? Constants.ChannelTypes[type.toUpperCase()] : 'text',
}, undefined, reason).then(data => this.client.actions.ChannelCreate.handle(data).channel); 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) { createDM(recipient) {
@@ -328,7 +335,7 @@ class RESTMethods {
data.position = _data.position || channel.position; data.position = _data.position || channel.position;
data.bitrate = _data.bitrate || (channel.bitrate ? channel.bitrate * 1000 : undefined); data.bitrate = _data.bitrate || (channel.bitrate ? channel.bitrate * 1000 : undefined);
data.user_limit = typeof _data.userLimit !== 'undefined' ? _data.userLimit : channel.userLimit; 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 ? data.permission_overwrites = _data.permissionOverwrites ?
resolvePermissions.call(this, _data.permissionOverwrites, channel.guild) : undefined; resolvePermissions.call(this, _data.permissionOverwrites, channel.guild) : undefined;
data.rate_limit_per_user = typeof _data.rateLimitPerUser !== 'undefined' ? data.rate_limit_per_user = typeof _data.rateLimitPerUser !== 'undefined' ?

View File

@@ -1022,9 +1022,12 @@ class Guild {
/** /**
* Creates a new channel in the guild. * Creates a new channel in the guild.
* @param {string} name The name of the new channel * @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 {string|ChannelData} [typeOrOptions='text']
* @param {ChannelCreationOverwrites[]|Collection<Snowflake, PermissionOverwrites>} [overwrites] Permission overwrites * The type of the new channel, either `text` or `voice` or `category`. **(deprecated, use options)**
* @param {string} [reason] Reason for creating this channel * 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>} * @returns {Promise<CategoryChannel|TextChannel|VoiceChannel>}
* @example * @example
* // Create a new text channel * // Create a new text channel
@@ -1041,8 +1044,21 @@ class Guild {
* .then(console.log) * .then(console.log)
* .catch(console.error); * .catch(console.error);
*/ */
createChannel(name, type, overwrites, reason) { createChannel(name, typeOrOptions, permissionOverwrites, reason) {
return this.client.rest.methods.createChannel(this, name, type, overwrites, 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);
} }
/** /**

View File

@@ -276,14 +276,15 @@ class GuildChannel extends Channel {
/** /**
* The data for a guild channel. * The data for a guild channel.
* @typedef {Object} ChannelData * @typedef {Object} ChannelData
* @property {string} [type] The type of the channel (Only when creating)
* @property {string} [name] The name of the channel * @property {string} [name] The name of the channel
* @property {number} [position] The position of the channel * @property {number} [position] The position of the channel
* @property {string} [topic] The topic of the text channel * @property {string} [topic] The topic of the text channel
* @property {boolean} [nsfw] Whether the channel is NSFW * @property {boolean} [nsfw] Whether the channel is NSFW
* @property {number} [bitrate] The bitrate of the voice channel * @property {number} [bitrate] The bitrate of the voice channel
* @property {number} [userLimit] The user limit of the channel * @property {number} [userLimit] The user limit of the channel
* @property {string} [parent] The parent ID of the channel * @property {CategoryChannel|Snowflake} [parent] The parent or parent ID of the channel
* @property {ChannelCreationOverwrites[]|Collection<Snowflake, PermissionOverwrites>} [overwrites] * @property {ChannelCreationOverwrites[]|Collection<Snowflake, PermissionOverwrites>} [permissionOverwrites]
* Overwrites of the channel * Overwrites of the channel
*/ */
@@ -398,8 +399,12 @@ class GuildChannel extends Channel {
* .catch(console.error); * .catch(console.error);
*/ */
clone(name = this.name, withPermissions = true, withTopic = true, reason) { clone(name = this.name, withPermissions = true, withTopic = true, reason) {
return this.guild.createChannel(name, this.type, withPermissions ? this.permissionOverwrites : [], reason) return this.guild.createChannel(name, {
.then(channel => withTopic ? channel.setTopic(this.topic) : channel); type: this.type,
overwritePermissions: withPermissions ? this.overwritePermissions : undefined,
topic: withTopic ? this.topic : undefined,
reason,
});
} }
/** /**

6
typings/index.d.ts vendored
View File

@@ -524,7 +524,8 @@ declare module 'discord.js' {
public addMember(user: UserResolvable, options: AddGuildMemberOptions): Promise<GuildMember>; public addMember(user: UserResolvable, options: AddGuildMemberOptions): Promise<GuildMember>;
public allowDMs(allow: boolean): Promise<Guild>; public allowDMs(allow: boolean): Promise<Guild>;
public ban(user: UserResolvable, options?: BanOptions | number | string): Promise<GuildMember | User | string>; 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 createEmoji(attachment: BufferResolvable | Base64Resolvable, name: string, roles?: Collection<Snowflake, Role> | Role[], reason?: string): Promise<Emoji>;
public createRole(data?: RoleData, reason?: string): Promise<Role>; public createRole(data?: RoleData, reason?: string): Promise<Role>;
public delete(): Promise<Guild>; public delete(): Promise<Guild>;
@@ -1617,12 +1618,15 @@ declare module 'discord.js' {
}; };
type ChannelData = { type ChannelData = {
type?: 'category' | 'text' | 'voice';
name?: string; name?: string;
position?: number; position?: number;
topic?: string; topic?: string;
nsfw?: boolean; nsfw?: boolean;
bitrate?: number; bitrate?: number;
userLimit?: number; userLimit?: number;
parent?: ChannelResolvable;
permissionOverwrites?: PermissionOverwrites[] | ChannelCreationOverwrites[];
rateLimitPerUser?: number; rateLimitPerUser?: number;
}; };