[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>
This commit is contained in:
zobweyt
2023-11-18 23:50:17 +03:00
committed by GitHub
parent ac274d4b76
commit b1787d8334
10 changed files with 48 additions and 69 deletions

View File

@@ -133,35 +133,21 @@ namespace Discord
/// specified channel. /// specified channel.
/// </returns> /// </returns>
ChannelPermissions GetPermissions(IGuildChannel channel); ChannelPermissions GetPermissions(IGuildChannel channel);
/// <summary> /// <summary>
/// Gets the guild avatar URL for this user. /// Gets the guild-specific avatar URL for this user, if it is set.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// 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 /// <note type="tip">
/// (i.e. their avatar identifier is not set), this method will return <see langword="null"/>. /// If you wish to retrieve the display avatar for this user, consider using <see cref="IUser.GetDisplayAvatarUrl"/>.
/// </note>
/// </remarks> /// </remarks>
/// <param name="format">The format to return.</param> /// <param name="format">The format of the image.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048. /// <param name="size">The size of the image that matches any power of two, ranging from 16 to 2048.</param>
/// </param>
/// <returns> /// <returns>
/// A string representing the user's avatar URL; <see langword="null"/> if the user does not have an avatar in place. /// A string representing the user's guild-specific avatar URL; <see langword="null"/> if the user has no guild avatar set.
/// </returns> /// </returns>
string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128); string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
/// <summary> /// <summary>
/// Gets the display avatar URL for this user.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048.</param>
/// <returns>
/// A string representing the URL of the displayed avatar for this user. <see langword="null"/> if the user does not have an avatar in place.
/// </returns>
string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
/// <summary>
/// Kicks this user from this guild. /// Kicks this user from this guild.
/// </summary> /// </summary>
/// <param name="reason">The reason for the kick which will be recorded in the audit log.</param> /// <param name="reason">The reason for the kick which will be recorded in the audit log.</param>

View File

@@ -12,41 +12,45 @@ namespace Discord
/// </summary> /// </summary>
string AvatarId { get; } string AvatarId { get; }
/// <summary> /// <summary>
/// Gets the avatar URL for this user. /// Gets the avatar URL for this user, if it is set.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This property retrieves a URL for this user's avatar. In event that the user does not have a valid avatar /// <note type="tip">
/// (i.e. their avatar identifier is not set), this method will return <see langword="null" />. If you wish to /// If you wish to retrieve the display avatar for this user, consider using <see cref="GetDisplayAvatarUrl"/>.
/// retrieve the default avatar for this user, consider using <see cref="IUser.GetDefaultAvatarUrl"/> (see /// </note>
/// example).
/// </remarks> /// </remarks>
/// <example> /// <param name="format">The format of the image.</param>
/// <para /// <param name="size">The size of the image that matches any power of two, ranging from 16 to 2048.</param>
/// >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.</para>
/// <code language="cs" region="GetAvatarUrl"
/// source="..\..\..\Discord.Net.Examples\Core\Entities\Users\IUser.Examples.cs"/>
/// </example>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048.
/// </param>
/// <returns> /// <returns>
/// A string representing the user's avatar URL; <see langword="null" /> if the user does not have an avatar in place. /// A string representing the user's avatar URL; <see langword="null" /> if the user has no avatar set.
/// </returns> /// </returns>
string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128); string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
/// <summary> /// <summary>
/// Gets the default avatar URL for this user. /// Gets the default avatar URL for this user.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This property retrieves a URL for this user's default avatar generated by Discord (Discord logo followed /// This avatar is auto-generated by Discord and consists of their logo combined with a random background color.
/// by a random color as its background). This property will always return a value as it is calculated based /// <note type="note">
/// on the user's <see cref="IUser.DiscriminatorValue"/> (<c>discriminator % 5</c>). /// The calculation is always done by taking the remainder of this user's <see cref="DiscriminatorValue"/> divided by 5.
/// </note>
/// </remarks> /// </remarks>
/// <returns> /// <returns>
/// A string representing the user's avatar URL. /// A string representing the user's default avatar URL.
/// </returns> /// </returns>
string GetDefaultAvatarUrl(); string GetDefaultAvatarUrl();
/// <summary> /// <summary>
/// Gets the display avatar URL for this user.
/// </summary>
/// <remarks>
/// This method will return <see cref="GetDefaultAvatarUrl" /> if the user has no avatar set.
/// </remarks>
/// <param name="format">The format of the image.</param>
/// <param name="size">The size of the image that matches any power of two, ranging from 16 to 2048.</param>
/// <returns>
/// A string representing the user's display avatar URL.
/// </returns>
string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128);
/// <summary>
/// Gets the per-username unique ID for this user. This will return "0000" for users who have migrated to new username system. /// Gets the per-username unique ID for this user. This will return "0000" for users who have migrated to new username system.
/// </summary> /// </summary>
string Discriminator { get; } string Discriminator { get; }

View File

@@ -8,16 +8,6 @@ namespace Discord.Net.Examples.Core.Entities.Users
[PublicAPI] [PublicAPI]
internal class UserExamples 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 #region CreateDMChannelAsync
public async Task MessageUserAsync(IUser user) public async Task MessageUserAsync(IUser user)

View File

