Skip to content

Commit

Permalink
[Feature] Avatar decorations support (#2782)
Browse files Browse the repository at this point in the history
* yup

* nullable...

* add props & methos to `GuildUser` & `IUser`
  • Loading branch information
Misha-133 committed Nov 18, 2023
1 parent b45b774 commit f64d9d6
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Discord.Net.Core/CDN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,15 @@ private static string FormatToExtension(ImageFormat format, string imageId)
_ => throw new ArgumentException(nameof(format)),
};
}

/// <summary>
/// Gets an avatar decoration url based off the hash.
/// </summary>
/// <param name="avatarDecorationHash">The hash of the avatar decoraition.</param>
/// <returns>
/// A URL to the avatar decoration.
/// </returns>
public static string GetAvatarDecorationUrl(string avatarDecorationHash)
=> $"{DiscordConfig.CDNUrl}avatar-decoration-presets/{avatarDecorationHash}.png";
}
}
25 changes: 25 additions & 0 deletions src/Discord.Net.Core/Entities/Users/IUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public interface IUser : ISnowflakeEntity, IMentionable, IPresence
/// </remarks>
string GlobalName { get; }

/// <summary>
/// Gets the hash of the avatar decoration.
/// </summary>
/// <remarks>
/// <see langword="null"/> if the user has no avatar decoration set.
/// </remarks>
string AvatarDecorationHash { get; }

/// <summary>
/// Gets the id of the avatar decoration's SKU.
/// </summary>
/// <remarks>
/// <see langword="null"/> if the user has no avatar decoration set.
/// </remarks>
ulong? AvatarDecorationSkuId { get; }

/// <summary>
/// Creates the direct message channel of this user.
/// </summary>
Expand All @@ -120,5 +136,14 @@ public interface IUser : ISnowflakeEntity, IMentionable, IPresence
/// contains the DM channel associated with this user.
/// </returns>
Task<IDMChannel> CreateDMChannelAsync(RequestOptions options = null);


/// <summary>
/// Gets the URL for user's avatar decoration.
/// </summary>
/// <remarks>
/// <see langword="null"/> if the user has no avatar decoration set.
/// </remarks>
string GetAvatarDecorationUrl();
}
}
12 changes: 12 additions & 0 deletions src/Discord.Net.Rest/API/Common/AvatarDecorationData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Discord.API;

internal class AvatarDecorationData
{
[JsonProperty("asset")]
public string Asset { get; set; }

[JsonProperty("sku_id")]
public ulong SkuId { get; set; }
}
3 changes: 3 additions & 0 deletions src/Discord.Net.Rest/API/Common/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ internal class User
[JsonProperty("global_name")]
public Optional<string> GlobalName { get; set; }

[JsonProperty("avatar_decoration_data")]
public Optional<AvatarDecorationData> AvatarDecoration { get; set; }

//CurrentUser
[JsonProperty("verified")]
public Optional<bool> Verified { get; set; }
Expand Down
19 changes: 19 additions & 0 deletions src/Discord.Net.Rest/Entities/Users/RestUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;

using EventUserModel = Discord.API.GuildScheduledEventUser;
using Model = Discord.API.User;

Expand Down Expand Up @@ -50,6 +51,13 @@ public class RestUser : RestEntity<ulong>, IUser, IUpdateable
/// <inheritdoc />
public virtual bool IsWebhook => false;

/// <inheritdoc />
public string AvatarDecorationHash { get; private set; }

/// <inheritdoc />
public ulong? AvatarDecorationSkuId { get; private set; }


internal RestUser(BaseDiscordClient discord, ulong id)
: base(discord, id)
{
Expand Down Expand Up @@ -96,6 +104,11 @@ internal virtual void Update(Model model)
PublicFlags = model.PublicFlags.Value;
if (model.GlobalName.IsSpecified)
GlobalName = model.GlobalName.Value;
if (model.AvatarDecoration is { IsSpecified: true, Value: not null })
{
AvatarDecorationHash = model.AvatarDecoration.Value?.Asset;
AvatarDecorationSkuId = model.AvatarDecoration.Value?.SkuId;
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -129,6 +142,12 @@ public string GetDefaultAvatarUrl()
? CDN.GetDefaultUserAvatarUrl(DiscriminatorValue)
: CDN.GetDefaultUserAvatarUrl(Id);

/// <inheritdoc />
public string GetAvatarDecorationUrl()
=> AvatarDecorationHash is not null
? CDN.GetAvatarDecorationUrl(AvatarDecorationHash)
: null;

/// <summary>
/// Gets the Username#Discriminator of the user.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public abstract class SocketUser : SocketEntity<ulong>, IUser
public IReadOnlyCollection<ClientType> ActiveClients => Presence.ActiveClients ?? ImmutableHashSet<ClientType>.Empty;
/// <inheritdoc />
public IReadOnlyCollection<IActivity> Activities => Presence.Activities ?? ImmutableList<IActivity>.Empty;

/// <inheritdoc />
public string AvatarDecorationHash { get; private set; }

/// <inheritdoc />
public ulong? AvatarDecorationSkuId { get; private set; }

/// <summary>
/// Gets mutual guilds shared with this user.
/// </summary>
Expand Down Expand Up @@ -98,6 +105,14 @@ internal virtual bool Update(ClientState state, Model model)
GlobalName = model.GlobalName.Value;
hasChanges = true;
}
if (model.AvatarDecoration is { IsSpecified: true, Value: not null }
&& (model.AvatarDecoration.Value.Asset != AvatarDecorationHash || model.AvatarDecoration.Value.SkuId != AvatarDecorationSkuId))
{
AvatarDecorationHash = model.AvatarDecoration.Value?.Asset;
AvatarDecorationSkuId = model.AvatarDecoration.Value?.SkuId;
hasChanges = true;
}

return hasChanges;
}

Expand All @@ -121,6 +136,12 @@ public string GetDefaultAvatarUrl()
? CDN.GetDefaultUserAvatarUrl(DiscriminatorValue)
: CDN.GetDefaultUserAvatarUrl(Id);

/// <inheritdoc />
public string GetAvatarDecorationUrl()
=> AvatarDecorationHash is not null
? CDN.GetAvatarDecorationUrl(AvatarDecorationHash)
: null;

/// <summary>
/// Gets the full name of the user (e.g. Example#0001).
/// </summary>
Expand Down

0 comments on commit f64d9d6

Please sign in to comment.