mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 10:33:30 +01:00
fix(Guild): Fix setChannelPositions method (#1900)
This commit is contained in:
@@ -945,31 +945,57 @@ class Guild extends Base {
|
|||||||
* @property {number} position New position for the channel
|
* @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.
|
* Batch-updates the guild's channels' positions.
|
||||||
* @param {ChannelPosition[]} channelPositions Channel positions to update
|
* @param {ChannelPosition[]} channelPositions Channel positions to update
|
||||||
* @returns {Promise<Guild>}
|
* @returns {Promise<Guild>}
|
||||||
* @example
|
* @example
|
||||||
* guild.updateChannels([{ channel: channelID, position: newChannelIndex }])
|
* guild.setChannelPositions([{ channel: channelID, position: newChannelIndex }])
|
||||||
* .then(guild => console.log(`Updated channel positions for ${guild.id}`))
|
* .then(guild => console.log(`Updated channel positions for ${guild.id}`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
setChannelPositions(channelPositions) {
|
setChannelPositions(channelPositions) {
|
||||||
const data = new Array(channelPositions.length);
|
const updatedChannels = channelPositions.map(r => ({
|
||||||
for (let i = 0; i < channelPositions.length; i++) {
|
id: this.client.resolver.resolveChannelID(r.channel),
|
||||||
data[i] = {
|
position: r.position,
|
||||||
id: this.client.resolver.resolveChannelID(channelPositions[i].channel),
|
}));
|
||||||
position: channelPositions[i].position,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.client.api.guilds(this.id).channels.patch({ data: {
|
return this.client.api.guilds(this.id).channels.patch({ data: updatedChannels }).then(() =>
|
||||||
guild_id: this.id,
|
|
||||||
channels: channelPositions,
|
|
||||||
} }).then(() =>
|
|
||||||
this.client.actions.GuildChannelsPositionUpdate.handle({
|
this.client.actions.GuildChannelsPositionUpdate.handle({
|
||||||
guild_id: this.id,
|
guild_id: this.id,
|
||||||
channels: channelPositions,
|
channels: updatedChannels,
|
||||||
}).guild
|
}).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.
|
* Fetches a collection of channels in the current guild sorted by position.
|
||||||
* @param {string} type The channel type
|
* @param {string} type The channel type
|
||||||
|
|||||||
Reference in New Issue
Block a user