mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
refactor: move Guild setPositions methods to managers (#6875)
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
@@ -192,6 +192,31 @@ class GuildChannelManager extends CachedManager {
|
||||
return channels;
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch-updates the guild's channels' positions.
|
||||
* <info>Only one channel's parent can be changed at a time</info>
|
||||
* @param {ChannelPosition[]} channelPositions Channel positions to update
|
||||
* @returns {Promise<Guild>}
|
||||
* @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
|
||||
|
||||
@@ -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<Guild>}
|
||||
* @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
|
||||
* <info>Only ever available for bots</info>
|
||||
|
||||
@@ -1187,24 +1187,14 @@ class Guild extends AnonymousGuild {
|
||||
* <info>Only one channel's parent can be changed at a time</info>
|
||||
* @param {ChannelPosition[]} channelPositions Channel positions to update
|
||||
* @returns {Promise<Guild>}
|
||||
* @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<Guild>}
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
4
typings/index.d.ts
vendored
4
typings/index.d.ts
vendored
@@ -797,6 +797,7 @@ export class Guild extends AnonymousGuild {
|
||||
public setAFKChannel(afkChannel: VoiceChannelResolvable | null, reason?: string): Promise<Guild>;
|
||||
public setAFKTimeout(afkTimeout: number, reason?: string): Promise<Guild>;
|
||||
public setBanner(banner: BufferResolvable | Base64Resolvable | null, reason?: string): Promise<Guild>;
|
||||
/** @deprecated Use {@link GuildChannelManager.setPositions} instead */
|
||||
public setChannelPositions(channelPositions: readonly ChannelPosition[]): Promise<Guild>;
|
||||
public setDefaultMessageNotifications(
|
||||
defaultMessageNotifications: DefaultMessageNotificationLevel | number,
|
||||
@@ -815,6 +816,7 @@ export class Guild extends AnonymousGuild {
|
||||
public setOwner(owner: GuildMemberResolvable, reason?: string): Promise<Guild>;
|
||||
public setPreferredLocale(preferredLocale: string, reason?: string): Promise<Guild>;
|
||||
public setPublicUpdatesChannel(publicUpdatesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
|
||||
/** @deprecated Use {@link RoleManager.setPositions} instead */
|
||||
public setRolePositions(rolePositions: readonly RolePosition[]): Promise<Guild>;
|
||||
public setRulesChannel(rulesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
|
||||
public setSplash(splash: BufferResolvable | Base64Resolvable | null, reason?: string): Promise<Guild>;
|
||||
@@ -2708,6 +2710,7 @@ export class GuildChannelManager extends CachedManager<
|
||||
): Promise<
|
||||
Collection<Snowflake, TextChannel | VoiceChannel | CategoryChannel | NewsChannel | StoreChannel | StageChannel>
|
||||
>;
|
||||
public setPositions(channelPositions: readonly ChannelPosition[]): Promise<Guild>;
|
||||
public fetchActiveThreads(cache?: boolean): Promise<FetchedThreads>;
|
||||
}
|
||||
|
||||
@@ -2895,6 +2898,7 @@ export class RoleManager extends CachedManager<Snowflake, Role, RoleResolvable>
|
||||
public fetch(id?: undefined, options?: BaseFetchOptions): Promise<Collection<Snowflake, Role>>;
|
||||
public create(options?: CreateRoleOptions): Promise<Role>;
|
||||
public edit(role: RoleResolvable, options: RoleData, reason?: string): Promise<Role>;
|
||||
public setPositions(rolePositions: readonly RolePosition[]): Promise<Guild>;
|
||||
}
|
||||
|
||||
export class StageInstanceManager extends CachedManager<Snowflake, StageInstance, StageInstanceResolvable> {
|
||||
|
||||
Reference in New Issue
Block a user