Skip to content

Commit

Permalink
Fix GuildChannel deserialisation with user_limit (serenity-rs#2715)
Browse files Browse the repository at this point in the history
This is documented as max 99 for voice channels, but 10k for stage
channels.
  • Loading branch information
GnomedDev committed Apr 1, 2024
1 parent 377ff7e commit d2ab3f3
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/builder/create_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct CreateChannel<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
bitrate: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
user_limit: Option<u32>,
user_limit: Option<NonMaxU16>,
#[serde(skip_serializing_if = "Option::is_none")]
rate_limit_per_user: Option<NonMaxU16>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<'a> CreateChannel<'a> {
/// Set how many users may occupy this voice channel
///
/// Only for [`ChannelType::Voice`] and [`ChannelType::Stage`]
pub fn user_limit(mut self, limit: u32) -> Self {
pub fn user_limit(mut self, limit: NonMaxU16) -> Self {
self.user_limit = Some(limit);
self
}
Expand Down
7 changes: 4 additions & 3 deletions src/builder/edit_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct EditChannel<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
bitrate: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
user_limit: Option<u32>,
user_limit: Option<NonMaxU16>,
#[serde(skip_serializing_if = "Option::is_none")]
permission_overwrites: Option<Cow<'a, [PermissionOverwrite]>>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -157,10 +157,11 @@ impl<'a> EditChannel<'a> {

/// The number of users that may be in the channel simultaneously.
///
/// This is for [voice] channels only.
/// This is for [voice] and [stage] channels only.
///
/// [voice]: ChannelType::Voice
pub fn user_limit(mut self, user_limit: u32) -> Self {
/// [stage]: ChannelType::Stage
pub fn user_limit(mut self, user_limit: NonMaxU16) -> Self {
self.user_limit = Some(user_limit);
self
}
Expand Down
5 changes: 3 additions & 2 deletions src/gateway/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ impl WsClient {

from_str(&decompressed).map_err(|why| {
warn!("Err deserializing bytes: {why:?}");
debug!("Failing bytes: {bytes:?}");
debug!("Failing text: {decompressed}");

why
})?
},
Message::Text(payload) => from_str(&payload).map_err(|why| {
warn!("Err deserializing text: {why:?}; text: {payload}");
warn!("Err deserializing text: {why:?}");
debug!("Failing text: {payload}");

why
})?,
Expand Down
4 changes: 2 additions & 2 deletions src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ pub struct GuildChannel {
pub topic: Option<FixedString<u16>>,
/// The maximum number of members allowed in the channel.
///
/// **Note**: This is only available for voice channels.
pub user_limit: Option<NonMaxU8>,
/// This is max 99 for voice channels and 10,000 for stage channels (0 refers to no limit).
pub user_limit: Option<NonMaxU16>,
/// Used to tell if the channel is not safe for work. Note however, it's recommended to use
/// [`Self::is_nsfw`] as it's gonna be more accurate.
// This field can or can not be present sometimes, but if it isn't default to `false`.
Expand Down
4 changes: 3 additions & 1 deletion src/model/guild/audit_log/change.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use nonmax::NonMaxU16;

use crate::internal::prelude::*;
use crate::json::Value;
use crate::model::channel::PermissionOverwrite;
Expand Down Expand Up @@ -254,7 +256,7 @@ generate_change! {
/// Unicode emoji of a role icon was changed.
"unicode_emoji" => UnicodeEmoji(FixedString),
/// Maximum number of users in a voice channel was changed.
"user_limit" => UserLimit(u64),
"user_limit" => UserLimit(NonMaxU16),
/// Number of uses of an invite was changed.
"uses" => Uses(u64),
/// Guild invite vanity url was changed.
Expand Down
21 changes: 1 addition & 20 deletions src/model/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,7 @@ pub mod emojis {
pub fn deserialize_guild_channels<'de, D: Deserializer<'de>>(
deserializer: D,
) -> StdResult<HashMap<ChannelId, GuildChannel>, D::Error> {
struct TryDeserialize<T>(StdResult<T, String>);
impl<'de, T: Deserialize<'de>> Deserialize<'de> for TryDeserialize<T> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
Ok(Self(T::deserialize(deserializer).map_err(|e| e.to_string())))
}
}

let vec: Vec<TryDeserialize<GuildChannel>> = Deserialize::deserialize(deserializer)?;
let mut map = HashMap::new();

for channel in vec {
match channel.0 {
Ok(channel) => {
map.insert(channel.id, channel);
},
Err(e) => tracing::warn!("skipping guild channel due to deserialization error: {}", e),
}
}

Ok(map)
deserializer.deserialize_seq(SequenceToMapVisitor::new(|channel: &GuildChannel| channel.id))
}

/// Used with `#[serde(with = "members")]
Expand Down

0 comments on commit d2ab3f3

Please sign in to comment.