diff --git a/src/client/ClientDataManager.js b/src/client/ClientDataManager.js index e8c5721eb..4eff1031c 100644 --- a/src/client/ClientDataManager.js +++ b/src/client/ClientDataManager.js @@ -2,6 +2,7 @@ const Constants = require('../util/Constants'); const Util = require('../util/Util'); const Guild = require('../structures/Guild'); const User = require('../structures/User'); +const CategoryChannel = require('../structures/CategoryChannel'); const DMChannel = require('../structures/DMChannel'); const Emoji = require('../structures/Emoji'); const TextChannel = require('../structures/TextChannel'); @@ -61,6 +62,9 @@ class ClientDataManager { } else if (data.type === Constants.ChannelTypes.VOICE) { channel = new VoiceChannel(guild, data); guild.channels.set(channel.id, channel); + } else if (data.type === Constants.ChannelTypes.CATEGORY) { + channel = new CategoryChannel(guild, data); + guild.channels.set(channel.id, channel); } } } diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index c621eead4..a896597fc 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -256,7 +256,7 @@ class RESTMethods { if (overwrites instanceof Collection) overwrites = overwrites.array(); return this.rest.makeRequest('post', Endpoints.Guild(guild).channels, true, { name: channelName, - type: channelType, + type: Constants.ChannelTypes[channelType.toUpperCase()], permission_overwrites: overwrites, }, undefined, reason).then(data => this.client.actions.ChannelCreate.handle(data).channel); } @@ -320,6 +320,7 @@ class RESTMethods { data.position = _data.position || channel.position; data.bitrate = _data.bitrate || channel.bitrate; data.user_limit = _data.userLimit || channel.userLimit; + data.parent_id = _data.parent || (channel.parent ? channel.parent.id : undefined); return this.rest.makeRequest('patch', Endpoints.Channel(channel), true, data, undefined, reason).then(newData => this.client.actions.ChannelUpdate.handle(newData).updated ); diff --git a/src/structures/CategoryChannel.js b/src/structures/CategoryChannel.js new file mode 100644 index 000000000..2c063f73d --- /dev/null +++ b/src/structures/CategoryChannel.js @@ -0,0 +1,18 @@ +const GuildChannel = require('./GuildChannel'); + +/** + * Represents a guild category channel on Discord. + * @extends {GuildChannel} + */ +class CategoryChannel extends GuildChannel { + /** + * The channels that are part of this category + * @type {?Collection} + * @readonly + */ + get children() { + return this.guild.channels.filter(c => c.parentID === this.id); + } +} + +module.exports = CategoryChannel; diff --git a/src/structures/Guild.js b/src/structures/Guild.js index fac0141b5..81ed613cf 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -876,7 +876,7 @@ class Guild { /** * 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` + * @param {string} type The type of the new channel, either `text` or `voice` or `category` * @param {Array} [overwrites] Permission overwrites to apply to the new channel * @param {string} [reason] Reason for creating this channel * @returns {Promise} diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index f54e60297..b196dc423 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -35,6 +35,12 @@ class GuildChannel extends Channel { */ this.position = data.position; + /** + * The ID of the category parent of this channel + * @type {?Snowflake} + */ + this.parentID = data.parent_id; + /** * A map of permission overwrites in this channel for roles and users * @type {Collection} @@ -57,6 +63,15 @@ class GuildChannel extends Channel { return sorted.array().indexOf(sorted.get(this.id)); } + /** + * The category parent of this channel + * @type {?CategoryChannel} + * @readonly + */ + get parent() { + return this.guild.channels.get(this.parentID) || null; + } + /** * Gets the overall set of permissions for a user in this channel, taking into account roles and permission * overwrites. @@ -213,7 +228,7 @@ class GuildChannel extends Channel { * .catch(console.error); */ edit(data, reason) { - return this.client.rest.methods.updateChannel(this, data, reason); + return this.client.rest.methods.updateChannel(this, data, reason).then(() => this); } /** @@ -243,7 +258,18 @@ class GuildChannel extends Channel { * .catch(console.error); */ setPosition(position, relative) { - return this.guild.setChannelPosition(this, position, relative).then(() => this); + return this.guild.setChannelPosition(this, position, relative); + } + + /** + * Set a new parent for the guild channel. + * @param {GuildChannel|SnowFlake} parent The new parent for the guild channel + * @param {string} [reason] Reason for changing the guild channel's parent + * @returns {Promise} + */ + setParent(parent, reason) { + parent = this.client.resolver.resolveChannelID(parent); + return this.edit({ parent }, reason); } /** diff --git a/src/structures/OAuth2Application.js b/src/structures/OAuth2Application.js index cfd657d95..90e1ae420 100644 --- a/src/structures/OAuth2Application.js +++ b/src/structures/OAuth2Application.js @@ -124,7 +124,7 @@ class OAuth2Application { /** * Reset the app's secret and bot token. - * This is only available when using a user account. + * This is only available when using a user account. * @returns {OAuth2Application} */ reset() { diff --git a/src/util/Constants.js b/src/util/Constants.js index 36b15c61c..7df35b926 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -275,6 +275,7 @@ exports.ChannelTypes = { DM: 1, VOICE: 2, GROUP_DM: 3, + CATEGORY: 4, }; exports.OPCodes = {