From afaa37616d7cf1f667f7352793a8ba6a13d3217a Mon Sep 17 00:00:00 2001 From: brxxn Date: Sun, 2 Jun 2019 13:14:07 -0400 Subject: [PATCH] [rest] add setRolePositions function Allows for role positions to be batch-updated similar to how channel positions are. It uses the Discord API endpoint PATCH /guild/:id/roles --- src/structures/Guild.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/structures/Guild.js b/src/structures/Guild.js index b1361aef41c8..444889be1996 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -967,6 +967,40 @@ class Guild extends Base { ); } + /** + * The data needed for updating a guild role's position + * @typedef {Object} GuildRolePosition + * @property {GuildRoleResolveable} role The ID of the role + * @property {number} position The position to update + */ + + /** + * Batch-updates the guild's role positions + * @param {GuildRolePosition[]} rolePositions Role positions to update + * @returns {Promise} + * @example + * guild.setRolePositions([{ role: roleID, position: updatedRoleIndex }]) + * .then(guild => console.log(`Role permissions updated for ${guild}`)) + * .catch(console.error); + */ + setRolePositions(rolePositions) { + // Make sure rolePositions are prepared for API + rolePositions = rolePositions.map(o => ({ + id: o.role, + position: o.position, + })); + + // Call the API to update role positions + return this.client.api.guilds(this.id).roles.patch({ + data: rolePositions, + }).then(() => + this.client.actions.GuildRolePositionUpdate.handle({ + guild_id: this.id, + roles: rolePositions, + }).guild + ); + } + /** * Edits the guild's embed. * @param {GuildEmbedData} embed The embed for the guild