diff --git a/src/client/actions/ActionsManager.js b/src/client/actions/ActionsManager.js index ac95aa7fb..d5cf61ed2 100644 --- a/src/client/actions/ActionsManager.js +++ b/src/client/actions/ActionsManager.js @@ -28,6 +28,7 @@ class ActionsManager { this.register(require('./GuildEmojiDelete')); this.register(require('./GuildEmojiUpdate')); this.register(require('./GuildRolesPositionUpdate')); + this.register(require('./GuildChannelsPositionUpdate')); } register(Action) { diff --git a/src/client/actions/GuildChannelsPositionUpdate.js b/src/client/actions/GuildChannelsPositionUpdate.js new file mode 100644 index 000000000..05043550c --- /dev/null +++ b/src/client/actions/GuildChannelsPositionUpdate.js @@ -0,0 +1,23 @@ +const Action = require('./Action'); + +class GuildChannelsPositionUpdate extends Action { + handle(data) { + const client = this.client; + + const guild = client.guilds.get(data.guild_id); + if (guild) { + for (const partialChannel of data.channels) { + const channel = guild.roles.get(partialChannel.id); + if (channel) { + channel.position = partialChannel.position; + } + } + } + + return { + guild, + }; + } +} + +module.exports = GuildChannelsPositionUpdate; diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index cd1987f3e..21d83242d 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -751,6 +751,15 @@ class RESTMethods { .then(() => user); } + updateChannelPositions(guildID, channels) { + return this.rest.makeRequest('patch', Constants.Endpoints.guildChannels(guildID), true, channels).then(() => + this.client.actions.GuildChannelsPositionUpdate.handle({ + guild_id: guildID, + channels, + }).guild + ); + } + setRolePositions(guildID, roles) { return this.rest.makeRequest('patch', Constants.Endpoints.guildRoles(guildID), true, roles).then(() => this.client.actions.GuildRolesPositionUpdate.handle({ diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 28c3b87ce..aac293ef2 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -633,7 +633,27 @@ class Guild { * .catch(console.error); */ createChannel(name, type, overwrites) { - return this.client.rest.methods.createChannel(this, name, type, overwrites); + return this.client.rest.methods.updateChannel(this, name, type, overwrites); + } + + /** + * The data needed for updating a channel's position. + * @typedef {Object} ChannelPosition + * @property {string} id The channel being updated's unique id. + * @property {number} position The new position of the channel. + */ + + /** + * Updates this guild's channel positions as a batch. + * @param {Array} channelPositions Array of objects that defines which channel is going where. + * @returns {Promise} + * @example + * guild.updateChannels([{ id: channelID, position: newChannelIndex }]) + * .then(guild => console.log(`Updated channels for ${guild.id}`)) + * .catch(console.error); + */ + updateChannelPositions(channelPositions) { + return this.client.rest.methods.updateChannelPositions(this.id, channelPositions); } /**