Skip to content

Commit

Permalink
feature(CategoryChannel): backport (#2117)
Browse files Browse the repository at this point in the history
* feature(CategoryChannel): backport


fix


no

* ????

* bad ternary
  • Loading branch information
Lewdcario authored and Gawdl3y committed Nov 20, 2017
1 parent b274dba commit cce2480
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/client/ClientDataManager.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/client/rest/RESTMethods.js
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
);
Expand Down
18 changes: 18 additions & 0 deletions 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<Snowflake, GuildChannel>}
* @readonly
*/
get children() {
return this.guild.channels.filter(c => c.parentID === this.id);
}
}

module.exports = CategoryChannel;
2 changes: 1 addition & 1 deletion src/structures/Guild.js
Expand Up @@ -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<PermissionOverwrites|Object>} [overwrites] Permission overwrites to apply to the new channel
* @param {string} [reason] Reason for creating this channel
* @returns {Promise<TextChannel|VoiceChannel>}
Expand Down
30 changes: 28 additions & 2 deletions src/structures/GuildChannel.js
Expand Up @@ -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<Snowflake, PermissionOverwrites>}
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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<GuildChannel>}
*/
setParent(parent, reason) {
parent = this.client.resolver.resolveChannelID(parent);
return this.edit({ parent }, reason);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/OAuth2Application.js
Expand Up @@ -124,7 +124,7 @@ class OAuth2Application {

/**
* Reset the app's secret and bot token.
* <warn>This is only available when using a user account.</warn>
* <warn>This is only available when using a user account.</warn>
* @returns {OAuth2Application}
*/
reset() {
Expand Down
1 change: 1 addition & 0 deletions src/util/Constants.js
Expand Up @@ -275,6 +275,7 @@ exports.ChannelTypes = {
DM: 1,
VOICE: 2,
GROUP_DM: 3,
CATEGORY: 4,
};

exports.OPCodes = {
Expand Down

0 comments on commit cce2480

Please sign in to comment.