From e12a5b6a0cfb4987cc504369a648e3f81ce8257e Mon Sep 17 00:00:00 2001
From: Rodry <38259440+ImRodry@users.noreply.github.com>
Date: Tue, 26 Oct 2021 20:28:56 +0100
Subject: [PATCH] refactor: move Guild setPositions methods to managers (#6875)
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
---
src/managers/GuildChannelManager.js | 25 +++++++++++++++++++++
src/managers/RoleManager.js | 26 ++++++++++++++++++++++
src/structures/Guild.js | 34 +++++------------------------
typings/index.d.ts | 4 ++++
4 files changed, 61 insertions(+), 28 deletions(-)
diff --git a/src/managers/GuildChannelManager.js b/src/managers/GuildChannelManager.js
index 55158c2a8..20865db7c 100644
--- a/src/managers/GuildChannelManager.js
+++ b/src/managers/GuildChannelManager.js
@@ -192,6 +192,31 @@ class GuildChannelManager extends CachedManager {
return channels;
}
+ /**
+ * Batch-updates the guild's channels' positions.
+ * Only one channel's parent can be changed at a time
+ * @param {ChannelPosition[]} channelPositions Channel positions to update
+ * @returns {Promise}
+ * @example
+ * guild.channels.setPositions([{ channel: channelId, position: newChannelIndex }])
+ * .then(guild => console.log(`Updated channel positions for ${guild}`))
+ * .catch(console.error);
+ */
+ async setPositions(channelPositions) {
+ channelPositions = channelPositions.map(r => ({
+ id: this.client.channels.resolveId(r.channel),
+ position: r.position,
+ lock_permissions: r.lockPermissions,
+ parent_id: typeof r.parent !== 'undefined' ? this.channels.resolveId(r.parent) : undefined,
+ }));
+
+ await this.client.api.guilds(this.id).channels.patch({ data: channelPositions });
+ return this.client.actions.GuildChannelsPositionUpdate.handle({
+ guild_id: this.id,
+ channels: channelPositions,
+ }).guild;
+ }
+
/**
* Obtains all active thread channels in the guild from Discord
* @param {boolean} [cache=true] Whether to cache the fetched data
diff --git a/src/managers/RoleManager.js b/src/managers/RoleManager.js
index 21e49bc66..d9a7ada27 100644
--- a/src/managers/RoleManager.js
+++ b/src/managers/RoleManager.js
@@ -218,6 +218,32 @@ class RoleManager extends CachedManager {
return clone;
}
+ /**
+ * Batch-updates the guild's role positions
+ * @param {GuildRolePosition[]} rolePositions Role positions to update
+ * @returns {Promise}
+ * @example
+ * guild.roles.setPositions([{ role: roleId, position: updatedRoleIndex }])
+ * .then(guild => console.log(`Role positions updated for ${guild}`))
+ * .catch(console.error);
+ */
+ async setPositions(rolePositions) {
+ // Make sure rolePositions are prepared for API
+ rolePositions = rolePositions.map(o => ({
+ id: this.resolveId(o.role),
+ position: o.position,
+ }));
+
+ // Call the API to update role positions
+ await this.client.api.guilds(this.id).roles.patch({
+ data: rolePositions,
+ });
+ return this.client.actions.GuildRolesPositionUpdate.handle({
+ guild_id: this.id,
+ roles: rolePositions,
+ }).guild;
+ }
+
/**
* Gets the managed role a user created when joining the guild, if any
* Only ever available for bots
diff --git a/src/structures/Guild.js b/src/structures/Guild.js
index 4bcbc52f8..d3147971d 100644
--- a/src/structures/Guild.js
+++ b/src/structures/Guild.js
@@ -1187,24 +1187,14 @@ class Guild extends AnonymousGuild {
* Only one channel's parent can be changed at a time
* @param {ChannelPosition[]} channelPositions Channel positions to update
* @returns {Promise}
+ * @deprecated Use {@link GuildChannelManager#setPositions} instead
* @example
* guild.setChannelPositions([{ channel: channelId, position: newChannelIndex }])
* .then(guild => console.log(`Updated channel positions for ${guild}`))
* .catch(console.error);
*/
- async setChannelPositions(channelPositions) {
- const updatedChannels = channelPositions.map(r => ({
- id: this.client.channels.resolveId(r.channel),
- position: r.position,
- lock_permissions: r.lockPermissions,
- parent_id: typeof r.parent !== 'undefined' ? this.channels.resolveId(r.parent) : undefined,
- }));
-
- await this.client.api.guilds(this.id).channels.patch({ data: updatedChannels });
- return this.client.actions.GuildChannelsPositionUpdate.handle({
- guild_id: this.id,
- channels: updatedChannels,
- }).guild;
+ setChannelPositions(channelPositions) {
+ return this.channels.setPositions(channelPositions);
}
/**
@@ -1218,26 +1208,14 @@ class Guild extends AnonymousGuild {
* Batch-updates the guild's role positions
* @param {GuildRolePosition[]} rolePositions Role positions to update
* @returns {Promise}
+ * @deprecated Use {@link RoleManager#setPositions} instead
* @example
* guild.setRolePositions([{ role: roleId, position: updatedRoleIndex }])
* .then(guild => console.log(`Role positions updated for ${guild}`))
* .catch(console.error);
*/
- async setRolePositions(rolePositions) {
- // Make sure rolePositions are prepared for API
- rolePositions = rolePositions.map(o => ({
- id: this.roles.resolveId(o.role),
- position: o.position,
- }));
-
- // Call the API to update role positions
- await this.client.api.guilds(this.id).roles.patch({
- data: rolePositions,
- });
- return this.client.actions.GuildRolesPositionUpdate.handle({
- guild_id: this.id,
- roles: rolePositions,
- }).guild;
+ setRolePositions(rolePositions) {
+ return this.roles.setPositions(rolePositions);
}
/**
diff --git a/typings/index.d.ts b/typings/index.d.ts
index d5199212f..4e51b1c57 100644
--- a/typings/index.d.ts
+++ b/typings/index.d.ts
@@ -797,6 +797,7 @@ export class Guild extends AnonymousGuild {
public setAFKChannel(afkChannel: VoiceChannelResolvable | null, reason?: string): Promise;
public setAFKTimeout(afkTimeout: number, reason?: string): Promise;
public setBanner(banner: BufferResolvable | Base64Resolvable | null, reason?: string): Promise;
+ /** @deprecated Use {@link GuildChannelManager.setPositions} instead */
public setChannelPositions(channelPositions: readonly ChannelPosition[]): Promise;
public setDefaultMessageNotifications(
defaultMessageNotifications: DefaultMessageNotificationLevel | number,
@@ -815,6 +816,7 @@ export class Guild extends AnonymousGuild {
public setOwner(owner: GuildMemberResolvable, reason?: string): Promise;
public setPreferredLocale(preferredLocale: string, reason?: string): Promise;
public setPublicUpdatesChannel(publicUpdatesChannel: TextChannelResolvable | null, reason?: string): Promise;
+ /** @deprecated Use {@link RoleManager.setPositions} instead */
public setRolePositions(rolePositions: readonly RolePosition[]): Promise;
public setRulesChannel(rulesChannel: TextChannelResolvable | null, reason?: string): Promise;
public setSplash(splash: BufferResolvable | Base64Resolvable | null, reason?: string): Promise;
@@ -2708,6 +2710,7 @@ export class GuildChannelManager extends CachedManager<
): Promise<
Collection
>;
+ public setPositions(channelPositions: readonly ChannelPosition[]): Promise;
public fetchActiveThreads(cache?: boolean): Promise;
}
@@ -2895,6 +2898,7 @@ export class RoleManager extends CachedManager
public fetch(id?: undefined, options?: BaseFetchOptions): Promise>;
public create(options?: CreateRoleOptions): Promise;
public edit(role: RoleResolvable, options: RoleData, reason?: string): Promise;
+ public setPositions(rolePositions: readonly RolePosition[]): Promise;
}
export class StageInstanceManager extends CachedManager {