mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
Implement 'Modify Guild Channel Positions' (#1198)
* Adding shuffleArray method to utils * Shuffle channels functionality on guild. * Comment fix * Removing shuffle functionality and replacing with a simple update * Code review changes to method/variable names * Update comment reference to channelId as well * Updating jsdoc with typedef of ChannelPosition
This commit is contained in:
@@ -28,6 +28,7 @@ class ActionsManager {
|
||||
this.register(require('./GuildEmojiDelete'));
|
||||
this.register(require('./GuildEmojiUpdate'));
|
||||
this.register(require('./GuildRolesPositionUpdate'));
|
||||
this.register(require('./GuildChannelsPositionUpdate'));
|
||||
}
|
||||
|
||||
register(Action) {
|
||||
|
||||
23
src/client/actions/GuildChannelsPositionUpdate.js
Normal file
23
src/client/actions/GuildChannelsPositionUpdate.js
Normal file
@@ -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;
|
||||
@@ -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({
|
||||
|
||||
@@ -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<ChannelPosition>} channelPositions Array of objects that defines which channel is going where.
|
||||
* @returns {Promise<Guild>}
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user