fix(Guild): Fix setChannelPositions method (#1900)

This commit is contained in:
Robin B
2017-09-08 00:50:28 +02:00
committed by Crawl
parent e34bd7c09c
commit 41c4999c2b

View File

@@ -945,31 +945,57 @@ class Guild extends Base {
* @property {number} position New position for the channel
*/
/**
* Set the position of a channel in this guild.
* @param {ChannelResolvable} channel The channel to edit, can be a channel object or a channel ID
* @param {number} position The new position of the channel
* @param {boolean} [relative=false] Position Moves the channel relative to its current position
* @returns {Promise<Guild>}
*/
setChannelPosition(channel, position, relative = false) {
if (typeof channel === 'string') {
channel = this.channels.get(channel);
}
if (!(channel instanceof GuildChannel)) {
return Promise.reject(new TypeError('INVALID_TYPE', 'channel', 'GuildChannel nor a Snowflake'));
}
position = Number(position);
if (isNaN(position)) return Promise.reject(new TypeError('INVALID_TYPE', 'position', 'number'));
let updatedChannels = this._sortedChannels(channel.type).array();
Util.moveElementInArray(updatedChannels, channel, position, relative);
updatedChannels = updatedChannels.map((r, i) => ({ id: r.id, position: i }));
return this.client.api.guilds(this.id).channels.patch({ data: updatedChannels })
.then(() =>
this.client.actions.GuildChannelsPositionUpdate.handle({
guild_id: this.id,
channels: updatedChannels,
}).guild
);
}
/**
* Batch-updates the guild's channels' positions.
* @param {ChannelPosition[]} channelPositions Channel positions to update
* @returns {Promise<Guild>}
* @example
* guild.updateChannels([{ channel: channelID, position: newChannelIndex }])
* guild.setChannelPositions([{ channel: channelID, position: newChannelIndex }])
* .then(guild => console.log(`Updated channel positions for ${guild.id}`))
* .catch(console.error);
*/
setChannelPositions(channelPositions) {
const data = new Array(channelPositions.length);
for (let i = 0; i < channelPositions.length; i++) {
data[i] = {
id: this.client.resolver.resolveChannelID(channelPositions[i].channel),
position: channelPositions[i].position,
};
}
const updatedChannels = channelPositions.map(r => ({
id: this.client.resolver.resolveChannelID(r.channel),
position: r.position,
}));
return this.client.api.guilds(this.id).channels.patch({ data: {
guild_id: this.id,
channels: channelPositions,
} }).then(() =>
return this.client.api.guilds(this.id).channels.patch({ data: updatedChannels }).then(() =>
this.client.actions.GuildChannelsPositionUpdate.handle({
guild_id: this.id,
channels: channelPositions,
channels: updatedChannels,
}).guild
);
}
@@ -1176,38 +1202,6 @@ class Guild extends Base {
);
}
/**
* Set the position of a channel in this guild.
* @param {ChannelResolvable} channel The channel to edit, can be a channel object or a channel ID
* @param {number} position The new position of the channel
* @param {boolean} [relative=false] Position Moves the channel relative to its current position
* @returns {Promise<Guild>}
*/
setChannelPosition(channel, position, relative = false) {
if (typeof channel === 'string') {
channel = this.channels.get(channel);
}
if (!(channel instanceof GuildChannel)) {
return Promise.reject(new TypeError('INVALID_TYPE', 'channel', 'GuildChannel nor a Snowflake'));
}
position = Number(position);
if (isNaN(position)) return Promise.reject(new TypeError('INVALID_TYPE', 'position', 'number'));
let updatedChannels = this._sortedChannels(channel.type).array();
Util.moveElementInArray(updatedChannels, channel, position, relative);
updatedChannels = updatedChannels.map((r, i) => ({ id: r.id, position: i }));
return this.client.api.guilds(this.id).channels.patch({ data: updatedChannels })
.then(() =>
this.client.actions.GuildChannelsPositionUpdate.handle({
guild_id: this.id,
channels: updatedChannels,
}).guild
);
}
/**
* Fetches a collection of channels in the current guild sorted by position.
* @param {string} type The channel type