@@ -194,15 +194,13 @@ namespace Discord.Rest
return new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, guildPerms.RawValue)); return new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, guildPerms.RawValue));
} }
/// <inheritdoc />
public string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> GuildAvatarId is not null
? GetGuildAvatarUrl(format, size)
: GetAvatarUrl(format, size);
/// <inheritdoc /> /// <inheritdoc />
public string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) public string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetGuildUserAvatarUrl(Id, GuildId, GuildAvatarId, size, format); => CDN.GetGuildUserAvatarUrl(Id, GuildId, GuildAvatarId, size, format);
/// <inheritdoc />
public override string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> GetGuildAvatarUrl(format, size) ?? base.GetDisplayAvatarUrl(format, size);
#endregion #endregion
#region IGuildUser #region IGuildUser

View File

@@ -142,6 +142,9 @@ namespace Discord.Rest
? CDN.GetDefaultUserAvatarUrl(DiscriminatorValue) ? CDN.GetDefaultUserAvatarUrl(DiscriminatorValue)
: CDN.GetDefaultUserAvatarUrl(Id); : CDN.GetDefaultUserAvatarUrl(Id);
public virtual string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> GetAvatarUrl(format, size) ?? GetDefaultAvatarUrl();
/// <inheritdoc /> /// <inheritdoc />
public string GetAvatarDecorationUrl() public string GetAvatarDecorationUrl()
=> AvatarDecorationHash is not null => AvatarDecorationHash is not null

View File

@@ -60,8 +60,6 @@ namespace Discord.Rest
/// <inheritdoc /> /// <inheritdoc />
string IGuildUser.GuildAvatarId => null; string IGuildUser.GuildAvatarId => null;
/// <inheritdoc /> /// <inheritdoc />
string IGuildUser.GetDisplayAvatarUrl(ImageFormat format, ushort size) => null;
/// <inheritdoc />
string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => null; string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => null;
/// <inheritdoc /> /// <inheritdoc />
bool? IGuildUser.IsPending => null; bool? IGuildUser.IsPending => null;

View File

@@ -259,16 +259,14 @@ namespace Discord.WebSocket
public ChannelPermissions GetPermissions(IGuildChannel channel) public ChannelPermissions GetPermissions(IGuildChannel channel)
=> new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, GuildPermissions.RawValue)); => new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, GuildPermissions.RawValue));
/// <inheritdoc />
public string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> GuildAvatarId is not null
? GetGuildAvatarUrl(format, size)
: GetAvatarUrl(format, size);
/// <inheritdoc /> /// <inheritdoc />
public string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) public string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetGuildUserAvatarUrl(Id, Guild.Id, GuildAvatarId, size, format); => CDN.GetGuildUserAvatarUrl(Id, Guild.Id, GuildAvatarId, size, format);
/// <inheritdoc />
public override string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> GetGuildAvatarUrl(format, size) ?? base.GetDisplayAvatarUrl(format, size);
private string DebuggerDisplay => DiscriminatorValue != 0 private string DebuggerDisplay => DiscriminatorValue != 0
? $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")}, Guild)" ? $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")}, Guild)"
: $"{Username} ({Id}{(IsBot ? ", Bot" : "")}, Guild)"; : $"{Username} ({Id}{(IsBot ? ", Bot" : "")}, Guild)";

View File

@@ -238,10 +238,11 @@ namespace Discord.WebSocket
IReadOnlyCollection<ulong> IGuildUser.RoleIds => GuildUser.Roles.Select(x => x.Id).ToImmutableArray(); IReadOnlyCollection<ulong> IGuildUser.RoleIds => GuildUser.Roles.Select(x => x.Id).ToImmutableArray();
/// <inheritdoc /> /// <inheritdoc />
string IGuildUser.GetDisplayAvatarUrl(ImageFormat format, ushort size) => GuildUser.GetDisplayAvatarUrl(format, size); string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => GuildUser.GetGuildAvatarUrl(format, size);
/// <inheritdoc /> /// <inheritdoc />
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; } internal override SocketGlobalUser GlobalUser { get => GuildUser.GlobalUser; set => GuildUser.GlobalUser = value; }

View File

@@ -137,6 +137,9 @@ namespace Discord.WebSocket
: CDN.GetDefaultUserAvatarUrl(Id); : CDN.GetDefaultUserAvatarUrl(Id);
/// <inheritdoc /> /// <inheritdoc />
public virtual string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> GetAvatarUrl(format, size) ?? GetDefaultAvatarUrl();
public string GetAvatarDecorationUrl() public string GetAvatarDecorationUrl()
=> AvatarDecorationHash is not null => AvatarDecorationHash is not null
? CDN.GetAvatarDecorationUrl(AvatarDecorationHash) ? CDN.GetAvatarDecorationUrl(AvatarDecorationHash)

View File

@@ -75,8 +75,6 @@ namespace Discord.WebSocket
/// <inheritdoc /> /// <inheritdoc />
string IGuildUser.GuildAvatarId => null; string IGuildUser.GuildAvatarId => null;
/// <inheritdoc /> /// <inheritdoc />
string IGuildUser.GetDisplayAvatarUrl(ImageFormat format, ushort size) => null;
/// <inheritdoc />
string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => null; string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => null;
/// <inheritdoc /> /// <inheritdoc />
DateTimeOffset? IGuildUser.PremiumSince => null; DateTimeOffset? IGuildUser.PremiumSince => null;