From b1787d833478f2022d369db3cbaa6ec4777ba407 Mon Sep 17 00:00:00 2001
From: zobweyt <98274273+zobweyt@users.noreply.github.com>
Date: Sat, 18 Nov 2023 23:50:17 +0300
Subject: [PATCH] [Refactor] Extract `GetDisplayAvatarUrl` in `IUser` (#2771)
* Implement `GetDisplayAvatarUrl` method
* Remove obsolete example
* Update XML documentation
---------
Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>
---
.../Entities/Users/IGuildUser.cs | 28 +++---------
src/Discord.Net.Core/Entities/Users/IUser.cs | 44 ++++++++++---------
.../Core/Entities/Users/IUser.Examples.cs | 10 -----
.../Entities/Users/RestGuildUser.cs | 10 ++---
.../Entities/Users/RestUser.cs | 3 ++
.../Entities/Users/RestWebhookUser.cs | 2 -
.../Entities/Users/SocketGuildUser.cs | 10 ++---
.../Entities/Users/SocketThreadUser.cs | 5 ++-
.../Entities/Users/SocketUser.cs | 3 ++
.../Entities/Users/SocketWebhookUser.cs | 2 -
10 files changed, 48 insertions(+), 69 deletions(-)
diff --git a/src/Discord.Net.Core/Entities/Users/IGuildUser.cs b/src/Discord.Net.Core/Entities/Users/IGuildUser.cs
index 814a5234..7e17c7cb 100644
--- a/src/Discord.Net.Core/Entities/Users/IGuildUser.cs
+++ b/src/Discord.Net.Core/Entities/Users/IGuildUser.cs
@@ -133,35 +133,21 @@ namespace Discord
/// specified channel.
///
ChannelPermissions GetPermissions(IGuildChannel channel);
-
///
- /// Gets the guild avatar URL for this user.
+ /// Gets the guild-specific avatar URL for this user, if it is set.
///
///
- /// This property retrieves a URL for this guild user's guild specific avatar. In event that the user does not have a valid guild avatar
- /// (i.e. their avatar identifier is not set), this method will return .
+ ///
+ /// If you wish to retrieve the display avatar for this user, consider using .
+ ///
///
- /// The format to return.
- /// The size of the image to return in. This can be any power of two between 16 and 2048.
- ///
+ /// The format of the image.
+ /// The size of the image that matches any power of two, ranging from 16 to 2048.
///
- /// A string representing the user's avatar URL; if the user does not have an avatar in place.
+ /// A string representing the user's guild-specific avatar URL; if the user has no guild avatar set.
///
string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
///
- /// Gets the display avatar URL for this user.
- ///
- ///
- /// This property retrieves an URL for this guild user's displayed avatar.
- /// If the user does not have a guild avatar, this will be the user's regular avatar.
- ///
- /// The format to return.
- /// The size of the image to return in. This can be any power of two between 16 and 2048.
- ///
- /// A string representing the URL of the displayed avatar for this user. if the user does not have an avatar in place.
- ///
- string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
- ///
/// Kicks this user from this guild.
///
/// The reason for the kick which will be recorded in the audit log.
diff --git a/src/Discord.Net.Core/Entities/Users/IUser.cs b/src/Discord.Net.Core/Entities/Users/IUser.cs
index 9c3dfb07..19e1b6d4 100644
--- a/src/Discord.Net.Core/Entities/Users/IUser.cs
+++ b/src/Discord.Net.Core/Entities/Users/IUser.cs
@@ -12,41 +12,45 @@ namespace Discord
///
string AvatarId { get; }
///
- /// Gets the avatar URL for this user.
+ /// Gets the avatar URL for this user, if it is set.
///
///
- /// This property retrieves a URL for this user's avatar. In event that the user does not have a valid avatar
- /// (i.e. their avatar identifier is not set), this method will return . If you wish to
- /// retrieve the default avatar for this user, consider using (see
- /// example).
+ ///
+ /// If you wish to retrieve the display avatar for this user, consider using .
+ ///
///
- ///
- /// The following example attempts to retrieve the user's current avatar and send it to a channel; if one is
- /// not set, a default avatar for this user will be returned instead.
- ///
- ///
- /// The format to return.
- /// The size of the image to return in. This can be any power of two between 16 and 2048.
- ///
+ /// The format of the image.
+ /// The size of the image that matches any power of two, ranging from 16 to 2048.
///
- /// A string representing the user's avatar URL; if the user does not have an avatar in place.
+ /// A string representing the user's avatar URL; if the user has no avatar set.
///
string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
///
/// Gets the default avatar URL for this user.
///
///
- /// This property retrieves a URL for this user's default avatar generated by Discord (Discord logo followed
- /// by a random color as its background). This property will always return a value as it is calculated based
- /// on the user's (discriminator % 5).
+ /// This avatar is auto-generated by Discord and consists of their logo combined with a random background color.
+ ///
+ /// The calculation is always done by taking the remainder of this user's divided by 5.
+ ///
///
///
- /// A string representing the user's avatar URL.
+ /// A string representing the user's default avatar URL.
///
string GetDefaultAvatarUrl();
///
+ /// Gets the display avatar URL for this user.
+ ///
+ ///
+ /// This method will return if the user has no avatar set.
+ ///
+ /// The format of the image.
+ /// The size of the image that matches any power of two, ranging from 16 to 2048.
+ ///
+ /// A string representing the user's display avatar URL.
+ ///
+ string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
+ ///
/// Gets the per-username unique ID for this user. This will return "0000" for users who have migrated to new username system.
///
string Discriminator { get; }
diff --git a/src/Discord.Net.Examples/Core/Entities/Users/IUser.Examples.cs b/src/Discord.Net.Examples/Core/Entities/Users/IUser.Examples.cs
index 5a671290..052bb381 100644
--- a/src/Discord.Net.Examples/Core/Entities/Users/IUser.Examples.cs
+++ b/src/Discord.Net.Examples/Core/Entities/Users/IUser.Examples.cs
@@ -8,16 +8,6 @@ namespace Discord.Net.Examples.Core.Entities.Users
[PublicAPI]
internal class UserExamples
{
- #region GetAvatarUrl
-
- public async Task GetAvatarAsync(IUser user, ITextChannel textChannel)
- {
- var userAvatarUrl = user.GetAvatarUrl() ?? user.GetDefaultAvatarUrl();
- await textChannel.SendMessageAsync(userAvatarUrl);
- }
-
- #endregion
-
#region CreateDMChannelAsync
public async Task MessageUserAsync(IUser user)
diff --git a/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs b/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs
index e6f14134..201cdbe6 100644
--- a/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs
+++ b/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs
@@ -194,15 +194,13 @@ namespace Discord.Rest
return new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, guildPerms.RawValue));
}
- ///
- public string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
- => GuildAvatarId is not null
- ? GetGuildAvatarUrl(format, size)
- : GetAvatarUrl(format, size);
-
///
public string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetGuildUserAvatarUrl(Id, GuildId, GuildAvatarId, size, format);
+
+ ///
+ public override string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
+ => GetGuildAvatarUrl(format, size) ?? base.GetDisplayAvatarUrl(format, size);
#endregion
#region IGuildUser
diff --git a/src/Discord.Net.Rest/Entities/Users/RestUser.cs b/src/Discord.Net.Rest/Entities/Users/RestUser.cs
index 43541c0a..e9da7087 100644
--- a/src/Discord.Net.Rest/Entities/Users/RestUser.cs
+++ b/src/Discord.Net.Rest/Entities/Users/RestUser.cs
@@ -142,6 +142,9 @@ namespace Discord.Rest
? CDN.GetDefaultUserAvatarUrl(DiscriminatorValue)
: CDN.GetDefaultUserAvatarUrl(Id);
+ public virtual string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
+ => GetAvatarUrl(format, size) ?? GetDefaultAvatarUrl();
+
///
public string GetAvatarDecorationUrl()
=> AvatarDecorationHash is not null
diff --git a/src/Discord.Net.Rest/Entities/Users/RestWebhookUser.cs b/src/Discord.Net.Rest/Entities/Users/RestWebhookUser.cs
index 268faf00..ba3501d7 100644
--- a/src/Discord.Net.Rest/Entities/Users/RestWebhookUser.cs
+++ b/src/Discord.Net.Rest/Entities/Users/RestWebhookUser.cs
@@ -60,8 +60,6 @@ namespace Discord.Rest
///
string IGuildUser.GuildAvatarId => null;
///
- string IGuildUser.GetDisplayAvatarUrl(ImageFormat format, ushort size) => null;
- ///
string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => null;
///
bool? IGuildUser.IsPending => null;
diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs
index 68a3f6ea..f34fd2fb 100644
--- a/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs
+++ b/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs
@@ -259,16 +259,14 @@ namespace Discord.WebSocket
public ChannelPermissions GetPermissions(IGuildChannel channel)
=> new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, GuildPermissions.RawValue));
- ///
- public string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
- => GuildAvatarId is not null
- ? GetGuildAvatarUrl(format, size)
- : GetAvatarUrl(format, size);
-
///
public string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetGuildUserAvatarUrl(Id, Guild.Id, GuildAvatarId, size, format);
+ ///
+ public override string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
+ => GetGuildAvatarUrl(format, size) ?? base.GetDisplayAvatarUrl(format, size);
+
private string DebuggerDisplay => DiscriminatorValue != 0
? $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")}, Guild)"
: $"{Username} ({Id}{(IsBot ? ", Bot" : "")}, Guild)";
diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs
index 30a2267c..429dc527 100644
--- a/src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs
+++ b/src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs
@@ -238,10 +238,11 @@ namespace Discord.WebSocket
IReadOnlyCollection IGuildUser.RoleIds => GuildUser.Roles.Select(x => x.Id).ToImmutableArray();
///
- string IGuildUser.GetDisplayAvatarUrl(ImageFormat format, ushort size) => GuildUser.GetDisplayAvatarUrl(format, size);
+ string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => GuildUser.GetGuildAvatarUrl(format, size);
///
- string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => GuildUser.GetGuildAvatarUrl(format, size);
+ public override string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
+ => GuildUser.GetGuildAvatarUrl() ?? base.GetDisplayAvatarUrl(format, size);
internal override SocketGlobalUser GlobalUser { get => GuildUser.GlobalUser; set => GuildUser.GlobalUser = value; }
diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs
index 54b0cbff..5705f666 100644
--- a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs
+++ b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs
@@ -137,6 +137,9 @@ namespace Discord.WebSocket
: CDN.GetDefaultUserAvatarUrl(Id);
///
+ public virtual string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
+ => GetAvatarUrl(format, size) ?? GetDefaultAvatarUrl();
+
public string GetAvatarDecorationUrl()
=> AvatarDecorationHash is not null
? CDN.GetAvatarDecorationUrl(AvatarDecorationHash)
diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketWebhookUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketWebhookUser.cs
index d76d9949..2c438e6c 100644
--- a/src/Discord.Net.WebSocket/Entities/Users/SocketWebhookUser.cs
+++ b/src/Discord.Net.WebSocket/Entities/Users/SocketWebhookUser.cs
@@ -75,8 +75,6 @@ namespace Discord.WebSocket
///
string IGuildUser.GuildAvatarId => null;
///
- string IGuildUser.GetDisplayAvatarUrl(ImageFormat format, ushort size) => null;
- ///
string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => null;
///
DateTimeOffset? IGuildUser.PremiumSince => null;