Skip to content

Commit

Permalink
Fix @everyone role deserialisation (serenity-rs#2716)
Browse files Browse the repository at this point in the history
The `@everyone` role sometimes has a role position of -1, but not always!
  • Loading branch information
GnomedDev committed Nov 15, 2024
1 parent 2f17cb5 commit d6d5237
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/builder/edit_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct EditRole<'a> {
mentionable: Option<bool>,

#[serde(skip)]
position: Option<u16>,
position: Option<i16>,
#[serde(skip)]
audit_log_reason: Option<&'a str>,
}
Expand Down Expand Up @@ -125,7 +125,7 @@ impl<'a> EditRole<'a> {

/// Set the role's position in the role list. This correlates to the role's position in the
/// user list.
pub fn position(mut self, position: u16) -> Self {
pub fn position(mut self, position: i16) -> Self {
self.position = Some(position);
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2209,7 +2209,7 @@ impl Http {
&self,
guild_id: GuildId,
role_id: RoleId,
position: u16,
position: i16,
audit_log_reason: Option<&str>,
) -> Result<Vec<Role>> {
let map = json!([{
Expand Down
2 changes: 1 addition & 1 deletion src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ impl GuildId {
self,
http: impl AsRef<Http>,
role_id: RoleId,
position: u16,
position: i16,
) -> Result<Vec<Role>> {
http.as_ref().edit_role_position(self, role_id, position, None).await
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/guild/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl Member {
/// more roles have the same highest position, then the role with the lowest ID is the highest.
#[cfg(feature = "cache")]
#[deprecated = "Use Guild::member_highest_role"]
pub fn highest_role_info(&self, cache: impl AsRef<Cache>) -> Option<(RoleId, u16)> {
pub fn highest_role_info(&self, cache: impl AsRef<Cache>) -> Option<(RoleId, i16)> {
cache
.as_ref()
.guild(self.guild_id)
Expand Down
2 changes: 1 addition & 1 deletion src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ impl Guild {
&self,
http: impl AsRef<Http>,
role_id: RoleId,
position: u16,
position: i16,
) -> Result<Vec<Role>> {
self.id.edit_role_position(http, role_id, position).await
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/guild/partial_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ impl PartialGuild {
&self,
http: impl AsRef<Http>,
role_id: RoleId,
position: u16,
position: i16,
) -> Result<Vec<Role>> {
self.id.edit_role_position(http, role_id, position).await
}
Expand Down
7 changes: 1 addition & 6 deletions src/model/guild/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ use crate::internal::prelude::*;
use crate::model::prelude::*;
use crate::model::utils::is_false;

fn minus1_as_0<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<u16, D::Error> {
i16::deserialize(deserializer).map(|val| if val == -1 { 0 } else { val as u16 })
}

/// Information about a role within a guild.
///
/// A role represents a set of permissions, and can be attached to one or multiple users. A role has
Expand Down Expand Up @@ -59,8 +55,7 @@ pub struct Role {
/// position is higher.
///
/// The `@everyone` role is usually either `-1` or `0`.
#[serde(deserialize_with = "minus1_as_0")]
pub position: u16,
pub position: i16,
/// The tags this role has. It can be used to determine if this role is a special role in this
/// guild such as guild subscriber role, or if the role is linked to an [`Integration`] or a
/// bot.
Expand Down

0 comments on commit d6d5237

Please sign in to comment.