Skip to content

Commit

Permalink
Fix and make edit_role_position plural (#2954)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski authored Aug 23, 2024
1 parent 8b201e9 commit 0c27128
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/builder/edit_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ impl<'a> EditRole<'a> {
};

if let Some(position) = self.position {
guild_id.edit_role_position(http, role.id, position, self.audit_log_reason).await?;
guild_id
.edit_role_positions(http, [(role.id, position)], self.audit_log_reason)
.await?;
}
Ok(role)
}
Expand Down
4 changes: 2 additions & 2 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2006,8 +2006,8 @@ impl Http {
from_value(value).map_err(From::from)
}

/// Changes the position of a role in a guild.
pub async fn edit_role_position(
/// Changes the positions of roles in a guild.
pub async fn edit_role_positions(
&self,
guild_id: GuildId,
map: &impl serde::Serialize,
Expand Down
34 changes: 23 additions & 11 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,27 +794,39 @@ impl GuildId {
builder.execute(http, self, sticker_id).await
}

/// Edit the position of a [`Role`] relative to all others in the [`Guild`].
/// Edits the position of [`Role`]s relative to all others in the [`Guild`].
///
/// **Note**: Requires the [Manage Roles] permission.
///
/// # Examples
///
/// ```rust,ignore
/// use serenity::model::{GuildId, RoleId};
/// GuildId::new(7).edit_role_position(&context, RoleId::new(8), 2);
/// ```rust,no_run
/// # use std::collections::HashMap;
/// # use serenity::http::Http;
/// use serenity::model::id::{GuildId, RoleId};
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: Http = unimplemented!();
/// let roles = HashMap::from([
/// (RoleId::new(8), 2),
/// (RoleId::new(10), 3),
/// (RoleId::new(11), 4),
/// (RoleId::new(25), 7),
/// ]);
/// GuildId::new(7).edit_role_positions(&http, roles, None);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns an [`Error::Http`] if the current user lacks permission.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
pub async fn edit_role_position(
pub async fn edit_role_positions(
self,
http: &Http,
role_id: RoleId,
position: i16,
roles: impl IntoIterator<Item = (RoleId, i16)>,
reason: Option<&str>,
) -> Result<Vec<Role>> {
#[derive(serde::Serialize)]
Expand All @@ -827,12 +839,12 @@ impl GuildId {
Maximum::AuditLogReason.check_overflow(reason.len())?;
}

let map = EditRole {
id: role_id,
let iter = roles.into_iter().map(|(id, position)| EditRole {
id,
position,
};
});

http.edit_role_position(self, &map, reason).await
http.edit_role_positions(self, &SerializeIter::new(iter), reason).await
}

/// Edits the guild's welcome screen.
Expand Down
26 changes: 20 additions & 6 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,24 +1090,38 @@ impl Guild {
///
/// Change the order of a role:
///
/// ```rust,ignore
/// ```rust,no_run
/// # use std::collections::HashMap;
/// # use serenity::http::Http;
/// # use serenity::model::guild::Guild;
/// use serenity::model::id::RoleId;
/// guild.edit_role_position(&context, RoleId::new(8), 2);
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: Http = unimplemented!();
/// # let guild: Guild = unimplemented!();
/// let roles = HashMap::from([
/// (RoleId::new(8), 2),
/// (RoleId::new(10), 3),
/// (RoleId::new(11), 4),
/// (RoleId::new(25), 7),
/// ]);
/// guild.edit_role_positions(&http, roles, None);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
pub async fn edit_role_position(
pub async fn edit_role_positions(
&self,
http: &Http,
role_id: RoleId,
position: i16,
roles: impl IntoIterator<Item = (RoleId, i16)>,
audit_log_reason: Option<&str>,
) -> Result<Vec<Role>> {
self.id.edit_role_position(http, role_id, position, audit_log_reason).await
self.id.edit_role_positions(http, roles, audit_log_reason).await
}

/// Modifies a scheduled event in the guild with the data set, if any.
Expand Down
26 changes: 20 additions & 6 deletions src/model/guild/partial_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,24 +815,38 @@ impl PartialGuild {
///
/// Change the order of a role:
///
/// ```rust,ignore
/// ```rust,no_run
/// # use std::collections::HashMap;
/// # use serenity::http::Http;
/// # use serenity::model::guild::PartialGuild;
/// use serenity::model::id::RoleId;
/// partial_guild.edit_role_position(&context, RoleId::new(8), 2);
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: Http = unimplemented!();
/// # let partial_guild: PartialGuild = unimplemented!();
/// let roles = HashMap::from([
/// (RoleId::new(8), 2),
/// (RoleId::new(10), 3),
/// (RoleId::new(11), 4),
/// (RoleId::new(25), 7),
/// ]);
/// partial_guild.edit_role_positions(&http, roles, None);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [Manage Roles]: Permissions::MANAGE_ROLES
pub async fn edit_role_position(
pub async fn edit_role_positions(
&self,
http: &Http,
role_id: RoleId,
position: i16,
roles: impl IntoIterator<Item = (RoleId, i16)>,
audit_log_reason: Option<&str>,
) -> Result<Vec<Role>> {
self.id.edit_role_position(http, role_id, position, audit_log_reason).await
self.id.edit_role_positions(http, roles, audit_log_reason).await
}

/// Edits a sticker.
Expand Down

0 comments on commit 0c27128

Please sign in to comment.