Skip to content

Commit d11970f

Browse files
authored
Add banner field to Member and PartialMember (#3368)
1 parent 2eed823 commit d11970f

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

src/cache/event.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ impl CacheUpdate for GuildMemberUpdateEvent {
205205
member.deaf.clone_from(&self.deaf);
206206
member.mute.clone_from(&self.mute);
207207
member.avatar.clone_from(&self.avatar);
208+
member.banner.clone_from(&self.banner);
208209
member.communication_disabled_until.clone_from(&self.communication_disabled_until);
209210
member.unusual_dm_activity_until.clone_from(&self.unusual_dm_activity_until);
210211

@@ -226,6 +227,7 @@ impl CacheUpdate for GuildMemberUpdateEvent {
226227
premium_since: self.premium_since,
227228
permissions: None,
228229
avatar: self.avatar,
230+
banner: self.banner,
229231
communication_disabled_until: self.communication_disabled_until,
230232
flags: GuildMemberFlags::default(),
231233
unusual_dm_activity_until: self.unusual_dm_activity_until,
@@ -460,6 +462,7 @@ impl CacheUpdate for PresenceUpdateEvent {
460462
premium_since: None,
461463
permissions: None,
462464
avatar: None,
465+
banner: None,
463466
communication_disabled_until: None,
464467
flags: GuildMemberFlags::default(),
465468
unusual_dm_activity_until: None,

src/model/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ pub struct GuildMemberUpdateEvent {
260260
#[serde(default)]
261261
pub mute: bool,
262262
pub avatar: Option<ImageHash>,
263+
pub banner: Option<ImageHash>,
263264
pub communication_disabled_until: Option<Timestamp>,
264265
pub unusual_dm_activity_until: Option<Timestamp>,
265266
}

src/model/guild/member.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::http::{CacheHttp, Http};
1212
use crate::internal::prelude::*;
1313
use crate::model::prelude::*;
1414
#[cfg(feature = "model")]
15-
use crate::model::utils::avatar_url;
15+
use crate::model::utils::{avatar_url, user_banner_url};
1616

1717
/// Information about a member of a guild.
1818
///
@@ -28,8 +28,10 @@ pub struct Member {
2828
///
2929
/// Can't be longer than 32 characters.
3030
pub nick: Option<String>,
31-
/// The guild avatar hash
31+
/// The member's guild avatar hash
3232
pub avatar: Option<ImageHash>,
33+
/// The member's guild banner hash
34+
pub banner: Option<ImageHash>,
3335
/// Vector of Ids of [`Role`]s given to the member.
3436
pub roles: Vec<RoleId>,
3537
/// Timestamp representing the date when the member joined.
@@ -530,6 +532,14 @@ impl Member {
530532
avatar_url(Some(self.guild_id), self.user.id, self.avatar.as_ref())
531533
}
532534

535+
/// Returns the formatted URL of the member's per guild banner, if one exists.
536+
///
537+
/// This will produce a WEBP image URL, or GIF if the member has a GIF avatar.
538+
#[must_use]
539+
pub fn banner_url(&self) -> Option<String> {
540+
user_banner_url(Some(self.guild_id), self.user.id, self.banner.as_ref())
541+
}
542+
533543
/// Retrieves the URL to the current member's avatar, falling back to the user's avatar, then
534544
/// default avatar if needed.
535545
///
@@ -609,8 +619,10 @@ pub struct PartialMember {
609619
///
610620
/// Will be None or a time in the past if the user is not flagged.
611621
pub unusual_dm_activity_until: Option<Timestamp>,
612-
/// The guild avatar hash
622+
/// The member's guild avatar hash
613623
pub avatar: Option<ImageHash>,
624+
/// The member's guild banner hash
625+
pub banner: Option<ImageHash>,
614626
}
615627

616628
impl From<PartialMember> for Member {
@@ -619,6 +631,7 @@ impl From<PartialMember> for Member {
619631
user: partial.user.unwrap_or_default(),
620632
nick: partial.nick,
621633
avatar: partial.avatar,
634+
banner: partial.banner,
622635
roles: partial.roles,
623636
joined_at: partial.joined_at,
624637
premium_since: partial.premium_since,
@@ -649,6 +662,7 @@ impl From<Member> for PartialMember {
649662
permissions: member.permissions,
650663
unusual_dm_activity_until: member.unusual_dm_activity_until,
651664
avatar: member.avatar,
665+
banner: member.banner,
652666
}
653667
}
654668
}

src/model/user.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::internal::prelude::*;
2424
#[cfg(feature = "model")]
2525
use crate::json::json;
2626
#[cfg(feature = "model")]
27-
use crate::model::utils::avatar_url;
27+
use crate::model::utils::{avatar_url, user_banner_url};
2828

2929
/// Used with `#[serde(with|deserialize_with|serialize_with)]`
3030
///
@@ -385,13 +385,10 @@ impl User {
385385
/// Returns the formatted URL of the user's banner, if one exists.
386386
///
387387
/// This will produce a WEBP image URL, or GIF if the user has a GIF banner.
388-
///
389-
/// **Note**: This will only be present if the user is fetched via Rest API, e.g. with
390-
/// [`crate::http::Http::get_user`].
391388
#[inline]
392389
#[must_use]
393390
pub fn banner_url(&self) -> Option<String> {
394-
banner_url(self.id, self.banner.as_ref())
391+
user_banner_url(None, self.id, self.banner.as_ref())
395392
}
396393

397394
/// Creates a direct message channel between the [current user] and the user. This can also
@@ -813,14 +810,6 @@ fn static_avatar_url(user_id: UserId, hash: Option<&ImageHash>) -> Option<String
813810
hash.map(|hash| cdn!("/avatars/{}/{}.webp?size=1024", user_id, hash))
814811
}
815812

816-
#[cfg(feature = "model")]
817-
fn banner_url(user_id: UserId, hash: Option<&ImageHash>) -> Option<String> {
818-
hash.map(|hash| {
819-
let ext = if hash.is_animated() { "gif" } else { "webp" };
820-
cdn!("/banners/{}/{}.{}?size=1024", user_id, hash, ext)
821-
})
822-
}
823-
824813
#[cfg(feature = "model")]
825814
fn tag(name: &str, discriminator: Option<NonZeroU16>) -> String {
826815
// 32: max length of username

src/model/utils.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ pub(super) fn avatar_url(
3636
})
3737
}
3838

39+
#[cfg(feature = "model")]
40+
pub(super) fn user_banner_url(
41+
guild_id: Option<GuildId>,
42+
user_id: UserId,
43+
hash: Option<&ImageHash>,
44+
) -> Option<String> {
45+
hash.map(|hash| {
46+
let ext = if hash.is_animated() { "gif" } else { "webp" };
47+
48+
if let Some(guild_id) = guild_id {
49+
cdn!("/guilds/{}/users/{}/banners/{}.{}?size=1024", guild_id, user_id, hash, ext)
50+
} else {
51+
cdn!("/banners/{}/{}.{}?size=1024", user_id, hash, ext)
52+
}
53+
})
54+
}
55+
3956
#[cfg(feature = "model")]
4057
pub(super) fn icon_url(id: GuildId, icon: Option<&ImageHash>) -> Option<String> {
4158
icon.map(|icon| {

0 commit comments

Comments
 (0)