[Feature] Avatar decorations support (#2782)

* yup

* nullable...

* add props & methos to `GuildUser` & `IUser`
This commit is contained in:
Mihail Gribkov
2023-11-18 23:38:36 +03:00
committed by GitHub
parent b45b7743a5
commit f64d9d6f22
6 changed files with 90 additions and 0 deletions

View File

@@ -257,5 +257,15 @@ namespace Discord
_ => 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";
}
}

View File

@@ -95,6 +95,22 @@ namespace Discord
/// </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>
@@ -120,5 +136,14 @@ namespace Discord
/// 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();
}
}

View File

@@ -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; }
}

View File

@@ -22,6 +22,9 @@ namespace Discord.API
[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; }

View File

@@ -4,6 +4,7 @@ using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;
using EventUserModel = Discord.API.GuildScheduledEventUser;
using Model = Discord.API.User;
@@ -50,6 +51,13 @@ namespace Discord.Rest
/// <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)
{
@@ -96,6 +104,11 @@ namespace Discord.Rest
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 />
@@ -129,6 +142,12 @@ namespace Discord.Rest
? 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>

View File

@@ -47,6 +47,13 @@ namespace Discord.WebSocket
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>
@@ -98,6 +105,14 @@ namespace Discord.WebSocket
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;
}
@@ -121,6 +136,12 @@ namespace Discord.WebSocket
? 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>