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 #2145

Merged
merged 2 commits into from Dec 6, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/client/ClientDataResolver.js
Expand Up @@ -11,6 +11,7 @@ const Channel = require('../structures/Channel');
const GuildMember = require('../structures/GuildMember');
const Emoji = require('../structures/Emoji');
const ReactionEmoji = require('../structures/ReactionEmoji');
const Role = require('../structures/Role');

/**
* The DataResolver identifies different objects and tries to resolve a specific piece of information from them, e.g.
Expand Down Expand Up @@ -101,6 +102,27 @@ class ClientDataResolver {
return guild.members.get(user.id) || null;
}

/**
* Data that can be resolved to a Role object. This can be:
* * A Role
* * A Snowflake
* @typedef {Role|Snowflake} RoleResolvable
*/

/**
* Resolves a RoleResolvable to a Role object.
* @param {GuildResolvable} guild The guild that this role is part of
* @param {RoleResolvable} role The role resolvable to resolve
* @returns {?Role}
*/
resolveRole(guild, role) {
if (role instanceof Role) return role;
guild = this.resolveGuild(guild);
if (!guild) return null;
if (typeof role === 'string') return guild.roles.get(role);
return null;
}

/**
* Data that can be resolved to give a Channel object. This can be:
* * A Channel object
Expand Down
25 changes: 24 additions & 1 deletion src/client/rest/RESTMethods.js
Expand Up @@ -253,7 +253,30 @@ class RESTMethods {
}

createChannel(guild, channelName, channelType, overwrites, reason) {
if (overwrites instanceof Collection) overwrites = overwrites.array();
if (overwrites instanceof Collection || overwrites instanceof Array) {
overwrites = overwrites.map(overwrite => {
let allow = overwrite.allow || overwrite._allowed;
let deny = overwrite.deny || overwrite._denied;
if (allow instanceof Array) allow = Permissions.resolve(allow);
if (deny instanceof Array) deny = Permissions.resolve(deny);

const role = this.client.resolver.resolveRole(this, overwrite.id);
if (role) {
overwrite.id = role.id;
overwrite.type = 'role';
} else {
overwrite.id = this.client.resolver.resolveUserID(overwrite.id);
overwrite.type = 'member';
}

return {
allow,
deny,
type: overwrite.type,
id: overwrite.id,
};
});
}
return this.rest.makeRequest('post', Endpoints.Guild(guild).channels, true, {
name: channelName,
type: Constants.ChannelTypes[channelType.toUpperCase()],
Expand Down
10 changes: 9 additions & 1 deletion src/structures/Guild.js
Expand Up @@ -873,11 +873,19 @@ class Guild {
if (!this.client.user.bot) this.client.syncGuilds([this]);
}

/**
* Can be used to overwrite permissions when creating a channel.
* @typedef {Object} ChannelCreationOverwrites
* @property {PermissionResolvable[]|number} [allow] The permissions to allow
* @property {PermissionResolvable[]|number} [deny] The permissions to deny
* @property {RoleResolvable|UserResolvable} id ID of the role or member this overwrite is for
*/

/**
* Creates a new channel in the guild.
* @param {string} name The name of the new channel
* @param {string} type The type of the new channel, either `text` or `voice` or `category`
* @param {Array<PermissionOverwrites|Object>} [overwrites] Permission overwrites to apply to the new channel
* @param {Array<PermissionOverwrites|ChannelCreationOverwrites>} [overwrites] Permission overwrites
* @param {string} [reason] Reason for creating this channel
* @returns {Promise<TextChannel|VoiceChannel>}
* @example
Expand Down
4 changes: 2 additions & 2 deletions src/structures/GuildChannel.js
Expand Up @@ -155,7 +155,7 @@ class GuildChannel extends Channel {
* @param {Role|Snowflake|UserResolvable} userOrRole The user or role to update
* @param {PermissionOverwriteOptions} options The configuration for the update
* @param {string} [reason] Reason for creating/editing this overwrite
* @returns {Promise}
* @returns {Promise<GuildChannel>}
* @example
* // Overwrite permissions for a message author
* message.channel.overwritePermissions(message.author, {
Expand Down Expand Up @@ -203,7 +203,7 @@ class GuildChannel extends Channel {
}
}

return this.client.rest.methods.setChannelOverwrite(this, payload, reason);
return this.client.rest.methods.setChannelOverwrite(this, payload, reason).then(() => this);
}

/**
Expand Down