diff --git a/src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs b/src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs index 5a5301c2d..4320ed229 100644 --- a/src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs +++ b/src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs @@ -31,14 +31,15 @@ namespace Discord /// public IReadOnlyList RoleIds { get; } /// - /// Gets the User that created this emoji. + /// Gets the cached User that created this emoji. /// /// - /// An optional who created this emoji. An unspecified value only indicates that the creator was not supplied as part of the API response. + /// An optional who created this emoji. + /// An unspecified value only indicates that the creator was not supplied as part of the API response. /// - public Optional Creator { get; } + public Optional> Creator { get; } - internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool requireColons, IReadOnlyList roleIds, Optional creator) : base(id, name, animated) + internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool requireColons, IReadOnlyList roleIds, Optional> creator) : base(id, name, animated) { IsManaged = isManaged; RequireColons = requireColons; diff --git a/src/Discord.Net.Rest/Extensions/EntityExtensions.cs b/src/Discord.Net.Rest/Extensions/EntityExtensions.cs index e1b0810f3..4491711dc 100644 --- a/src/Discord.Net.Rest/Extensions/EntityExtensions.cs +++ b/src/Discord.Net.Rest/Extensions/EntityExtensions.cs @@ -1,5 +1,6 @@ using System.Collections.Immutable; using System.Linq; +using System.Threading.Tasks; namespace Discord.Rest { @@ -7,14 +8,19 @@ namespace Discord.Rest { public static GuildEmote ToEntity(this API.Emoji model, IGuild guild) { - return new GuildEmote(model.Id.Value, model.Name, model.Animated.GetValueOrDefault(), model.Managed, model.RequireColons, ImmutableArray.Create(model.Roles), GetEmoteAuthor(model, guild)); + return new GuildEmote(model.Id.Value, model.Name, model.Animated.GetValueOrDefault(), model.Managed, model.RequireColons, ImmutableArray.Create(model.Roles), GetEmoteAuthorAsync(model, guild).Result); } - internal static Optional GetEmoteAuthor(API.Emoji model, IGuild guild) + internal static async Task>> GetEmoteAuthorAsync(API.Emoji model, IGuild guild) { if (!model.User.IsSpecified || guild == null) - return new Optional(); - return new Optional(guild.GetUserAsync(model.User.Value.Id).Result); + // api did not provide the user, or guild provided was null + return new Optional>(); + // get the cached user, if exists in cache + var cachedUser = await guild.GetUserAsync(model.User.Value.Id, CacheMode.CacheOnly).ConfigureAwait(false); + var isCached = cachedUser != null; + var cacheable = new Cacheable(cachedUser, model.User.Value.Id, isCached, async () => await guild.GetUserAsync(model.User.Value.Id).ConfigureAwait(false)); + return new Optional>(cacheable); } public static Embed ToEntity(this API.Embed model)